Java 如何区分==与.equals()方法

本文深入探讨了Java中equals()方法与==运算符的区别,解释了两者在对象比较中的作用。==用于比较引用是否指向同一内存地址,而equals()则用于比较对象的内容是否相同。通过示例代码展示了如何正确使用这两个工具进行对象比较。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一般来说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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值