the = = and equals methods in Java

Source: Internet
Author: User
Tags instance method stub

There are two ways to test the equality of two variables in a Java program: 1, = =; 2, Equals () method

When you use = = To determine whether two variables are equal, if two variables are basic type variables and are basic numeric types (not necessarily requiring the data type to be strictly the same), the value of two variables is returned as true if they are equal.

However, for variables of two reference types, the = = Judgment Returns true only if they point to the same object. = = is not available for two objects that do not have a parent-child relationship on the comparison type.

 Public classEqualtest { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub        intit = 65; floatFL = 65.0f; System.out.println ("are 65 and 65.0f equal?" "+ (it = =FL)); String str1=NewString ("Hello"); String str2=NewString ("Hello"); System.out.println ("Is str1 equal to str2?" "+ (str1==str2)); System.out.println ("Is str1 equal to str2?" " +(Str1.equals (STR2))); }}

The results are as follows:

are 65 and 65.0f equal? true is str1 equal to str2?  falsestr1 is equal to str2?  True

Parsing: For str1 and STR2, because they are reference type variables, they point to two string objects created by the new keyword, so str1 and str2 two variables are not equal.

For starters, String has a very confusing place, what is the difference between the "Hello" direct and the new String ("Hello")? The JVM will use a constant pool to manage these strings, and when using the new string ("Hello"), the JVM will first use a constant pool to manage the "hello" direct amount, and then call the string class's constructor to create a new string object. The newly created string object is saved in heap memory, in other words, the new string ("Hello") produces a total of two string variables.

Constant pool

Chang is designed to manage some of the data that is determined at compile time and saved in the compiled. class file. Includes constants on classes, methods, interfaces, and string constants.

 Public classEqualtest { Public Static voidMain (string[] args) {//TODO auto-generated Method StubString S1 = "HelloWorld"; String S2= "Hello"; String S3= "World"; String S4= "Hello" + "world"; String S5= s2+S3; String S6=NewString ("HelloWorld"); System.out.println ("is S1 equal to S4?" "+ (S1==S4));//trueSystem.out.println ("is S1 equal to S5?") "+ (S1==S5));//falseSystem.out.println ("is S1 equal to S6?") "+ (S1==S6));//false    }}

The JVM constant pool guarantees that the same string has only one direct volume and does not produce multiple replicas. So the strings quoted by S1 and S4 in the example can be determined at compile time. They refer to the same string object in a constant pool.

A string object created using the new string () is created at run time and is saved in the runtime memory area (heap memory) and is not placed in a constant pool.

But most of the time, if the program determines whether the two reference variables are equal, it also wants to have a rule that is similar to "value equality", and does not strictly demand that two reference variables point to the same object and can take advantage of the equal () method.

The equal () method is an instance method provided by the object class, so all reference variables can call the method to determine whether they are equal to other reference variables.

= = and equals methods in Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.