A small experiment on the difference between equals and = = in Java

Source: Internet
Author: User

In Java, equals and = = are often easy to confuse, the simple point is that equals is the comparison of the value is equal, is a method, = = Compare the two are both the same object, is an operator.

Several small experiments were done to compare the results.

Experiment One:

String str1= "AB"; string str2= "AB"; System.out.println (S1==S2);//true
System. Out. println(str1. equals(str2)); True

Both STR1 and str2 point to the same object in the constant pool, so System.out.println (S1==S2), return to true, and of course, str1 and str2 have the same value for the string.

Experiment Two:

1 String str1= "AB"; 2 String str2= "abc"; 3 System.out.println (STR1==STR2); // false 4 System.out.println (Str1.equals (str2)); // false

Here the str1 points to the string AB, initially in the constant pool and did not find the string ABC, then the address store string ABC, and the str2 point to the ABC string, so str1 and str2 and not an object, their string value is not the same.

Experiment Three:

 1  String str1= "AB" ;  2  String str2= "AB" ;  3  String str6=STR2;  4  System.out.println (STR1==STR6); // true  5  System.out.println (STR2==STR6); // true  6  System.out.println (str1.equals (STR6)); // true  7  System.out.println (str2.equals (STR6)); // true  

This assigns the value of the STR2 to STR6 because the AB string already exists in the constant pool, so str1, str2, and STR6 share the same object, and = = also returns true with Equals

Experiment Four:

1 New String ("abc");2 string str2= "abc";3 System.out.println (STR1==STR2);//  false

Two references were created. Two objects were created. Two references point to a different two objects, respectively.
The code above shows that as long as new () is used to create the object, it is created in the heap, and its string is stored separately, even if it is the same as the data in the stack, it is not shared with the data in the stack.

Experiment Five:

 1  String str1= "AB" ;  2  string Str3=new  String ("AB" ); 3  string Str4=new  String ("AB" ); 4  System.out.println (STR3==STR4); // false  5  System.out.println (str1.equals (STR3)); // true  6  System.out.println (str3.equals (STR4)); // true  

because the values of STR3 and STR4 strings are the same, str3. equals(str4) is true, but STR3==STR4 returns false because STR3 and STR4 are not the same object. Also because the values of the str1 and STR3 strings are the same, even though str1 is not pointing to the same object as STR3, Str1.equals (STR3) also returns true

Experiment Six:

1String str1= "AB";2String str3=NewString ("AB");3String str4=NewString ("AB");
String STR5=STR3;4System.out.println (Str1.equals (STR3));//true5System.out.println (Str3.equals (STR4));//true6System.out.println (Str3.equals (STR5));//true7System.out.println (Str4.equals (STR5));//true8System.out.println (STR3==STR5);//true9System.out.println (STR4==STR5);//falseTenSystem.out.println (STR1==STR5);//false OneSystem.out.println (Str1.equals (STR5));//true

This assigns the STR3 to STR5, so str3 and STR5 point to the same object, so STR3==STR5 returns a true value, and Str3.equals (STR5) also returns a true value. Because STR4 and STR5 are not the same object, STR4==STR5 returns false. But also because the value of the string is the same, str4.euqals (STR5) returns a value of true.

Here is all the code for the above experiment:

1  Public classTest2 {3      Public Static voidMain (string[] args)4     {5String str1= "AB";6String str2= "ABC";7String str6=str2;8String str3=NewString ("AB");9String str4=NewString ("AB");TenString str5=STR3; OneSystem.out.println (STR1==STR6);//true ASystem.out.println (STR2==STR6);//true -System.out.println (Str1.equals (STR6));//true -System.out.println (Str2.equals (STR6));//true theSystem.out.println (STR1==STR2);//true -System.out.println (Str1.equals (str2));//true -System.out.println (STR3==STR4);//false -System.out.println (Str1.equals (STR3));//true +System.out.println (Str3.equals (STR4));//true -System.out.println (Str3.equals (STR5));//true +System.out.println (Str4.equals (STR5));//true ASystem.out.println (STR3==STR5);//true atSystem.out.println (STR4==STR5);//false -System.out.println (STR1==STR5);//false -System.out.println (Str1.equals (STR5));//true -     } -}

Replace the string class with an integer wrapper class, and the test code and results are as follows:

1 classTestnumber2 {3      Public Static voidMain (string[] args)4     {5Integer str1=23;6Integer str2=23;7Integer str6=str2;8Integer str3=NewInteger (23);9Integer str4=NewInteger (23);TenInteger str5=STR3; OneSystem.out.println (STR1==STR6);//true ASystem.out.println (STR2==STR6);//true -System.out.println (Str1.equals (STR6));//true -System.out.println (Str2.equals (STR6));//true theSystem.out.println (STR1==STR2);//true -System.out.println (Str1.equals (str2));//true -System.out.println (STR3==STR4);//false -System.out.println (Str1.equals (STR3));//true +System.out.println (Str3.equals (STR4));//true -System.out.println (Str3.equals (STR5));//true +System.out.println (Str4.equals (STR5));//true ASystem.out.println (STR3==STR5);//true atSystem.out.println (STR4==STR5);//false -     } -}

A small experiment on the difference between equals and = = in Java

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.