The differences between the == operator and the equals method in Java and a detailed explanation of the intern method

  • 2020-07-21 08:14:55
  • OfStack

The differences between the == operator and the equals method in Java and a detailed explanation of the intern method

1. == operator with equals() method
2. Application of hashCode() method
3. intern () method


/* 
Come from xixifeng.com 
Author:  As the wind (StellAah) 
*/ 
public class AboutString2  
{ 
  public static void main(String[]arsgs) 
  { 
    String myName="xixifeng.com"; 
    String myName3="xixifeng";  
    String myName4=".com"; 
    String myName5=myName3+myName4; 
    String myName6="xixifeng.com"; 
     
    if(myName==myName5) 
    { 
       
      System.out.println("myName==myName5"); 
    } 
    else 
    { 
      System.out.println("myName="+myName); 
      System.out.println("myName5="+myName5); 
      System.out.println("myName!=myName5"); 
    } 
    // Run print out : myName!=myName5 
     
    if(myName==myName6) 
    { 
      System.out.println("myName==myName6"); 
    } 
    else 
    { 
      System.out.println("myName!=myName6"); 
    } 
    // Run out : myName!=myName5,myName==myName6 
     
    //myName,myName5(myName5 The value of is made myName3+myName4 To get the ),myName6  this 3 The values of the objects are the same ,  why   Between them == The result of the operation is  myName!=myName5,myName==myName6 ? ? 
    // The reason is that == The operator  
    // clearly == To participate in String The operation is not used to compare values , It's used to compare the equality of objects . 
    // So in the String In the operation , How do I compare their values to each other ,java  provides equals() methods  , It is mainly used to compare the value of an object for equality  
    // The sample is as follows : 
    //myName==myName5  is false ( Is not the same 1 An object ) 
    if(myName.equals(myName5)) 
    { 
      System.out.println("myName.equals(myName5)  The result of the comparison is true !"); 
    } 
    else 
    { 
      System.out.println("myName.equals(myName5)  The result of the comparison is false !"); 
    } 
    // Run output :myName.equals(myName5)  The result of the comparison is true !  In the myName with myName5 Is not the same 1 An object , Full instructions : 
    //equals() methods [ Is used to compare the value of the object is equal ] 
     
    // Raise the question again : Does the hash value of two objects equal to each other mean that they are equal to each other ? 
    //( By the above test myName==myName5  conclusion false  (1) shows that myName with myName5 Is not the same 1 An object ) 
    System.out.println(myName.hashCode()); 
    System.out.println(myName5.hashCode()); 
    // After the test   2. myName with myName5 The hash values are the same  
    // By 1. , 2.   conclusion :  Two-Object hashCode Values are equal , Does not indicate that its objects are also equal . 
 
    // Throw doubt :  How to make myName with myName5 Is equal to ? 
    // The introduction of intern() methods  
    myName5=myName5.intern(); 
    if(myName==myName5) 
    { 
       
      System.out.println("(myName==myName5)  Have to true"); 
    } 
    else 
    { 
      System.out.println("(myName==myName5)  Have to false"); 
    } 
    // Run print out : (myName==myName5)  Have to true 
    // conclusion : intern() Method can make two ( Unequal objects with equal values ) Objects become equal  
    //myName5.intern(); The meaning of , Can be interpreted as : myName5 Find objects in memory to marry themselves , On the condition that , The object must be equal to its own value .  To find the , Specifies the object . 
    //myName5 To find the object , And that , You don't have to create objects anymore , So let's do that , You can save memory resources . 
     
    // Throw doubt :  What objects are recommended intern() ? ? 
    // myName="xixifeng.com" myName6="xixifeng.com", myName with myName6 The objects are equal , The above has been confirmed . 
    //  So said , Objects do not need to be used when assigning the same value directly intern(). 
    //myName="xixifeng.com" myName5=myName3+myName4, They have the same value , But the objects are not equal , The above has been confirmed . 
    // So said ,  Objects are given indirectly ( Possible with existing objects ) The same value , Suggest using 1 Under the intern() methods , Thus objects that exist in common memory . 
     
     
    //== To participate in int In the type of operation , A similar comparison is made  
    int i=8; 
    int j=3; 
    int k=5; 
    int m=j+k; 
    int n=8; 
    if(i==m) 
    { 
      System.out.println("i="+i); 
      System.out.println("m="+m); 
      System.out.println("i==m"); 
    } 
    else 
    { 
      System.out.println("i!=m"); 
    } 
     
    if(i==n) 
    { 
      System.out.println("... ... ... ..."); 
      System.out.println("i="+i); 
      System.out.println("n="+n); 
      System.out.println("i==n"); 
    } 
    else 
    { 
      System.out.println("i!=n"); 
    } 
    // Run out i=m(m The value of the j+k get ),i=n 
    //i,m,n They're all the same  ,  Due to the i==m  conclusion true i==n conclusion true  
    // So we can conclude : == When participating in non-object type operations , It is used to compare the values of constants to see if they are equal     
  } 
} 

In conclusion, the following conclusions can be drawn:

1) the == operator is used to compare whether an object is expected when it participates in an object type operation.
2) the == operator is used to compare values for equality when performing non-object type operations.
3) The equals() method is used to compare the values of two objects for equality
4) Just because two objects have equal hashCode() values does not mean their objects are equal
5) The intern() method can make two objects (whose objects are not equal but whose values are equal) equal, thus sharing existing objects in memory, which can save memory resources.
6) The intern() method under 1 is recommended when the object is indirectly assigned (possibly with the same value as the existing object), so that the object in common memory can be used.

Thank you for reading, I hope to help you, thank you for your support to this site!


Related articles: