惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Security Affairs
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
大猫的无限游戏
大猫的无限游戏
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
爱范儿
爱范儿
阮一峰的网络日志
阮一峰的网络日志
GbyAI
GbyAI
D
Docker
美团技术团队
N
Netflix TechBlog - Medium
罗磊的独立博客
V
Visual Studio Blog
人人都是产品经理
人人都是产品经理
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Hugging Face - Blog
Hugging Face - Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Jina AI
Jina AI
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
M
MIT News - Artificial intelligence
腾讯CDC
MongoDB | Blog
MongoDB | Blog
Last Week in AI
Last Week in AI
博客园 - 三生石上(FineUI控件)
博客园 - 叶小钗
V
V2EX
L
LangChain Blog
博客园 - 【当耐特】
B
Blog RSS Feed
量子位
U
Unit 42
Engineering at Meta
Engineering at Meta
小众软件
小众软件
宝玉的分享
宝玉的分享
H
Help Net Security
Microsoft Azure Blog
Microsoft Azure Blog
云风的 BLOG
云风的 BLOG
博客园 - 聂微东
博客园 - 司徒正美
The Cloudflare Blog
The GitHub Blog
The GitHub Blog
T
Tailwind CSS Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
The Last Watchdog
The Last Watchdog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
SegmentFault 最新的问题
博客园_首页
Attack and Defense Labs
Attack and Defense Labs
TaoSecurity Blog
TaoSecurity Blog
Apple Machine Learning Research
Apple Machine Learning Research
S
Security @ Cisco Blogs

咸糖 - 自律者自由

2025 年终总结:降噪、重构与长期主义 在新加坡和新山吃过最好的食物(持续更新) 写给还在迷茫的你:我的三本大学回忆 2024 年终总结 Neovim: No Crash Incremental Selection 2022 年终总结 使用 neovim 作为 PDE(个性化开发环境) shell 是一个不错的生产力工具 使用二八法则省力地学习 awk 肉身翻墙新加坡安顿指南 使用 Docker Compose 建立你自己的开发环境 关于编写可维护的代码的一些实践与想法 我为什么使用双向链接做笔记? 关于焦虑和拖延症 Golang: 如何处理日渐膨胀的 interface 使用番茄工作法来更好的利用你的时间 Unix 如何杀死一个进程和它的子孙进程? Golang: 让你的零值更有用 使用 Mock 和 Interface 进行 Golang 单测 关于 Golang Slice 的一些细节 总结一些计算机常用的原则 重新学习英语语法 上班族近期小半年入门投资基金组合的学习与实践经历 疫情期间的肉身翻墙新加坡指南 About me 软技能:大厂底层员工打工指南 软技能:我是如何获取知识与信息的? 分布式的令牌桶算法的实现 实现一个AtomicInteger GC root 在哪里? 什么是 Minor GC/Major GC 漏桶算法的设计与实现 剑指offer 单例模式 TCP 针对面试学习 Actor 如何处理阻塞消息 Akka 源码解析 How to learn scala AES 需要限制 SEED 长度 2018年年度总结 Java 集合扩容
Java 如何区分==与.equals()方法
xiantang · 2019-02-24 · via 咸糖 - 自律者自由

文章目录

【注意】最后更新于 December 29, 2025,文中内容可能已过时,请谨慎使用。

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

  1. 最主要的不同是.equals()是方法,==是运算符。
  2. 使用==来进行引用的比较,指的是是否指向同一内存地址。使用.equals()来进行对象内容的比较,比较值。
  3. 如果没有重写.equals就会默认调用最近的父类来重写这个方法。
  4. 示例代码:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// 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)); 
    } 
} 

输出:

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

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

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

等于运算符(==)

我们可以通过==运算符来比较每个原始类型(包括布尔类型),也可以用来比较自定义类型(object types)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
// 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); 
    } 
} 

输出:

1
2
3
4
false
false
true
true

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
// 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); 
    } 
} 

输出:

1
2
3
false
false
// error: incomparable types: Thread and String

.equals()

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
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)); 
    } 
} 

输出:

1
2
3
4
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

文章作者 xiantang

上次更新 2025-12-29 (4c152d04)

赞赏支持

微信打赏 支付宝打赏