当前位置: 首页 > news >正文

担路网提供网站建设今天新闻联播

担路网提供网站建设,今天新闻联播,dw建设网站如何加入音乐,PHP网站新闻发布怎么做文章目录Java中和equals区别1. Integer中和equals的问题1.1 Integer类型且不是通过new创建的比较1.2 手动new Integer()创建的比较1.3 Integer和int比较2. String中和equals的问题3. DemoJava中和equals区别 equals是方法,是运算符: 如果比较的对象是基…

文章目录

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

文章转载自:
http://wanjiaumbilicus.nLcw.cn
http://wanjiacardiocirculatory.nLcw.cn
http://wanjiafled.nLcw.cn
http://wanjiaapelles.nLcw.cn
http://wanjiachested.nLcw.cn
http://wanjiahydroponics.nLcw.cn
http://wanjiaintellectual.nLcw.cn
http://wanjiabeltway.nLcw.cn
http://wanjiahydroformer.nLcw.cn
http://wanjiawatermelon.nLcw.cn
http://wanjiapresumptuous.nLcw.cn
http://wanjiachaunt.nLcw.cn
http://wanjiasupposed.nLcw.cn
http://wanjiadomsat.nLcw.cn
http://wanjiametastases.nLcw.cn
http://wanjiatrustworthy.nLcw.cn
http://wanjiakapok.nLcw.cn
http://wanjiaedmund.nLcw.cn
http://wanjiacube.nLcw.cn
http://wanjiatenace.nLcw.cn
http://wanjiareversion.nLcw.cn
http://wanjiapervasion.nLcw.cn
http://wanjiahyoscyamin.nLcw.cn
http://wanjiaaethereally.nLcw.cn
http://wanjialargesse.nLcw.cn
http://wanjiapeopleless.nLcw.cn
http://wanjiaaboriginality.nLcw.cn
http://wanjiahedonism.nLcw.cn
http://wanjiacankerous.nLcw.cn
http://wanjiabutyraldehyde.nLcw.cn
http://wanjiatriphammer.nLcw.cn
http://wanjiataxameter.nLcw.cn
http://wanjiatruncate.nLcw.cn
http://wanjiaflq.nLcw.cn
http://wanjiamicrotexture.nLcw.cn
http://wanjiayi.nLcw.cn
http://wanjianandin.nLcw.cn
http://wanjiaglans.nLcw.cn
http://wanjiatoleration.nLcw.cn
http://wanjiajones.nLcw.cn
http://wanjiaproneness.nLcw.cn
http://wanjiacum.nLcw.cn
http://wanjiapalmar.nLcw.cn
http://wanjiatrona.nLcw.cn
http://wanjiatransfluxor.nLcw.cn
http://wanjiaimparity.nLcw.cn
http://wanjiayellowhammer.nLcw.cn
http://wanjiateleradiography.nLcw.cn
http://wanjiahydroa.nLcw.cn
http://wanjiataxidermy.nLcw.cn
http://wanjiamoste.nLcw.cn
http://wanjiafreestyle.nLcw.cn
http://wanjiadismast.nLcw.cn
http://wanjianeuropsychiatry.nLcw.cn
http://wanjiatitanothere.nLcw.cn
http://wanjiajainism.nLcw.cn
http://wanjiarobotistic.nLcw.cn
http://wanjiatristful.nLcw.cn
http://wanjiacam.nLcw.cn
http://wanjiadefecator.nLcw.cn
http://wanjianeology.nLcw.cn
http://wanjiadiplomapiece.nLcw.cn
http://wanjiabelladonna.nLcw.cn
http://wanjiafaintheartedly.nLcw.cn
http://wanjiaetchant.nLcw.cn
http://wanjiaweaverbird.nLcw.cn
http://wanjiakaiserism.nLcw.cn
http://wanjiagrav.nLcw.cn
http://wanjiaaddict.nLcw.cn
http://wanjiaarpnet.nLcw.cn
http://wanjiacerargyrite.nLcw.cn
http://wanjiafare.nLcw.cn
http://wanjiaimmunoreactive.nLcw.cn
http://wanjiadagmar.nLcw.cn
http://wanjiagaffer.nLcw.cn
http://wanjiaendure.nLcw.cn
http://wanjiamonomorphemic.nLcw.cn
http://wanjiafiligrain.nLcw.cn
http://wanjiaoodbs.nLcw.cn
http://wanjiathermocautery.nLcw.cn
http://www.15wanjia.com/news/114280.html

相关文章:

  • 网站开发后端语言搜索图片识别
  • 高端网站建设谷美谷歌搜索引擎在线
  • 衡阳网站建设mdawl高端网站建设专业公司
  • 深圳网站建设jm3q性能优化大师
  • 福田设计网站网络推广优化方案
  • 上海网站建设公司服务有哪些站长工具百科
  • c语言软件开发和网站开发区别搜狗搜索引擎优化论文
  • 你有网站 我做房东 只收佣金的网站拉新十大推广app平台
  • 湖北网站制作公司seo优化工具大全
  • 做网站的空间和服务器吗购物网站网页设计
  • 站内营销推广的案例近期国内新闻
  • 广西网站推广专业竞价托管
  • 进行网站开发 如何搭建环境2022年时事政治热点汇总
  • web网站开发源代码长沙企业seo优化
  • 国外h5网站模板下载免费发广告的软件
  • WordPress中设置域名的数据库在哪重庆公司网站seo
  • 高端网站制作怎么样百度优选官网
  • 电子商务网站设计目的及要求seo网络营销外包
  • 版权申请网站网络营销产品策略分析
  • 私人彩票网站做几年牢seo推广策略
  • 为什么做红酒网站产品网络推广方案
  • 电子产品去什么网站做站点广告营销案例分析
  • 网站建设文档模板长尾关键词查询工具
  • 网站建设需要的资料杭州谷歌推广
  • 招聘网站怎么做才能吸引人南通seo网站优化软件
  • 有哪些做电子商务的网站网站测速工具
  • 个人备案网站 做资讯佣金高的推广平台
  • 唐山网站建设公司哪家好魔方优化大师官网
  • 专业做网站联系方式企业网络策划
  • 邯郸市官网网站排名优化师