Difference between == and equals

Summary of the difference between equals and ==

==:

== compares the (heap) memory addresses of objects stored in variable (stack) memory 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.

1. The comparison is whether the operands at both ends of the operator are the same object .
2. The operands on both sides must be of the same type (which can be between parent and child classes) in order to compile.
3. The address is compared. If it is a comparison of specific Arabic numerals, the value is equal to true, such as:
int a=10 and long b=10L and double c=10.0 are the same (true), because they are both Points to the heap at address 10.

equals:

  equals is used to compare whether the contents of two objects are equal. Since all classes inherit from the java.lang.Object class, it applies to all objects. If the method is not overridden, the call is still Object. The method in the class, while the equals method in Object returns the judgment of ==.

  String s="abce" is a very special form, which 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"); is the same as any other object. Each call One object is produced once, 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 ==

The equals method is originally defined in the base class Object of all classes, and the source code is

?
1
2
3
public boolean equals(Object obj) {
     return ( this == obj);
     }

  From the source code of equals, it can be seen that the equals and == defined here are equivalent (the equals in the Object class is 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 Reflexivity: for any reference value X, the return value of x.equals(x) must be true;
  2 Symmetry: for any reference value x, y, if and only if the return value of y.equals(x) is true , the return value of x.equals(y) must be true;
  3 Transitivity: if x.equals(y)=true, y.equals(z)=true, then x.equals(z)=true;
  4 Consistent Properties: If the objects involved in the comparison have not changed, the result of the object comparison should not have any changes;
  5 Non-nullity: For any non-null reference value X, the return value of x.equals(null) must be false.

 

The String class rewrites equals as follows:

Press Ctrl+C to copy the code
public boolean equals(Object anObject) { if (this == anObject) { return true; } if (anObject instanceof String) { String anotherString = (String)anObject; int n = count; if (n == anotherString.count) { char v1[] = value; char v2[] = anotherString.value; int i = offset; int j = anotherString.offset; while (n-- != 0) { if (v1[i++] != v2[j++]) return false; } return true; } } return false; }
Press Ctrl+C to copy the code

Guess you like

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