打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
(2)eques(==)比较

String s1 = null;
String s2 = null;
System.out.println(s1==s2);//true
//System.out.println(s1.equals(s2));//NullPointerException
s1 = s2;
System.out.println(s1==s2);//true
//System.out.println(s1.equals(s2));//NullPointerException
System.out.println("***1***");

s1 = null;
s2 = "";
System.out.println(s1==s2);//false
//System.out.println(s1.equals(s2));//NullPointerException
s1 = s2;
System.out.println(s1==s2);//true
System.out.println(s1.equals(s2));//true
System.out.println("***2***");

s1 = "";
s2 = null;
System.out.println(s1==s2);//false
System.out.println(s1.equals(s2));//false
s1 = s2;
System.out.println(s1==s2);//true
//System.out.println(s1.equals(s2));//NullPointerException
System.out.println("***3***");

s1 = "";
s2 = "";
System.out.println(s1==s2);//true
System.out.println(s1.equals(s2));//true
s1 = s2;
System.out.println(s1==s2);//true
System.out.println(s1.equals(s2));//true
System.out.println("***4***");
s1 = new String("");
s2 = new String("");
System.out.println(s1==s2);//false
System.out.println(s1.equals(s2));//true
s1 = s2;
System.out.println(s1==s2);//true
System.out.println(s1.equals(s2));//true
System.out.println("***5***");

s1 = "null";
s2 = "null";
System.out.println(s1==s2);//ture
System.out.println(s1.equals(s2));//true
s1 = s2;
System.out.println(s1==s2);//true
System.out.println(s1.equals(s2));//true
System.out.println("***6***");
s1 = new String("null");
s2 = new String("null");
System.out.println(s1==s2);//flase
System.out.println(s1.equals(s2));//true
s1 = s2;
System.out.println(s1==s2);//true
System.out.println(s1.equals(s2));//true
System.out.println("***7***");

s1 = "abc";
s2 = "abc";
System.out.println(s1==s2);//ture
System.out.println(s1.equals(s2));//true
s1 = s2;
System.out.println(s1==s2);//true
System.out.println(s1.equals(s2));//true
System.out.println("***8***");
s1 = new String("abc");
s2 = new String("abc");
System.out.println(s1==s2);//false
System.out.println(s1.equals(s2));//true
s1 = s2;
System.out.println(s1==s2);//true
System.out.println(s1.equals(s2));//true
System.out.println("***9***");
 

总结:

   凡是比较基本类型只能用==,比较结果你看到的字面值相等就会相等,

什么叫基本类型? byte,short,int,long,char,float,double这几种就是基本类型.基本类型不是对象所以不存在用equals比较.
   凡是比较引用类型(对象),==比较的是两个引用是不是指向同一个内存地址,equals比较的是两个引用的字面值是不是相同,什么叫字面值?你能看到的值就是字面值,比如:String s="abc"; abc就是字面值.
   基本类型的包装类型是对象类型,所以除非两个相同字面值的引用指向同一内存地址,值才会相等,比如:
Integer a=new Integer(3);Integer b=a;//互相赋值这样用等号或equals比较都会得出true,记住一点,凡是两个引用都用了new去初始化,那==比较的结果都只会是false,互相之间有了赋值,比较结果就为true.
比较不好理解的一点:
       String a="abc";
       String b="abc";
a==b和a.equals(b);结果都会为true,因为没有用new去新建对象,其实a和b都指向的是同一个String对象"abc",改成:
       String a=new String("abc"); 
       String b=new String"abc");
后==的结果就是false了.总之参照上面的说明,不难理解.

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
java中equal和==的比较
javaSE中的==和equals的联系与区别
java中String str=""与new String()的区别
面试题:Integer的数据直接赋值
Java面试基础:Q1. ==和equals()的区别
java中equals和equalsignorecase的区别
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服