The difference between = = and equals is the difference between null and ""

Source: Internet
Author: User

The data types in Java can be divided into two categories:
1. Basic Data Types, also known as the original data type. Byte,short,char,int,long,float,double,boolean
The comparison between them, applying the double equals sign (= =), compares their values.
2. Composite data type (Class)
When they compare with (= =), they compare the storage address in memory, so unless it is the same new object, their comparison result is true, otherwise the result is false. All classes in Java are inherited from the base class of object, and a method of equals is defined in the base class in object, and the initial behavior of this method is to compare the memory address of the object, but in some class libraries This method is overridden, such as string,integer,date in these classes where equals has its own implementation, and is no longer a comparison class in the heap memory of the storage address.
For the equals comparison between composite data types, the comparison between them is based on the address value of the location in memory where the Equals method is not covered, because the Equals method of object is compared with the double equals sign (= =). So the result of the comparison is the same as the double equals sign (= =).
1 public class TestString {2 public  static void Main (string[] args) {3 string S1 = "Monday"; 4 String s2 = "Monday"; 5 if (S1 = = s2) 6 {7 System.out.println ("S1 = = S2"), 8 else{9 System.out.println ("S1! = s2");} 10}11}
Compile and run the program, output: S1 = = S2 Description: S1 and S2 refer to the same String object-"Monday"!
2. Change the program a little bit,There will be more strange discoveries:
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 the S2 with the new operator
Program output:
S1! = S2
S1 equals s2
Description: S1 S2 cited two "Monday" string objects respectively


3. String buffer pool
Originally, the program at run time will create a string buffer pool when using S2 = "Monday" expression is to create a string, the program first in this string buffer pool to find the same value of the object, in the first program, S1 was put into the pool, so when the S2 was created, The program found a S1 with the same value
S2 refers to the object referenced by S1 "Monday"
In the second procedure, the new operator was used, and he understood the telling program: "I want a new one!" Don't be old! "So a new" Monday "Sting object is created in memory. Their values are the same, but the locations are different, one swimming in the pool and one resting on the shore. Alas, it is a waste of resources, obviously the same must be divided into what?

4. Change the program again:
this time join: S2 = S2.intern ();
Program output:
S1 = = S2
S1 equals s2

Originally, (Java.lang.String's Intern () method "abc". The return value of the Intern () method is also the string "abc", which looks as if this method is of little use. But in fact, it did a little trick: Check the string pool for the existence of a string "abc", if present, return the string in the pool, if it does not exist, the method will add "ABC" to the string pool, and then return its reference.



The difference between null and ""

String str1 = null; STR reference is empty
String str2 = ""; str references an empty string


That is, NULL has no space allocated, "" is allocated space, so str1 is not yet an instantiated object, and STR2 has been instantiated.
Note Because NULL is not an object, "" is an object. So the comparison must be the IF (Str1==null) {...} And if (Str2.equals (")") {...}.
objects are compared with equals, and null is compared by an equal sign. Therefore, if str1=null, the following syntax error:
if (Str1.equals ("") | | Str1==null) {//If str1 has no value, then ....
。。。。
}
The correct wording is if (str1==null| | Str1.equals ("")) {///First judge is not an object, if yes, then determine if it is an empty string
//...
}
For example: An empty glass, you can not say that there is nothing in it, because there is air, of course, it can be made into a vacuum, null and "" The difference is like vacuum and air.

The difference between = = and equals is the difference between null and ""

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.