当前位置:网站首页>The difference between == and equals()
The difference between == and equals()
2022-08-06 13:14:00 【big sun flower princess】
1, equals()
What are the execution results of the following codes?
public class Main {public static void main(String[] args) {String s1 = "hello world";String s2 = "hello world";String s3 = new String("hello world");String s4 = "hello"+"world";String s5 = "hello"+new String("world");String s6 = "hello";String s7 = s6+"world";System.out.println(s1.equals(s2));System.out.println(s1.equals(s3));System.out.println(s1.equals(s4));System.out.println(s1.equals(s5));System.out.println(s1.equals(s7));}}The above output results are true for the following reasons:
equals() can only be used between reference types. It is defined in the Object class. The default is to compare addresses. It is often overridden to compare whether specific property values are the same.The String class overrides equals() to compare the contents of strings for the same.
2, reference type
What is the result of executing the following code?
public class Main {public static void main(String[] args) {String s1 = "hello world";String s2 = "hello world";String s3 = new String("hello world");String s4 = "hello"+"world";String s5 = "hello"+new String("world");String s6 = "hello";String s7 = s6+"world";System.out.println(s1==s2);System.out.println(s1==s3);System.out.println(s1==s4);System.out.println(s1==s5);System.out.println(s1==s7);}}Running the above code, we can see that the results of s1==s2 and s1==s4 are true, and the other results are false.
The reasons are as follows:
Since the String type is a reference data type rather than a basic data type, == here compares whether the referenced addresses are the same.
Here, s1 creates a new literal object "hello world", which is stored in the heap and cached in the constant pool at the same time, so s2 directly reuses the objects in the constant pool.Therefore, using the same address of the two, the result of s1==s2 is true.
s3 will create another string object when new String() is performed, and refer to the content of the string, so the addresses of s3 and s1 are not the same, so the result of s1==s3 returns false.Similarly, the result of s1==s5 also returns false.
Since s4 uses two literals for splicing, it will directly reuse the objects in the constant pool.Therefore, the address of s4 is the same as that of s1, so the result of s1==s4 returns true.
Because s6 is a variable, it will not be directly connected at compile time, but a new object will be created to store hello world.Therefore, the addresses of s7 and s1 are not the same, and the result returns false.
3, wrapper class
What is the result of executing the following code?
public class Main {public static void main(String[] args) {Integer i1 = 127;Integer i2 = 127;Integer i3 = 128;Integer i4 = 128;Integer i5 = new Integer(127);int i6 = 127;System.out.println(i1==i2);System.out.println(i3==i4);System.out.println(i1==i5);System.out.println(i1==i6);}}Executing the above code, we can see that the result of i1==i2 is true, the result of i3==i4 is false, the result of i1==i5 is false, and the result of i1==i6 is true.
The reason is as follows: Integer.valueOf() will reuse the data in the range of -128 to 127, so the range between -128 and 127 will return true, that is, the result of i1==i2 is true, i3==i4The result is false.Since i5 creates a new object during new Integer(), the addresses of i1 and i5 are not the same, so the result returns false.When i6 and i1 are == compared, the basic package type will be automatically unboxed to the basic type and then compared, so Integer() will be automatically unboxed to the int type and then compared, so it returns true.
边栏推荐
- 生产级Redis 高并发分布式锁实战2:缓存架构设计问题优化
- SQL图解面试题:如何找到破产玩家?(交叉连接)
- Dry goods | Those ingenious pooling operations, I burst after watching!
- “恰好装满求最值”背包问题的初始化解析
- 【TypeScript】深入学习TypeScript命名空间
- leetcode 105. 从前序与中序遍历序列构造二叉树
- leetcode.10 Regular Expressions
- 【TypeScript】深入学习TypeScript装饰器
- Web page side IM products RainbowChat - Web v4.1 version has been released
- 解决spacedesk卸载/重装软件时显示 指定的账户已存在
猜你喜欢

TcpServer::start都做了些什么

【TypeScript 必学必会】 关于TypeScript你必须知道的一切

易知微数字孪生智慧港口|打造智能化调度综合管控“大脑”,实现港口建设“新升级”

接口的安全设计三要素:ticket,签名,时间戳

速览muduo组成结构

剖析SGI STL空间配置器(deallocate内存回收函数和reallocate内存扩充函数)

3D格式转换工具HOOPS Exchange最全技术指南(一):4大功能特征与典型使用场景

Hands-on deep learning_ResNet

Yizhiwei Digital Twin Smart Port | Create a "brain" for intelligent dispatching and comprehensive management and control, and realize a "new upgrade" of port construction

高性能云原生数据对象存储MinIO实战-上
随机推荐
The "Pytorch Common Functions Manual" compiled by Dr. Harbin Institute of Technology for half a year is open for download!Contains more than 200 functions! …
高性能云原生数据对象存储MinIO实战-上
接口的安全设计三要素:ticket,签名,时间戳
【TypeScript 必学必会】 关于TypeScript你必须知道的一切
mybaits-plus笔记
洛谷 P1776:宝物筛选 ← 多重背包问题 二进制优化
Web page side IM products RainbowChat - Web v4.1 version has been released
==和equals()的区别
Promrtheus etcd 监控
TensorRT详细入门指南
解决创建虚拟机时No DEFAULT or UI configuration directive found问题
Unity tool class ResourcesManager resource manager
分布式锁的应用实践 | 微服务申请逐渐递增且不重复的号码
Wang Shuang Assembly Language Chapter 6: Programs Containing Multiple Segments
动物主题网页设计(小白必看)
Yizhiwei Digital Twin Smart Port | Create a "brain" for intelligent dispatching and comprehensive management and control, and realize a "new upgrade" of port construction
Luogu P1776: Treasure Screening ← Multiple Knapsack Problem Binary Optimization
【 TypeScript will learn will be 】 you must know all about TypeScript
SAP 特征 Classification
KVM 简介