Differences between equals and equal sign (=) in java: javaequals

Source: Internet
Author: User

Differences between equals and equal sign (=) in java, javaequals

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 comparison between them, apply the double equal sign (=) to compare their values.
2. composite data types (classes) when they are compared with (=), 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 (=.

publicclass TestString {  publicstaticvoid 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:

publicclass TestString { publicstaticvoid 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 use the new operator to create a program output in s2: s1! = S2 s1 equals s2 Description: s1 s2 references two "Monday" String objects respectively.
3. the string buffer pool is originally created when the program is running. When the 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 and references s2 to the second program of the object Monday referenced by s1. It uses the new operator and tells the program clearly: "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:

publicclass TestString { publicstaticvoid 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 original, (java. lang. the intern () method of String "abc ". the Return Value of the 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. )

 

Original article: http://www.jb51.net/article/36499.htm

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.