Differences between equals and == in Java
Data types in java can be divided into two categories:
1. Basic data type, also known as original data type. byte,short,char,int,long,float,double,boolean
The comparison between them, using the double sign (==), compares their values.
2. Composite data types (classes)
When they compare with (==), they compare their storage address in memory, so unless they compare with the same new object, the result is true, otherwise the result is false. All classes in JAVA are inherited from the base class of Object. A method of equals is defined in the base class of Object. The initial behavior of this method is to compare the memory addresses of objects. But in some class libraries, this method is overwritten. For example, String, Integer and Date have their own implementations of equals in these classes, instead of storing the comparative classes in heap memory. Address.
// Object:
public boolean equals(Object obj) {
return (this == obj);
}
// String: Rewriting Object's equals method
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}
For the equals comparison between composite data types, the comparison between them is based on the address value of their storage location in memory without overwriting the equals method, because Object's equals method is also compared by double equals (==), so the result of comparison is the same as that of double equals (==).
1 public class TestString {
2 public static void main(String[] args) {
3 String s1 = "Monday";
4 String s2 = "Monday";
5 if (s1 == s2)
6 {
7 System.out.println("s1 == s2");}
8 else{
9 System.out.println("s1 != s2");}
10 }
11 }
Compile and run the program, output: s1 = s2 description: s1 and s2 refer to the same String object - "Monday"!
2. A slight change in the procedure will lead to a more strange discovery:
public class TestString {
public static void 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 create s2 with the new operator
Program output:
s1 != s2
s1 equals s2
Note: s1 s2 refers to two "Monday" String objects, respectively
-
String buffer pool
Originally, when the program runs, it creates a string buffer pool. When the expression s2 = Monday is used to create a string, the program first looks for objects with the same value in the String buffer pool. In the first program, s1 is first placed in the pool, so when s2 is created, the program finds s1 with the same value.
Refer s2 to the object "Monday" referenced by s1
In the second section of the program, the new operator is used, and he clearly tells the program, "I want a new one!" Don't be old!" So a new "Monday" Sting object is created in memory. They are of the same value, but in different positions, one swims in the pool and the other rests on the shore. Oh, it's a waste of resources. What do we have to do separately?
4. Change the procedure again:
public class TestString {
public static void 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 accession: s2 = s2.intern();
Program output:
s1 == s2
s1 equals s2
Originally, (the return value of the intern() method of java.lang.String "abc". intern() method is still the string "abc". On the surface, it seems that this method is useless. But in fact, it does a little action: check whether a string like "abc" exists in the string pool, and if it exists, return the string in the pool; if it does not exist, the method adds "abc" to the string pool and then returns its reference.
)