On the difference between equals and equals of == in Java

  • 2020-04-01 01:54:35
  • OfStack

Data types in Java can be divided into two categories:
1. Basic data type, also known as raw data type. Byte, short, char, int, long, float, double, boolean    The comparison between them, using the double equal sign (==), compares their values.
2. Composite data type (class)    When they compare with (==), they are comparing their location in memory, so unless it is the same object that comes out of new, their comparison is true, or false. In all of the JAVA classes are inherited from the base class Object, the Object of the base class defines an equals method, the method of the initial behavior is the Object of memory address, but this method of some libraries have covered off, such as String, Integer, Date in the equals of those class has its own implementation, rather than the comparison classes stored in the heap memory address.     Equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals.


 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 2. If you change the program a little bit, you'll find something even stranger:


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 create s2 with the new operator to output: s1! = s2 s1 equals s2 indicates that s1 s2 refers to two "Monday"String objects respectively
3. A String buffer pool, the original program at run time will create a String buffer pool when using s2 = "Monday" the expression, is to create a String, the program will first in this String buffer pool for the same value object, in the first program, s1 to be put into the pool, so at the time of s2 is created, the program found with the same value of s1 to s2 reference s1 referenced objects "Monday" in the second section of the program, using the new operator, he understood that tell the program: "I want a new! Not old!" A new "Monday" string object is then created in memory. They have the same value, but in different positions, one swims in the pool and the other rests on the shore. Oh, what a waste of resources, clearly is the same to do what?
4. Change the program again:

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

S2 = s2.intern(); Output: s1 == s2 s1 equals s2, (java.lang.String of intern() method "ABC ". Intern () method returns the String" ABC ". But in fact, it does a little trick: it checks if there is a string like "ABC" in the string pool, and if there is, it returns the string in the pool. If it doesn't, the method adds "ABC" to the string pool and then returns a reference to it. )


Related articles: