Difference between Equals/equals and == operator?

前端 未结 11 830
一生所求
一生所求 2020-11-22 14:14

What is the difference between a == b and a.Equals(b)?

11条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 14:50

    == is a fundamental operator in the language. The operator == tests to see if two object reference variables refer to the exact same instance of an object.

    equals () is an instance method which is fundamentally defined by the java.lang.Object class. The method, .equals() tests to see if the two objects being compared to each other are equivalent , but they need not be the exact same instance of the same object.

    The == operator always gives you the same result, but the equals () method gives you output according to your implementation (implemented logic).Proper overriding of equals: Considerations’ whenever overriding equals () method.

    1. Reflexive: For any non-null reference x, x.equals(x) should return true.

    2. Symmetric: For any non-null reference x and y, if x.equals(y) is true then y.equals(x) must return true.

    3. Transitive: For any non-null reference x , y and z, if x.equals(y) is true, y.equals(z) is true then x.equals(z) must return true.

    4. Consistent: For any non-null reference x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, without changing the information provided for the equals comparisons.

    5. For any non-null reference x, x.equals (null) must return false. NOTE: If o1.equals (o2) is true then o1.hashcode () ==o2.hashcode (), but the reverse may or may not be true.

    Examples:

    Integer i = new Integer(10); Integer j = i;

    in the above code. i == j is true because both i and j refer to the same object.

    Integer i = new Integer (10); Integer j = new Integer(10);

    In the above code, i == j is FALSE because, although they both have the value 10, they are two different objects. But i.equals (j) will return true.

    Using Auto-boxing

    Integer i = 10;
    Integer j = 10;
    Boolean b = (i == j);
    System.out.println (b);

    This will return TRUE because integers between ranges -127 to 128 be pooled, so in this case both are same objects (JVM not going to create a new object it will retrieve it from pool).

    String class overrides the equals method, so here is an example of equals vs. == String s1 = new String ("abc"); String s2 = new String ("abc");

    NOTE: Strings are created in String constant pool so when we create like String s=”abc” it will check the pool by invoking the native method intern (), for its existence in the existing pool if it did not found any String then it will create new one but if we invoke new operator then it will create one new String irrespective of checking the pool for existence.

      public class StringEqualityTest {
        public static void main(String []a){
    
        String s1=new String("abc");
        String s2=new String("abc");
    
         System.out.print("s1.equals(s2)  :"+s1.equals(s2)+"  s1==s2   :");
         System.out.println(s1==s2);
    
         String s3="abc";
         String s4="abc";
    
         System.out.print("s3.equals(s4)  :"+s1.equals(s2)+"  s3==s4   :");
         System.out.println(s3==s4);
    
           }
    
           }
    

    OUTPUT: s1.equals(s2) :true s1==s2 :false s3.equals(s4) :true s3==s4 :true

提交回复
热议问题