Difference between == and equals in Java

A.== can be used for basic types and reference types: when used for basic types, it compares whether the values ​​are the same; when used for reference types, it compares whether the objects are the same.
       B. For String a = "a"; Integer b = 1; This type of unique object creation method, the value of == is the same.
       C. There is no equals method for basic types, equals only compares whether the values ​​(contents in the object) are the same (same returns true).
       D. If a class does not define the equals method, it will inherit the equals method in Object by default, and the return value is the same as the == method.
Details:
       ① The essence of == and equals.
       When using "==" to compare variables in JAVA, the system uses the value stored in the "stack" of the variables as the basis for comparison.
       The basic data type stores its content value in the "stack", and the object type stores the address in the "stack", and these addresses point to the objects in the "heap".
       The Object class in the java.lang package has the public boolean equals(Object obj) method, which compares two objects for equality.
       The equals method of other objects returns true only when the contents of the objects pointed to by the two references being compared are the same.
       In short, "==" and "!=" compare addresses. It can also be considered that "==" and "!=" compare object handles; and equals() compares object contents. Or, ,"= =" and "!=" compare the contents of the "stack",
       ②== operator. It is specially used to compare whether the values ​​of two variables are equal, that is, to compare whether the values ​​stored in the memory corresponding to the variables are the same. To compare whether the data of two basic types or two reference variables are equivalent, you can only use = = operator.
      The basic data types of Java are (char, byte, short, int, long, float, double, boolean).
      If the data pointed to by a variable is of object type, then two pieces of memory are involved at this time. The object itself occupies a piece of memory (for memory), and the variable itself also occupies a piece of memory. For example, Object obj = new Object() The variable obj is a Memory, new Object() is a memory, at this time, the data stored in the memory corresponding to the variable is the first address of the memory occupied by the object. For variables that point to object memory, if you want to compare whether two variables point to the same object, that is, to see if the values ​​in the memory corresponding to the two variables are equal, then you need to use the == operator for comparison.
       ③ Differences formed by constructors. For String and Integer, because of their unique way of creating objects. Using the constructor to get an object, the == method comparison produces different results. String a = "abc"; String b = "abc"; At this point a==b gets the result true. String a = new String("abc"); String b = new String("abc"); At this time, the result of a==b is false. Integer a = 1; Integer b = 1; the result of a==b is true. Integer a = new Integer(1); Integer b = new Integer(1); At this time, the result obtained by a==b is false.
       Through this, we can also more easily understand the actual operation of == on memory, which is similar to the basic type comparison.
       String object and string connection pool:
       The inclusion of text in quotation marks is a unique way to create objects of the String class. But the result returned by "==" is true, why? Because in the JVM, there is a string pool, which holds many Strings Object, and can be shared and used, so it improves the efficiency. The string pool is maintained by the String class, and we can call the intern() method to access the string pool. When an object is created using the text enclosed in quotation marks, the created object is added to the string pool. If the next string object is to be created, the JVM will first look in the string pool to see if there is a corresponding string object. If it exists, it will return a reference to the object of the existing object to the object reference to be created. If it does not exist, a new object will be created, and an object reference of the new object will be returned to the object reference to be created. The above paragraph It may be difficult to understand. Using the code to understand that str2 and str1 are two object references, and point to the same object. So '==' returns true.
       Only when the quotation marks contain text to create an object will the created object be created into the string pool. String str = new String("abc") The string object created by this method is not put into the string pool. Therefore, the performance of creating an object with text enclosed in quotation marks is better than the performance of creating a string object with the later method.
String str1 = "abc";

String str2 = "abc";
String str3 = str1+str2; //This creation method is not put into the string pool.

String str4 = str1+"cd"; //This creation method It is not put into the string pool.

String str5 = "ab"+str2; //This creation method is not put into the string pool.

And what about new String()? Let's take a look at the String constructor we called: Java code public String(String original) {    //other code ...   


















As we all know, there are two commonly used methods for creating an instance (object) of a class:
we use new to call the constructor method of the String class above to create an object, and assign its reference to str variable. At the same time, we noticed that the parameter accepted by the called constructor method is also a String object, which is "abc".

Using new to create an object is to call the newInstance method of the Class class, and use the reflection mechanism to create the object.
       ④ equals method. It is used to compare whether the content of two independent objects is the same, just like comparing whether two people look the same, the two objects it compares are independent. For example, for the following code:
String a=new String("foo");
String b=new String("foo");
The two new statements create two objects, and then use the a and b variables to point to them respectively One of the objects, these are two different objects, their first addresses are different, that is, the values ​​stored in a and b are not the same, so the expression a==b returns false, and the two The contents of the objects are the same, so the expression a.equals(b) will return true.
       In actual development, we often need to compare whether the contents of the passed strings are equal. Many people use == for comparison without paying attention. This is wrong, and there are a lot of such errors. Remember, string comparisons basically use the equals method.
       ⑤ If a class does not define the equals method. It will inherit the equals method of the Object class. The implementation code of the equals method of the Object class is as follows:
boolean equals(Object o){
return this==o;
}
This shows that if a class does not define its own equals method, its default equals method (inherited from the Object class) uses the == operator, and also compares whether the objects pointed to by two variables are the same object. At this time, use equals and Using == yields the same result, and always returns false if comparing two separate objects. If you write a class that wants to be able to compare the content of two instance objects created by the class to see if the content is the same, then you must override the equals method and write your own code to decide under what circumstances the content of the two objects can be considered the same.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326488891&siteId=291194637