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}
}

相关内容

热门资讯

文艺汇演主持词优秀 文艺汇演主持词优秀  主持词要注意活动对象,针对活动对象写相应的主持词。在当下这个社会中,主持人在活...
《老爸快跑》里的经典台词 《老爸快跑》里的经典台词  《老爸快跑》是由高一功执导,张云宵编剧,徐峥、伊春德主演的电视剧,于20...
公司领导年会致辞 公司领导年会致辞  在日常学习、工作和生活中,大家或多或少都用到过致辞吧,致辞要求风格的雅、俗、庄、...
秋季开学典礼主持词 秋季开学典礼主持词(精选6篇)  主持词已成为各种演出活动和集会中不可或缺的一部分。在一步步向前发展...
当幸福来敲门经典台词 当幸福来敲门经典台词大全  在日新月异的现代社会中,我们都可能会用到台词,台词可以刻画人物的性格,表...
六一儿童节开幕致辞 六一儿童节开幕致辞(通用5篇)  在日常的学习、工作、生活中,大家一定都接触过致辞吧,致辞要求风格的...
春晚主持词 春晚主持词(精选11篇)  主持词要根据活动对象的不同去设置不同的主持词。随着社会一步步向前发展,各...
小学国庆节主题活动主持词 小学国庆节主题活动主持词  主持词是主持人在节目进行过程中用于串联节目的串联词。在当下的社会中,活动...
八年级班会主持词 八年级班会主持词  主持词要注意活动对象,针对活动对象写相应的主持词。在如今这个中国,活动集会越来越...
职工追悼词 职工追悼词 各位亲友、各位来宾:  今天我们怀着十分沉痛的心情深切悼念退休职工×××。  ×××因病...
春天活动主持词 春天活动主持词  大家上午好!  踏着春天的脚步,踩着春风的节拍,春天已经来到我们中间,春天是生命的...
幼儿园家长会园长致辞 幼儿园家长会园长致辞幼儿园家长会园长致辞亲爱的家长、老师们:首先感谢大家在百忙中抽空参加今天举行的家...
教师节活动主持词 教师节活动主持词  一、什么是主持词  由主持人于节目进行过程中串联节目的串联词。如今的各种演出活动...
百日宴致辞 百日宴致辞范文  在日复一日的学习、工作或生活中,许多人都有过写致辞的经历,对致辞都不陌生吧,在各种...
2021年会总经理简短致辞 2021年会总经理简短致辞范文(通用6篇)  在学习、工作、生活中,许多人都有过写致辞的经历,对致辞...
中学秋季开学典礼主持词 中学秋季开学典礼主持词  中学秋季开学典礼主持词    第一项:升国旗仪式(升旗仪式结束后,请新教师...
婚礼男方家长经典致辞 婚礼男方家长经典致辞  大家好!今天是我儿子××和××小姐结婚的大喜日子,我感到非常高兴和荣幸。高兴...
元宵晚会主持词 关于元宵晚会主持词(通用11篇)  主持词是主持人在台上表演的灵魂之所在。在当今社会生活中,司仪等是...
国学经典诵读比赛主持词 国学经典诵读比赛主持词  主持词可以采用和历史文化有关的表述方法去写作以提升活动的文化内涵。随着社会...
离职感谢词 离职感谢词  在xx近两个月的生活,让我感触很多,首先感谢领导一直以来对我们的包容,感谢x经理的照顾...