Summary of the difference between equals() and ==

== compares the value when comparing basic data types, and compares the address value of the two objects when comparing two objects with the == sign: ( == compares the object stored in the variable (stack) memory ( heap) memory address, which is used to determine whether the addresses of two objects are the same, that is, whether they refer to the same object. The comparison is a real pointer operation);

The source code of the equals method in the Object class :
  1. publicboolean equals(Object obj)   
  2. {  
  3.     returnthis == obj;   
  4. }  

The equals() method exists in the Object class, because the Object class is the direct or indirect parent class of all classes, that is to say, the equals() method in all classes inherits from the Object class, and through the source code, we find that in the Object class The bottom layer of the equals() method relies on the == sign. Then, in all classes that do not override the equals() method, calling the equals() method has the same effect as using the == sign, and is also the address value of the comparison. However, Of all the classes provided by Java, most classes have rewritten the equals() method. The rewritten equals() method generally compares the values ​​of two objects:

String s = "abce" is a very special form that is essentially different from new. It is the only way in java to generate objects without new. Assignment in the form of String s="abce"; is called direct quantity in java, it is placed in the constant pool rather than in the compressed heap like new. Strings in this form are detained within the JVM, that is, after declaring such a string, the JVM will first look for an object with a value of "abcd" in the constant pool, and if so, it will Assign it to the current reference. That is, the original reference and the current reference point to the same object, if not, create a new "abcd" in the constant pool, next time if there is String s1 = "abcd"; s1 points to the object "abcd", that is, a string declared in this form, as long as the values ​​are equal, any multiple references point to the same object.

And String s = new String("abcd"); just like any other object. An object is produced each time it is called, as long as they are called. It can also be understood in this way: String str = "hello"; first find out if there is an object "hello" in the memory, if so, let str point to that "hello". If there is no "hello" in the memory, create a new one The object saves "hello". String str=new String ("hello") is to create a new object to save "hello" regardless of whether the object "hello" already exists in the memory.

Specifically, see the following code:

copy code
1  public  class test1 {
 2      public  static  void main(String[] args) {
 3          String a = new String("ab"); // a is a reference 
4          String b = new String("ab"); // b For another reference, the content of the object is the same 
5          String aa = "ab"; // Put it in the constant pool 
6          String bb = "ab"; // Find it from the constant pool 
7          if (aa == bb) // true 
8              System.out.println("aa==bb" );
 9          if (a == b)// false,非同一对象
10             System.out.println("a==b");
11         if (a.equals(b)) // true
12             System.out.println("aEQb");
13         if (42 == 42.0) { // true
14             System.out.println("true");
15         }
16     }
17 }
copy code
Difference between equals and ==

From the source code of equals, it can be seen that the equals and == defined here are equivalent (the equals in the Object class are no different), the reason for the difference is that some classes (like String, Integer, etc.) have rewritten equals, But classes that do not rewrite equals (such as the class we wrote ourselves) can only inherit the equals method from the Object class, and its equals method is equivalent to ==, unless we override equals in this class.

  There are five points to note about equals again:

  1   自反性:对任意引用值X,x.equals(x)的返回值一定为true;
  2   对称性:对于任何引用值x,y,当且仅当y.equals(x)返回值为true时,x.equals(y)的返回值一定为true;
  3   传递性:如果x.equals(y)=true, y.equals(z)=true,则x.equals(z)=true ;
  4   一致性:如果参与比较的对象没任何改变,则对象比较的结果也不应该有任何改变;
  5   非空性:任何非空的引用值X,x.equals(null)的返回值一定为false 。

String类对equals的重写如下

copy code
 1 public boolean equals(Object anObject) {
 2     if (this == anObject) {
 3         return true;
 4     }
 5     if (anObject instanceof String) {
 6         String anotherString = (String)anObject;
 7         int n = count;
 8         if (n == anotherString.count) {
 9         char v1[] = value;
10         char v2[] = anotherString.value;
11         int i = offset;
12         int j = anotherString.offset;
13         while (n-- != 0) {
14             if (v1[i++] != v2[j++])
15             return false;
16         }
17         return true;
18         }
19     }
20     return false;
21     }
copy code

Also, "==" runs faster than "equals" because "==" just compares references.

Guess you like

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