一般来说equals()"=="运算符是用来比较两个对象是否相等,但是这两者之前还是有许多不同:

  1. 最主要的不同是.equals()是方法,==是运算符。
  2. 使用==来进行引用的比较,指的是是否指向同一内存地址。使用.equals()来进行对象内容的比较,比较值。
  3. 如果没有重写.equals就会默认调用最近的父类来重写这个方法。
  4. 示例代码:

// 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)); 
    } 
} 

输出:

false
true

解释: 这里我们创建了两个对象分别命名为s1s2

  • s1s2是指向不同对象的引用。
  • 当我们使用==来比较s1s2时,返回的是false,因为他们所占用的内存空间不一样。
  • 使用.equals()时,因为只比较值,所以结果时true

我们来分别理解一下这两者的具体区别:

等于运算符(==)

我们可以通过==运算符来比较每个原始类型(包括布尔类型),也可以用来比较自定义类型(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); 
    } 
} 

输出:

false
false
true
true

如果我们使用==来比较自定义类型,需要保证参数类型兼容(compatibility)(要么是子类和父类的关系,要么是相同类型)。否则我们会产生编译错误。

// 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); 
    } 
} 

输出:

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

.equals()

在Java中,使用equals()对于String的比较是基于String的数据/内容,也就是值。如果所有的他们的内容相同,并且都是String类型,就会返回true。如果所有的字符不匹配就会返回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)); 
    } 
} 

输出:

true
false
false
true

解释:这里我们使用.equals方法去检查是否两个对象是否包含相同的值。

  • 在上面的例子中,我们创建来3个线程对象和两个字符串对象。
  • 在第一次比较中,我们比较是否t1==t3,众所周知,返回true的原因是因为t1和t3是指向相同对象的引用。
  • 当我们使用.equals()比较两个String对象时,我们需要确定两个对象是否具有相同的值。
  • String "GEEKS" 对象包含相同的“GEEK”,所以返回true

本文作者:Bishal Kumar Dubey
译者:xiantang
原文地址:Difference between == and .equals() method in Java