The difference between "equals" and "= =" in Java

Source: Internet
Author: User

Data types in Java have basic data types and reference data types.

The basic data types are: Int,char,float,long,double,boolean,byte,short (Note: string is not the base data type), and the comparison between the base data types is "= =".

Reference data types include: Integer,char,float,long,double,boolean,byte,short. The reference data type "= =" compares their storage addresses in memory, and equals compares values for equality.

For both types of string and integer, there are two ways in which they are created: a. Using the constructor B. Do not use constructors, so they are more special. string S1 = "Walker"; string s2 = "Walker"; at this point, the result of "S1 = = S2" is true. string S1 = new String ("Walker"); String s2 = new String ("Walker"), at which point the result of "S1 = = S2" is false. Similarly: Integer t1 = 1; Integer t2 = 1; At this point, the result of "T1 = = t2" is true. Integer t1 = new Integer (1); The integer t2 = new Integer (1), at which point the result of "T1 = = t2" is false.

Practice is the only criterion for testing truth, so take a look at the following procedure:

Verify = = and equals, while validating the result of equals in custom objects/** * @author Walker */public class Equalstest {public static void main (string[] args) {String S1 = new String ("Walker"); String s2 = new String ("Walker"); String s3 = "Walker"; String S4 = "Walker"; String S5 = "Walker"; String s6 = "Moro";        String s7 = s5 + S6;  This method is not put into the memory pool, so when compared with = = S3 and s7 return falsestring s8 = "Walker" + "cat";     This way will be put into the memory pool, in fact, it is equal to create a "walker", so with = = Compare S3 and S8 is returned truestring S9 = s5 + "Cat"; This method does not fit into the memory pool, so returns Falseif (S1 = = s2) {System.out.println ("S1 = = s2") When comparing S3 and s9 with = =;} else {System.out.println ("S1! = S2") ;//To}if (S1 = = S3) {System.out.println ("S1 = S3");} else {System.out.println ("S1! = s3");//To}if (s3 = s2) {System.out.prin TLN ("S3 = = s2");} else {System.out.println ("S3! = S2"),//To}if (s3 = = S4) {System.out.println ("S3 = = S4"),//to} else {System.out.println ("S3! = S4 ");} if (s7 = = S8) {System.out.println ("s7 = = S8");} else {System.out.println ("S7! = S8");//To}if (s7 = S9) {System.out.println ("S7 = = S9");} else {System.out.println ("S7! = S9");//To}if (S8 == S9) {System.out.println ("S8 = = S9");} else {System.out.println ("S8! = S9");//To}if (s3 = = S8) {System.out.println ("S3 = = S8 ");//to} else {System.out.println (" S3! = S8 ");} if (s3 = = S7) {System.out.println ("S3 = = s7");} else {System.out.println ("S3! = s7");//To}if (s1.equals (S2)) {SYSTEM.OUT.PR Intln ("S1.equals (S2)"),//pair} if (S2.equals (S3)) {System.out.println ("S2.equals (S3)"),//pair} if (S1.equals (S3)) { System.out.println ("S1.equals (S3)");//To}if (S3.equals (S4)) {System.out.println ("S3.equals (S4)");//To}if (s7.equals (S8)) {System.out.println ("s7.equals (S8)");//To}if (S7.equals (S9)) {System.out.println ("s7.equals (S9)");//To}if ( S8.equals (S9)) {System.out.println ("s8.equals (S9)");//to}integer integer1 = new Integer (888); Integer integer2 = new Integer (888), if (integer1 = = Integer2) {System.out.println ("Integer1 = = Integer2");} else {System.out.println (" Integer1! = integer2 ");//To}if (Integer1.equals (Integer2)) {System.out.println (" integer1.equals (Integer2) ");//To} else {System.out.println ("Integer1 notequalsInteger2 ");}}} 

Output Result:

S1! = s2s1! = s3s3! = S2S3 = s4s7! = s8s7! = s9s8! = S9S3 = s8s3! = s7s1.equals (s2) s2.equals (S3) S1.equals (S3) S3.equals (s 4) s7.equals (S8) s7.equals (S9) s8.equals (S9) Integer1! = Integer2integer1.equals (Integer2)
The result of the output is that for string and integer types, the constructor is used to create an object, and "= =" Compares whether it points to the same reference and "Equlas" compares the values of the two. For direct creation, not through the constructor, the "= =" Comparison is whether the two values are equal (in fact, it can be understood that the comparison is a reference, as to why please see the explanations below).

In addition, for string s3 = "Walker", this approach is actually to save "Walker" in the constant pool, when the creation of string S4 = "Walker", the first JVM in the constant pool to find whether there is "Walker" this string, If you do not create a new string into the constant pool, and then assign the address to S4, if it has already, the constant pool "Walker" corresponds to the address assigned to S4, obviously here is the address assigned to S4, so here S3 and S4 point to the same piece of memory area. So s3 = = S4 Output is true, which is why I said earlier that the constructor's creation of the object is not used in the "= =" Comparison can also be understood as a comparison of references. Because the values of both S3 and S4 are references to "Walker", the comparison of values is actually a comparison of references. The creation of string S8 = "Walker" + "Moro" is actually equivalent to string s8 = "Walker"; so the reference to S8 is the same as the reference to which S3 is pointing. For string s7 = S5 + S6, and string S9 = S5 + "Moro", the two methods of creation are, in fact, the constructor, that is, the same way through the new String (), so the output is false when compared with s3 = = S7 and S = = S9.

Of course for the basic data type, the "= =" Comparison is the value is equal.

Then take a look at the comparison of the custom classes:

public class ObjectEquals {int i;objectequals () {}objectequals (int i) {this.i = i;}}
Verify = = and equals, while validating the result of equals in custom objects/** * @author Walker */public class Equalstest {public static void main (string[] args) {//Compare the object of the custom class objectequals O1 = new ObjectEquals (); ObjectEquals O2 = new ObjectEquals (); o1.i = o2.i = 8;if (O1 = O2) {Sy Stem.out.println ("O1 = = O2");} else {System.out.println ("O1! = O2"),//To}if (O1.equals (O2)) {System.out.println ("o1.equals (O2)");} else {  System.out.println ("O1 notequals O2");//to}objectequals O3 = new ObjectEquals (8); ObjectEquals O4 = new ObjectEquals (8); if (O3 = = O4) {System.out.println ("O3 = = O4");} else {System.out.println ("O3! = O4"),//To}if (O3.equals (O4)) {System.out.println ("o3.equals (O4)");} else { System.out.println ("O3 notequals O4");//To}}}
Output Result:

O1! = O2o1 notequals O2o3! = O4o3 Notequals O4
Explain the result; for a custom class, because the ObjectEquals class I did not override the Equals method, the default is to use Equals () inherited from the object class when Equalstest compares objects of type objectequals. Method. The Equals () method code for the object class is as follows:
Boolean equals (Object o) {return this = = O;}
So if the Equals () method is not overridden, then using the Equals () method in Equalstest is the same as using "= =", which is to compare whether the object pointed to by two variables is the same object, so comparing the two independent objects returns is of course false, If you want to compare values, you must override the Equals () method.

Respect copyright, reprint please indicate this article link

Welcome to follow the public number (Xingzhemoluo), common communication programming experience, scan the following QR code can be;




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.