美文网首页
equals() 和“==”的区别

equals() 和“==”的区别

作者: ae12 | 来源:发表于2018-11-12 16:53 被阅读6次

    网上看到的,英语原文,结合代码示例很容易理解。这里贴出地址,以及全文原文(因为不知道是不是被墙着呢)。翻译徒增虚无,原汁原味更容易理解
    Difference between == and .equals() method in Java
    https://www.geeksforgeeks.org/difference-equals-method-java/

    In general both equals() and “==” operator in Java are used to compare objects to check equality but here are some of the differences between the two:

    1. Main difference between .equals() method and == operator is that one is method and other is operator.
      equals()是方法,==是操作符

    2. We can use == operators for reference comparison (address comparison) and .equals() method for content comparison. In simple words, == checks if both objects point to the same memory location whereas .equals() evaluates to the comparison of values in the objects.
      ==是引用类型的地址比较(地址比较)是操作符,而equals()是值比较,简单说:==是检查两个对象是否指向统一内存地址,equals()比较对象的值

    3. If a class does not override the equals method, then by default it uses equals(Object o) method of the closest parent class that has overridden this method. See this for detail

    4. Coding Example:

    // Java program to understand  
    // the concept of == operator 
    public class Test { 
        public static void main(String[] args) 
        { 
            String s1 = new String("HELLO"); 
            String s2 = new String("HELLO"); 
            System.out.println(s1 == s2); 
            System.out.println(s1.equals(s2)); 
        } 
    } 
    

    Output:

    false
    true
    

    Explanation: Here we are creating two objects namely s1 and s2.
    Both s1 and s2 refers to different objects.
    When we use == operator for s1 and s2 comparison then the result is false as both have different addresses in memory.
    Using equals, the result is true because its only comparing the values given in s1 and s2.

    Equality operator(==)

    We can apply equality operators for every primitive types including boolean type. we can also apply equality operators for object types.

    // Java program to illustrate  
    // == operator for compatible data 
    // types 
    class Test { 
        public static void main(String[] args) 
        { 
            // integer-type 
            System.out.println(10 == 20); 
      
            // char-type 
            System.out.println('a' == 'b'); 
      
            // char and double type 
            System.out.println('a' == 97.0); 
      
            // boolean type 
            System.out.println(true == true); 
        } 
    } 
    

    Output:

    false
    false
    true
    true
    

    If we apply == for object types then, there should be compatibility between arguments types (either child to parent or parent to child or same type). Otherwise we will get compile time error.

    // Java program to illustrate  
    // == operator for incompatible data types 
    class Test { 
        public static void main(String[] args) 
        { 
            Thread t = new Thread(); 
            Object o = new Object(); 
            String s = new String("GEEKS"); 
      
            System.out.println(t == o); 
            System.out.println(o == s); 
      
           // Uncomment to see error  
           System.out.println(t==s); 
        } 
    } 
    

    Output:

    false
    false
    // error: incomparable types: Thread and String
    

    .equals()

    In Java, string equals() method compares the two given strings based on the data/content of the string. If all the contents of both the strings are same then it returns true. If all characters are not matched then it returns false.

    public class Test { 
        public static void main(String[] args) 
        { 
            Thread t1 = new Thread(); 
            Thread t2 = new Thread(); 
            Thread t3 = t1; 
      
            String s1 = new String("GEEKS"); 
            String s2 = new String("GEEKS"); 
      
            System.out.println(t1 == t3); 
            System.out.println(t1 == t2); 
      
            System.out.println(t1.equals(t2)); 
            System.out.println(s1.equals(s2)); 
        } 
    } 
    

    Output:

    true
    false
    false
    true
    

    Explanation: Here we are using .equals method to check whether two objects contains the same data or not.

    • In the above example, we are creating 3 Thread objects and 2 String objects.
    • In the first comparison, we are checking that t1 == t3 or not. As we know that both t1 and t3 pointing to same object that’s why it returns true.
    • When we are comparing 2 String objects by .equals() operator then we are checking that is both objects contains the same data or not.
    • Both the objects contains the same String i.e. GEEKS that’s why it returns true.

    This article is contributed by Bishal Kumar Dubey. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

    相关文章

      网友评论

          本文标题:equals() 和“==”的区别

          本文链接:https://www.haomeiwen.com/subject/bceqxqtx.html