Java中==和equals区别
创始人
2024-05-29 01:50:33
0

文章目录

  • Java中==和equals区别
  • 1. Integer中==和equals的问题
    • 1.1 Integer类型且不是通过new创建的比较
    • 1.2 手动new Integer()创建的比较
    • 1.3 Integer和int比较
  • 2. String中==和equals的问题
    • 3. Demo

Java中==和equals区别

  • equals是方法,==是运算符
  • ==: 如果比较的对象是基本数据类型,则比较的是数值是否相等;如果比较的是引用数据类型,则比较的是对象的地址是否相等
  • equals():用来比较两个对象的内容是否相等
    注意::equals方法不能用于基本数据类型的变量,如果没有对equals方法进行重写,则比较的是引用类型的变量所指向的对象的地址

1. Integer中==和equals的问题

1.1 Integer类型且不是通过new创建的比较

Integer类型且值[-128,127]在这个范围,并且不是通过new创建,这时候会调用Integer.valueOf()自动装箱方法,在这个方法中,值[-128,127]在这个范围则会从缓存中去取对象而不是new一个对象;否则重新new对象

public static Integer valueOf(int i) {if (i >= IntegerCache.low && i <= IntegerCache.high)return IntegerCache.cache[i + (-IntegerCache.low)];return new Integer(i);}
/*** 值位于[-128,127]之间则会从缓存中去取,而不是重新new一个对象*/
Integer x = 100;
Integer y = 100;
System.out.println(x==y);//true/*** 都是Integer并且两者的值不是在[-128,127]之间* 这时返回的是重新new出来两个对象,因此这两个对象的地址不同,故==比较的值不同* 要想返回值相同需要使用equals方法*/
Integer a = 128;
Integer b = 128;
System.out.println(a==b);//false
System.out.println(a.equals(b));//true

1.2 手动new Integer()创建的比较

手动new Integer()创建对象,不走自动装箱机制,没有调用Integer.valueOf()方法,使用==比较的地址是不同的

/*** 自己手动new Integer创建了了两个对象* 不走自动装箱机制,没有调用Integer.valueOf()方法,这是两个不同的对象,故==比较的地址是不同的* 要比较两个值,需要使用equals方法*/Integer m = new Integer(120);Integer n = new Integer(120);System.out.println(m==n);//falseSystem.out.println(m.equals(n));//true

1.3 Integer和int比较

Integer和int使用==比较的是值,而不是内存地址

Integer a1 = new Integer(110);
int a2 = 110;
Integer a3 = 110;
System.out.println("a1==a2 is "+(a1==a2));//a1==a2 is true
System.out.println("a2==a3 is "+(a2==a3));//a2==a3 is true
System.out.println("a1==a3 is "+(a1==a3));//a1==a3 is false
System.out.println("a1.equals(a2) is "+a1.equals(a2));//a1.equals(a2) is true
System.out.println("a3.equals(a2) is "+a3.equals(a2));//a3.equals(a2) is true
System.out.println("a1.equals(a3) is "+a1.equals(a3));//a1.equals(a3) is true

注意:

  1. a1是new Integer()创建的对象,没有调用Integer.valueOf()自动装箱方法,而a3是会调用Integer.valueOf()自动装箱方法从缓存中取到的对象,a1==a3返回是false。
  2. a1.equals(a2)没有问题,a2.equals(a1)会报错Cannot resolve method ‘equals(java.lang.Integer)’

2. String中==和equals的问题

String str1 = "abc";
String str2 = new String("abc");
String str3 = "abc";
String str4 = new String("abc");/*** str2和str2不同引用地址*/
System.out.println("str1==str2 is "+(str1==str2)); //str1==str2 is false/*** str1和str3都在公共池中,引用相同*/
System.out.println("str1==str3 is "+(str1==str3));//str1==str3 is true
/*** str2和str4堆中不同引用地址*/
System.out.println("str2==str4 is "+(str2==str4));//str2==str4 is false
System.out.println("str1.equals(str2) is "+str1.equals(str2));//str1.equals(str2) is true
System.out.println("str1.equals(str3) is "+str1.equals(str3));//str1.equals(str3) is true
System.out.println("str2.equals(str4) is "+str2.equals(str4));//str2.equals(str4) is true

3. Demo

public class TestEquals {public static void main(String[] args) {//testInteger();testString();}private static void testString() {String str1 = "abc";String str2 = new String("abc");String str3 = "abc";String str4 = new String("abc");/*** str2和str2不同引用地址*/System.out.println("str1==str2 is "+(str1==str2)); //str1==str2 is false/*** str1和str3都在公共池中,引用相同*/System.out.println("str1==str3 is "+(str1==str3));//str1==str3 is true/*** str2和str4堆中不同引用地址*/System.out.println("str2==str4 is "+(str2==str4));//str2==str4 is falseSystem.out.println("str1.equals(str2) is "+str1.equals(str2));//str1.equals(str2) is trueSystem.out.println("str1.equals(str3) is "+str1.equals(str3));//str1.equals(str3) is trueSystem.out.println("str2.equals(str4) is "+str2.equals(str4));//str2.equals(str4) is true}private static void testInteger() {/*** 都是Integer且值都在[-128,127]之间,并且不是通过new创建* 值位于[-128,127]之间则会从缓存中去取,而不是重新new一个对象*/Integer x = 100;Integer y = 100;System.out.println(x==y);//true/*** 都是Integer并且两者的值不是在[-128,127]之间,并且不是通过new创建* 这时返回的是重新new出来两个对象,因此这两个对象的地址不同,故==比较的值不同* 要比较两个值,需要使用equals方法*/Integer a = 128;Integer b = 128;System.out.println(a==b);//falseSystem.out.println(a.equals(b));//true/*** 自己手动new Integer创建了了两个对象* 因为手动new,所以不走自动装箱机制,没有调用Integer.valueOf()方法,这是两个不同的对象,故==比较的地址是不同的* 要比较两个值,需要使用equals方法*/Integer m = new Integer(120);Integer n = new Integer(120);System.out.println(m==n);//falseSystem.out.println(m.equals(n));//trueInteger a1 = new Integer(110);int a2 = 110;Integer a3 = 110;System.out.println("a1==a2 is "+(a1==a2));//a1==a2 is trueSystem.out.println("a2==a3 is "+(a2==a3));//a2==a3 is trueSystem.out.println("a1==a3 is "+(a1==a3));//a1==a3 is falseSystem.out.println("a1.equals(a2) is "+a1.equals(a2));//a1.equals(a2) is trueSystem.out.println("a3.equals(a2) is "+a3.equals(a2));//a3.equals(a2) is trueSystem.out.println("a1.equals(a3) is "+a1.equals(a3));//a1.equals(a3) is true}
}

相关内容

热门资讯

初中生晨会主持词 初中生晨会主持词(通用5篇)  主持词要尽量增加文化内涵、寓教于乐,不断提高观众的文化知识和素养。现...
晨会主持词开场白   开晨会是公司职场管理的规章制度,那么公司晨会主持如何开场白好呢?以下是小编为大家搜集整理提供到的...
企业年会主持词 企业年会主持词  主持词是主持人在台上表演的灵魂之所在。在人们越来越多的参与各种活动的今天,主持词是...
企业晚会的主持词 企业晚会的主持词  借鉴诗词和散文诗是主持词的一种写作手法。在人们越来越多的参与各种活动的今天,主持...
年终总结会主持词 2021年终总结会主持词范文(精选13篇)  契合现场环境的主持词能给集会带来双倍的效果。现今社会在...
半台词爆笑 三句半台词大全爆笑  三句半是一种中国民间群众传统曲艺表演形式,下面是为带大家整理的爆笑的'三句半台...
三八妇女节活动主持词 三八妇女节活动主持词3篇  三月的春风拂过我们脚下的土地,三月的惊雷敲响了我们奋进的汽笛,三月我们迎...
文艺晚会主持人主持词 文艺晚会主持人主持词(精选10篇)  主持词是各种演出活动和集会中主持人串联节目的串联词。在当下这个...
校园文艺晚会结束语 下面文艺晚会结束语是小编为你们寻找的,希望你们会喜欢喔文艺晚会结束语一女1:最明快的,莫过于一年一度...
红色经典诵读主持词 红色经典诵读主持词红色经典诵读主持词尊敬的各位领导、敬爱的老师、亲爱的同学们 :大家好!甲:今天的阳...
答谢会主持词 答谢会主持词15篇  主持词要根据活动对象的不同去设置不同的主持词。随着中国在不断地进步,主持人在活...
年会游戏主持词 年会游戏主持词  主持词没有固定的格式,他的最大特点就是富有个性。在人们积极参与各种活动的今天,主持...
《我是女王》经典台词及剧情介... 《我是女王》经典台词及剧情介绍  一、经典台词  一个偶尔会消失的男人,总有一天会永远的消失。  女...
追梦的主持串词 关于追梦的主持串词  篇一:梦想串词  各位老师,大家好:  又到了一个追梦的季节。春之漫妙、夏之热...
生日宴会精彩致辞 生日宴会精彩致辞(精选5篇)  在日常学习、工作抑或是生活中,大家都不可避免地会接触到致辞吧,致辞是...
暨迎元旦合唱比赛主持词 暨迎元旦合唱比赛主持词  主持词没有固定的格式,他的最大特点就是富有个性。在当下这个社会中,很多场合...
六一主持词开场白和结束语 六一主持词开场白和结束语(精选9篇)  主持词是各种演出活动和集会中主持人串联节目的串联词。在如今这...
国学大讲堂主持词 国学大讲堂主持词  (开场语)同学们老师们:  大家下午好!在各方的积极努力下,国学大讲堂终于以全新...
文艺汇演主持词优秀 文艺汇演主持词优秀  主持词要注意活动对象,针对活动对象写相应的主持词。在当下这个社会中,主持人在活...
《老爸快跑》里的经典台词 《老爸快跑》里的经典台词  《老爸快跑》是由高一功执导,张云宵编剧,徐峥、伊春德主演的电视剧,于20...