On the difference between equals and equals (= =) in Java _java

Source: Internet
Author: User

The data types in Java can be grouped into two categories:
1. The basic data type, also known as the original data type. Byte,short,char,int,long,float,double,boolean their comparisons, apply the double equals sign (= =) and compare their values.
2. Composite data types (classes) when they compare with (= =), they compare their storage addresses in memory, so unless the same object is new, their comparison results to 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 overwritten, such as String,integer,   Date in these classes equals has its own implementation, and is no longer the address of the comparison class in heap memory. For equals comparisons between composite data types, the comparison between them is based on the address value of their place in memory, without having to overwrite the Equals method, because the Equals method of the object is also compared with the double equal sign (= =). So the result of the comparison is the same as the result of the double equal sign (= =).

Copy Code code as follows:

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 refer to the same String object-"Monday"! 2. A few more changes to the program, there will be more strange discovery:

Copy Code code as follows:

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 S2 to create the program output with the new operator: S1!= s2 s1 equals s2 Description: S1 s2 reference Two "Monday" string objects, respectively
3. String buffer pool original, when the program runs, it creates a string buffer pool when an expression such as S2 = "Monday" is used to create a string, the program first looks for the same object in the string buffer pool, and in the first program, the S1 is first placed in the pool, So when S2 was created, the program found a S1 that had the same value and S2 referenced the object "Monday" in the second procedure, using the new operator, and he understood that he told the 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 positions are different, one swims in the pool and a rest on the shore. Alas, it is a waste of resources, obviously the same must be done separately?
4. Change the procedure again:
Copy Code code as follows:

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");
}
}
}

This time add: S2 = S2.intern (); Program output: S1 = = s2 S1 equals s2 original, (Java.lang.String intern () method "abc". The return value of the Intern () method or the string "ABC" appears to be useless on the surface. But in fact, it does a little trick: Check the string pool for "abc" such a string, if it exists, return the string in the pool, if not, the method will add "ABC" to the string pool, and then return its reference. )

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.