代码详解java里的“==”和“equels”区别
测试1:
先看一组String类型比较,废话不多说,直接上代码:
public class Test { public static void main(String[] args) { String a = "java书苑"; String b = "java书苑"; String c = new String("java书苑"); String d = new String("java书苑").intern(); if(a == b){ System.out.println("a == b"); }else{ System.out.println("a != b"); } if(a.equals(b)){ System.out.println("a.equals(b)"); }else{ System.out.println("!a.equals(b)"); } if(a == c){ System.out.println("a == c"); }else{ System.out.println("a != c"); } if(a.equals(c)){ System.out.println("a.equals(c)"); }else{ System.out.println("!a.equals(c)"); } if(a == d){ System.out.println("a == d"); }else{ System.out.println("a != d"); } if(a.equals(d)){ System.out.println("a.equals(d)"); }else{ System.out.println("a.equals(d)"); } } }
输出结果:
a == b a.equals(b) a != c a.equals(c) a == d a.equals(d)
总结:
结果a == b:程序在运行的时候会创建一个字符串缓冲池,在String a = “java书苑”时, “java书苑”被放到了字符串缓冲池中,当 String b = “java书苑” 创建字符串的时候,程序首先会在这个String缓冲池中寻找相同值的对象,所以在b被创建的时候,程序找到了具有相同值的a,将b 引用 a 所引用的对象。所以a和b引用的同一个对象,故a == b。
结果a != c:String c = new String(“java书苑”)时new了一个新的对象,故不从String缓冲池寻找,二十直接创建一个新的对象。所以a != c。
结果a == d :当调用 intern 方法时,如果池已经包含一个等于此 String 对象的字符串(该对象由 equals(Object) 方法确定),则返回池中的字符串。否则,将此 String 对象添加到池中,并且返回此 String 对象的引用。所有d调用的同样是a的对象。
equals比较的是值,故值一样时便相等。
测试2:
这是一组int类型和Integer类型的测试:
public class Test { public static void main(String[] args) { int a = 127; int a1 = 127; int b = 128; int b1 = 128; Integer c = 127; Integer c1 = 127; Integer d = 128; Integer d1 = 128; if(a == a1){ System.out.println("a == a1"); }else{ System.out.println("a != a1"); } if(b == b1){ System.out.println("b == b1"); }else{ System.out.println("b != b1"); } if(c == c1){ System.out.println("c == c1"); }else{ System.out.println("c != c1"); } if(d == d1){ System.out.println("d == d1"); }else{ System.out.println("d != d1"); } } }
输出的结果:
a == a1 b == b1 c == c1 d != d1
结果”a == a1”和”b == b1”:int 是基本类型,直接存数值,而integer是对象,用一个引用指向这个对象,多以比较的时候”a == a1”和”b == b1”。
结果“c == c1”和“d != d1”这里可能有人会有疑问,为什么“d != d1”.我们一起看一下Integer的源码。
/** * Cache to support the object identity semantics of autoboxing for values between * -128 and 127 (inclusive) as required by JLS. * * The cache is initialized on first usage. The size of the cache * may be controlled by the -XX:AutoBoxCacheMax=<size> option. * During VM initialization, java.lang.Integer.IntegerCache.high property * may be set and saved in the private system properties in the * sun.misc.VM class. */ private static class IntegerCache { static final int low = -128; static final int high; static final Integer cache[]; static { // high value may be configured by property int h = 127; String integerCacheHighPropValue = sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high"); if (integerCacheHighPropValue != null) { int i = parseInt(integerCacheHighPropValue); i = Math.max(i, 127); // Maximum array size is Integer.MAX_VALUE h = Math.min(i, Integer.MAX_VALUE - (-low) -1); } high = h; cache = new Integer[(high - low) + 1]; int j = low; for(int k = 0; k < cache.length; k++) cache[k] = new Integer(j++); } private IntegerCache() {} } /** * Returns an {@code Integer} instance representing the specified * {@code int} value. If a new {@code Integer} instance is not * required, this method should generally be used in preference to * the constructor {@link #Integer(int)}, as this method is likely * to yield significantly better space and time performance by * caching frequently requested values. * * This method will always cache values in the range -128 to 127, * inclusive, and may cache other values outside of this range. * * @param i an {@code int} value. * @return an {@code Integer} instance representing {@code i}. * @since 1.5 */ public static Integer valueOf(int i) { assert IntegerCache.high >= 127; if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); }
结论:这里 Integer 会初始化一个[-128,127]的常量池,如果数值在这个范围时,则引用的是同一个对象,如果不在这个范围,通过源码可以看出返回的是new了一个新的对象: return new Integer(i);
所以,结果“c == c1”是引用了同一个对象,结果“d != d1”,是new了一个新的对象,故不等。
- 一条关于swap争用的报警邮件分析(一)(r7笔记第28天)
- Spring-IOC(2)
- Python3 pandas read_csv 读取txt文件报错:IOError: Initializing from file failed
- dataguard添加临时数据文件的bug(r7笔记第27天)
- 简单易学的机器学习算法——线性支持向量机
- Java设计模式-模板方式模式
- 由一条create语句的问题对比mysql和oracle中的date差别 (r7笔记第26天)
- Java设计模式-命令模式
- 关于执行计划中的%CPU的含义 (r7笔记第25天)
- Java设计模式-享元模式
- 简单易学的机器学习算法——线性可分支持向量机
- 基于gensim的Doc2Vec简析,以及用python 实现简要代码
- 关于db link权限分配的苦旅(一) (r7笔记第42天)
- Java设计模式-责任链模式
- java教程
- Java快速入门
- Java 开发环境配置
- Java基本语法
- Java 对象和类
- Java 基本数据类型
- Java 变量类型
- Java 修饰符
- Java 运算符
- Java 循环结构
- Java 分支结构
- Java Number类
- Java Character类
- Java String类
- Java StringBuffer和StringBuilder类
- Java 数组
- Java 日期时间
- Java 正则表达式
- Java 方法
- Java 流(Stream)、文件(File)和IO
- Java 异常处理
- Java 继承
- Java 重写(Override)与重载(Overload)
- Java 多态
- Java 抽象类
- Java 封装
- Java 接口
- Java 包(package)
- Java 数据结构
- Java 集合框架
- Java 泛型
- Java 序列化
- Java 网络编程
- Java 发送邮件
- Java 多线程编程
- Java Applet基础
- Java 文档注释
- Flutter基础widgets教程-AspectRatio篇
- go2school-2
- go2school-1
- [源码阅读] 阿里SOFA服务注册中心MetaServer(1)
- Hadoop项目:从cdn日志统计直播流量
- Flutter基础widgets教程-Baseline篇
- 常用Linux脚本集锦
- 大型前端项目的断点调试共享化和复用化实践
- 常用Linux命令集锦
- 使用Azure Functions玩转Serverless
- Best practice on when to use the wrapper class and primitive type in Java
- Flutter基础widgets教程-BottomNavigationBar篇
- 直播系统介绍
- Azure内容审查器之羞羞图审查
- Clickhouse简介和性能对比