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

自己建设房源网站百度一下你就知道官网百度

自己建设房源网站,百度一下你就知道官网百度,苏州展示型网站建设,app界面生成器文章目录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://www.qdjiajiao.com/news/8032.html

相关文章:

  • 网站打不开被拦截怎么办博客网站seo
  • vs进行网站建设百度权重高的网站有哪些
  • 电商网站开发服务器网站优化系统
  • 门户网站系统业务流程图网页设计流程步骤
  • 怎么给新网站做推广津seo快速排名
  • 博彩网站开发逻辑google应用商店
  • 上海市政网网址合肥百度搜索排名优化
  • 半成品个人在家加工焦作关键词优化排名
  • 国家林业建设工程协会网站整站排名优化品牌
  • 国企网站建设站长是什么级别
  • 网站建设合同书(范本)手机怎么制作网站
  • 聊城seo整站优化报价电商网站订烟
  • 提供手机网站建设天天seo伪原创工具
  • vs2010网站制作教程国内最好的搜索引擎
  • 用旧手机做网站谷歌seo博客
  • 网站制作费一般多少2345浏览器网址
  • 互联网服务平台生成二维码百度关键词优化手段
  • 设计的有趣的网站推荐网络推广文案怎么写
  • 中国建设网站的公司百度关键词seo年度费用
  • 企业网站推广解决方案web前端培训费用大概多少
  • 舞蹈网站模板今日热点新闻头条国内
  • 网站程序开发要点营销推广策略
  • 卢松松网站的百度广告怎么做的推广网站推广
  • 官方网站下载拼多多app网站排名优化专业定制
  • 做网站好的网站建设公司百度投诉平台在哪里投诉
  • java 做视频网站实例网站建设与维护
  • 松原网站制作北京推广服务
  • 苹果官网首页刷移动关键词优化
  • 公司建立网站seo学堂
  • 检查网站打开速度线上职业技能培训平台