首页 > 其他 > 详细

equals与==的区分

时间:2018-03-10 10:52:12      阅读:183      评论:0      收藏:0      [点我收藏+]

equals与==的区分

对于比较数值

public class Test {
public static void main(String[] args){
    int a=30;
    int b=30;
    System.out.println("hellow world!");
    System.out.println(a==b);
    }
}

运行结果:
hellow world!
true

对于比较字符串时,

==比较的是地址,其内容分别保存在了不同的空间,所以即使内容相等,但是地址的值是不相等的。

public class Test {
public static void main(String[] args){
    String a="hellow";
    String b=new String("hellow");
    String c=b;
    //System.out.println("hellow world!");
    System.out.println(a==b);
    System.out.println(a==c);
    System.out.println(b==c);
    }
}
运行结果:
false
false
true

而equals只是比较的是字符串内容而不是地址,但是这里涉及到数据库char和varcha的区别,空格equals是能识别出来的。

public class Test {
public static void main(String[] args){
    String a="hellow";
    String b=new String("hellow");
    String c=b;
    //System.out.println("hellow world!");
    System.out.println(a.equals(b));
    System.out.println(a.equals(c));
    System.out.println(b.equals(c));
    }
}
运行结果:
true
true    
true

equals与==的区分

原文:https://www.cnblogs.com/renxiuxing/p/8537379.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!