首页 > 其他 > 详细

包装类对象之间值的比较, 使用equals 方法

时间:2020-07-19 14:52:22      阅读:43      评论:0      收藏:0      [点我收藏+]
public class Demo01 {
    public static void main(String[] args) {
        //OOP 规约 7. 所有的相同类型的包装类对象之间值的比较,全部使用 equals 方法比较。
        Integer a = 12;
        Integer b = 12;
        System.out.println(a == b); // 在-128 至 127 之间的赋值,Integer 对象是在IntegerCache.cache 产生,会复用已有对象
        Integer c = 12334;
        Integer d = 12334;
        System.out.println(c == d); // 错误
        System.out.println(c.equals(d)); // 正确
    }

}
/*
result:
true
false
true
* */

包装类对象之间值的比较, 使用equals 方法

原文:https://www.cnblogs.com/lixyuan/p/13338924.html

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