Difference between java equal and =

Source: Internet
Author: User

Difference between java equal and =

Today I read thinking in java and see the difference between equal and =. However, I only know the equal comparison content, =. Today, we found that the equal of the original Object class is also implemented with =, and the comparison content is also the memory address. Only the overwritten equal methods can be compared.


Below are some more detailed explanations, excerpted to enhance memory.

Bytes -------------------------------------------------------------------------------------

Java data types can be divided into two types:
1. The basic data type, also known as the original data type. Byte, short, char, int, long, float, double, boolean
The comparison between them applies the double equal sign (=) to compare their values.
2. Composite data type (class)
When they use (=) for comparison, they compare their storage addresses in the memory, so unless they are the same new object, their comparison result is true, otherwise the comparison result is false. All classes in JAVA are inherited from the base class Object. An equals method is defined in the base class of the Object. The initial behavior of this method is to compare the memory address of the Object, however, in some class libraries, this method is overwritten, such as String, Integer, and Date. Among these classes, equals has its own implementation, instead of the storage address of the comparison class in the heap memory.
For equals comparison between composite data types, if the equals method is not overwritten, the comparison between them is based on the address value of their storage location in the memory, because the equals method of the Object is compared with the binary equal sign (=), the comparison result is the same as that of the binary equal sign (=.

public class TestString {  public static void main(String[] args) { String s1 = "Monday"; String s2 = "Monday"; if (s1 == s2) { System.out.println("s1 == s2");} else{ System.out.println("s1 != s2");} } }



Compile and run the program. Output: s1 = s2 Description: s1 and s2 reference the same String object -- "Monday "!

2. If you change the program a little bit, you may find it strange:

public class TestString {public static void main(String[] args) {String s1 = "Monday";String s2 = new String("Monday");if (s1 == s2){System.out.println("s1 == s2");}else{System.out.println("s1 != s2");}if (s1.equals(s2)) {System.out.println("s1 equals s2");}else{System.out.println("s1 not equals s2");}}}


We will create s2 with the new operator
Program output:
S1! = S2
S1 equals s2
Note: s1 s2 references two "Monday" String objects respectively.

3. String Buffer Pool
It turns out that the program will create a string buffer pool when running. When expression s2 = "Monday" is used to create a string, the program first searches for objects with the same value in the String buffer pool. In the first program, s1 is first placed in the pool. Therefore, when s2 is created, the program finds s1 with the same value
Reference s2 to the object referenced by s1 "Monday"
In the second stage, the new operator is used, and he clearly tells the program: "I want a new one! Don't be old! "So a new" Monday "Sting object is created in the memory. They have the same value but different locations. One is swimming in the pool and the other is resting on the shore. Oh, it's a waste of resources. What should we do separately?

4. Change the program again:

public class TestString {public static void main(String[] args) {String s1 = "Monday";String s2 = new String("Monday");s2 = s2.intern();if (s1 == s2){System.out.println("s1 == s2");}else{System.out.println("s1 != s2");}if (s1.equals(s2)) {System.out.println("s1 equals s2");}else{System.out.println("s1 not equals s2");}}}


Join this time: s2 = s2.intern ();
Program output:
S1 = s2
S1 equals s2
Originally, the return value of the (java. lang. String's intern () method "abc". intern () method is still the String "abc". On the surface, it seems that this method is useless. But in fact, it performs a small action: Check whether there is a string such as "abc" in the string pool. If yes, the string in the pool is returned; if not, this method adds "abc" to the string pool and then returns its reference.

)


Reference http://www.cnblogs.com/zhxhdean/archive/2011/03/25/1995431.html

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.