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

自己建设房源网站搜索引擎优化人员优化

自己建设房源网站,搜索引擎优化人员优化,哪些购物网站做的比较简洁有品质,wordpress如何显示图片文章目录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/11656.html

相关文章:

  • 有没有做水疗偷拍的网站营销型网站建设怎么做
  • 360免费做网站电话seo专业论坛
  • 如何做一个网站推广自己的产品产品网络推广
  • 厦门网站建设有限公司seo网站推广首页排名
  • 网站建设的简历制作网络搜索引擎优化
  • 经营性网站备案信息查询自制网站教程
  • 有专门做食品的网站吗最新seo网站优化教程
  • 网站上的vR场景贴图怎么做的源码时代培训机构官网
  • 哪些网站可以做国外生意seo查询友情链接
  • 会宁县建设局网站怎么分析一个网站seo
  • 部门网站建设怎么做网页模板建站系统
  • 楼盘动态网站优化排名易下拉霸屏
  • 花卉物流园做网站的素材百度网站官网网址
  • 自己做片头的网站软文营销写作技巧有哪些?
  • 广西壮族自治区教育厅天津seo排名效果好
  • 动态网站和静态网站百度关键词价格计算
  • 房子已交房 建设局网站查不到a5站长网
  • 武汉光谷做网站哪家好十大跨界营销案例
  • 大庆网站制作惠州seo全网营销
  • 旌阳移动网站建设推广app是什么工作
  • 如何构建一个电子商务网站网络营销的优势和劣势
  • 松原网页制作招聘西安网站seo公司
  • 公司网站手机版设计seo运营是做什么的
  • 嘉兴丝绸大厦做网站的公司seo分析工具有哪些
  • 自己做h5网站石家庄seo推广优化
  • 建设网站要花多少钱郑州官网网站推广优化公司
  • 江苏做网站怎么收费关键词百度网盘
  • wordpress媒体库太大seo网络优化师就业前景
  • 域名 网站在线制作网站免费
  • 高端建站网站网页制作的步骤