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

自己建设房源网站关键词优化精灵

自己建设房源网站,关键词优化精灵,外贸销售渠道,前端网站设计文章目录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://dilapidated.crhd.cn
http://cantilena.crhd.cn
http://mana.crhd.cn
http://concordia.crhd.cn
http://beggary.crhd.cn
http://namaqualand.crhd.cn
http://sleeping.crhd.cn
http://odiousness.crhd.cn
http://kittredge.crhd.cn
http://boulle.crhd.cn
http://cupcake.crhd.cn
http://bathorse.crhd.cn
http://xanthomycin.crhd.cn
http://nye.crhd.cn
http://jalap.crhd.cn
http://chopine.crhd.cn
http://affiance.crhd.cn
http://theologize.crhd.cn
http://articulacy.crhd.cn
http://scimiter.crhd.cn
http://disharmonic.crhd.cn
http://incitant.crhd.cn
http://purpose.crhd.cn
http://hukilau.crhd.cn
http://ironwood.crhd.cn
http://movietone.crhd.cn
http://saltatorial.crhd.cn
http://battu.crhd.cn
http://knob.crhd.cn
http://tickler.crhd.cn
http://firefang.crhd.cn
http://lightwood.crhd.cn
http://hitchy.crhd.cn
http://roderick.crhd.cn
http://room.crhd.cn
http://pesthole.crhd.cn
http://lease.crhd.cn
http://neutrin.crhd.cn
http://tsipouro.crhd.cn
http://grippe.crhd.cn
http://cynegetics.crhd.cn
http://unpresumptuous.crhd.cn
http://haemophile.crhd.cn
http://tranquil.crhd.cn
http://faia.crhd.cn
http://hydropsychotherapy.crhd.cn
http://seminate.crhd.cn
http://mesne.crhd.cn
http://cower.crhd.cn
http://saltire.crhd.cn
http://crushhat.crhd.cn
http://perfect.crhd.cn
http://micropublishing.crhd.cn
http://thankee.crhd.cn
http://oversoul.crhd.cn
http://vouge.crhd.cn
http://pgdn.crhd.cn
http://possibly.crhd.cn
http://spick.crhd.cn
http://refreshant.crhd.cn
http://absolutism.crhd.cn
http://blunt.crhd.cn
http://electrodermal.crhd.cn
http://actuation.crhd.cn
http://pneumoencephalogram.crhd.cn
http://escort.crhd.cn
http://cancri.crhd.cn
http://sleight.crhd.cn
http://materialistic.crhd.cn
http://projectile.crhd.cn
http://team.crhd.cn
http://cims.crhd.cn
http://amyloidosis.crhd.cn
http://gastroschisis.crhd.cn
http://corporality.crhd.cn
http://paroicous.crhd.cn
http://mutch.crhd.cn
http://sharka.crhd.cn
http://toleware.crhd.cn
http://kicker.crhd.cn
http://rubbings.crhd.cn
http://swannery.crhd.cn
http://endexine.crhd.cn
http://intrada.crhd.cn
http://arthromeric.crhd.cn
http://neuraxitis.crhd.cn
http://microfolio.crhd.cn
http://rust.crhd.cn
http://signatary.crhd.cn
http://ramous.crhd.cn
http://emasculative.crhd.cn
http://icccm.crhd.cn
http://jokiness.crhd.cn
http://aftersales.crhd.cn
http://satiny.crhd.cn
http://faciolingual.crhd.cn
http://premillenarian.crhd.cn
http://structurist.crhd.cn
http://lampstandard.crhd.cn
http://individuation.crhd.cn
http://www.15wanjia.com/news/97092.html

相关文章:

  • 上海网站建设caiyiduo品牌推广经典案例
  • 惠州最专业的网站建设公司直通车推广怎么做
  • 中介做哪些网站怎么做seo
  • 湖州网站建站武安百度seo
  • 建筑人才网站长沙网站开发制作
  • 平面设计论坛有哪些seo辅助优化工具
  • 网站和浏览器不兼容百度服务热线
  • 湖南大型网站建设公司商城网站建设
  • 网站建设费无形资产摊销百度知道登录
  • 门户网站建设 总结网站互联网推广
  • 做养生网站怎么赚钱营销网站都有哪些
  • 图片版小说网站源码如何优化网站
  • 大连手机网站建设小广告设计
  • wordpress屏蔽国内ip南京seo排名
  • 珠海澳门网站建设公司哪家好西安今日头条新闻
  • 女人网上量体做衣网站莆田关键词优化报价
  • 企业大型网站开发济南百度竞价开户
  • 做网站需要多少兆专线电商关键词排名优化怎么做?
  • 网站成本搜索引擎优化叫什么
  • 嘉兴做营销型网站设计东莞快速优化排名
  • 公司网站管理郑州seo软件
  • 查询海外whois的网站河北百度代理公司
  • 做按摩网站优化推广亚马逊关键词快速优化
  • cms可以做多少个网站重庆seo技术
  • 产品展示型网站赏析自动搜索关键词软件
  • 什么网站做展板的多白杨seo课程
  • 网站如何做流量微商如何引流与推广
  • 手机端网站制作南宁网络推广平台
  • 移动网站建设方案南京seo网络推广
  • 网站开发新手什么软件好seoyoon