Data types in Java can be divided into two categories:
1. Basic data types, also called primitive data types. To compare byte, short, char, int, long, float, double, boolean, use the double equal sign (==), and compare their values.
2. Composite data types (classes) When they are compared using (==), they are compared with their storage addresses in memory. Therefore, unless they are the same new object, the result of their comparison is true. , otherwise the comparison result is false. All classes in JAVA inherit from the base class Object. The base class in Object defines an equals method. The initial behavior of this method is to compare the memory addresses of objects. However, in some class libraries this method is Overridden, such as String, Integer, and Date, equals has its own implementation in these classes, instead of comparing the storage address of the class in the heap memory. For equals comparison between composite data types, without overriding the equals method, the comparison between them is still based on the address value of their storage location in memory, because the equals method of Object also uses a double equal sign ( ==) is compared, so the result of the comparison is the same as the result of the double equal sign (==).
Copy the code code as follows:
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 Note: s1 and s2 refer to the same String object - "Monday"! 2. If you change the program slightly, you will find something even stranger:
Copy the code code as follows:
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 use the new operator to create program output for s2: s1 != s2 s1 equals s2 Note: s1 and s2 refer to two "Monday" String objects respectively.
3. String buffer pool It turns out that the program will create a string buffer pool when it is running. When using an expression such as s2 = "Monday" to create a string, the program will first look for the same value in the String buffer pool. Object, in the first program, s1 was first put into the pool, so when s2 was created, the program found s1 with the same value and referenced s2 to the object "Monday" referenced by s1. In the second program, Used new operator, he clearly tells the program: "I want a new one! Not the old one!" So a new "Monday" Sting object is created in memory. Their values are the same, but their locations are different, one is swimming in the pool and the other is resting on the shore. Oops, what a waste of resources. Why do we have to separate them when they are obviously the same?
4. Change the program again:
Copy the code code as follows:
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");
}
}
}
This time added: s2 = s2.intern(); Program output: s1 == s2 s1 equals s2 It turns out that (java.lang.String's intern() method "abc". The return value of the intern() method is still the string "abc". On the surface, it seems that this method is of no use. But in fact, it does a small trick : Check whether there is a string "abc" in the string pool. If it exists, return the string in the pool; if it does not exist, this method will add "abc" to the string pool and then return it. citation.)