java "equals" and "==" similarities
First, let’s briefly talk about “equal” and “==”
== Operation compares whether the values of the two variables are equal for the basic data type.
For reference variables, it means whether the addresses stored in the heap are the same,
Is the content in the stack the same?
Whether the two variables represented by the equals operation are references to the same object,
That is, whether the content in the heap is the same.
In summary, == compares the addresses of 2 objects, while equals compares the contents of 2 objects.
Let me briefly introduce the String class
String class is also called immutable character sequence
String uses private final char value[] to realize the storage of strings. That is to say, after the String object is created, the string content stored in this object cannot be modified. The String class has a special creation method, which is to use "" double quotes to create it. For example, new String("123") actually creates 2 String objects, one is created by "123" through "" double quotes, and the other is created by new. However, the periods they create are different, one is the compilation period and the other is the run period. Java overloads the + operator for String type, and you can directly use + to concatenate two strings. Calling the intern() method of the String class during runtime can dynamically add objects to the String Pool.
Differentiate between two methods of creating String objects "" and new()
String is a special packaging data. Can be used:
String str1 = new String("123");String str2 = "123";Create two forms
The first one is to use new() to create a new object, which will be stored in the heap. A new object is created every time it is called. (It is actually two, as mentioned above, but after the existence of "123" in the constant pool, the new "123" will no longer be created in the constant pool)
The second type is to first create a variable str to the object of the String class in the stack, and then use symbolic reference to find out if there is "abc" in the string constant pool. If not, store "abc" into the string constant pool and let str point to "abc". If there is already "abc", then directly let str point to "abc".
We should pay attention at this time
On the one hand, the first writing method is beneficial and saves memory space. At the same time, it can improve the running speed of the program to a certain extent, because the JVM will automatically decide whether it is necessary to create a new object based on the actual situation of the data in the stack. For the code of String str = new String("123");, new objects are created in the heap regardless of whether the string values are equal or not, whether it is necessary to create new objects, thereby increasing the burden on the program. On the other hand, when we define a class using a format such as String str = "123";, we always take it for granted that we create an object str of the String class.
The object may not have been created! And maybe just point to an object that has been created previously. Only through the new() method can we ensure that a new object is created every time.
Please see the following example
package testString; public class testString { public static void main(String[] args) { String a = "123"; String b = "123"; System.out.println(a==b); System.out.println(a.equals(b)); System.out.println("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- And the content of the two referenced objects in the heap is the same, so a.equals(b) is true */ String c = new String("1234"); String d = new String("1234"); System.out.println(c==d); System.out.println(c.equals(d)); System.out.println("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- But c is the same as d heap, so c.equals(d) is true */ String e = "a1"; String f = "a"+1; System.out.println(e==f); System.out.println(e.equals(f)); System.out.println("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- * Yes, Java itself has certain operator overloading, but you cannot use the operator overloading that defines itself. Unlike c++, String f = "a"+1; * This sentence will be made by the compiler String f = "a1"; this is the same as the first case we talked about, so I won't repeat it. * The reason why the compiler does this is because it can determine when compiled */ String g = "gh"; String hh = "h"; String h = "g" + hh ; System.out.println(g==h); System.out.println(g.equals(h)); System.out.println("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Thank you for reading, I hope it can help you. Thank you for your support for this site!