int 和 Integer 对比

1、两个数进行比较,只要有一个 int 类型的,值相等(不管范围是多少)就为 true
2、两个 Integer 比较(不是 new 出来的),只要超过 -128~127 就会是 false
3、两个 Integer 比较,只要有一个是 new 出来的,不管怎样都是 false

public class TestInteger {
    /**
     * 1、两个数进行比较,只要有一个 int 类型的,值相等(不管范围是多少)就为 true
     * 2、两个 Integer 比较(不是 new 出来的),只要超过 -128~127 就会是 false
     * 3、两个 Integer 比较,只要有一个是 new 出来的,不管怎样都是 false
     */
    public static void main(String[] args) {
        int a = 1277;
        int b = 1277;
        //true  ====》只要是两个 int 型的进行比较,值相等就一定会相等
        System.out.println(a == b);

        int c = 127;
        Integer d = 127;
        //true  ====》只要是两个数有一个 int 类型的,值相等就一定会相等
        System.out.println(c == d);


        int e = 1277;
        Integer f = new Integer(1277);
        //true
        System.out.println(e == f);

        System.out.println("============================================================");


        Integer aa = 127;
        Integer bb = 127;
        //true
        System.out.println(aa == bb);

        Integer cc = 128;
        Integer dd = 128;
        //false ====> 两个 Integer 比较(不是 new 出来的),只要超过 -128~127 就会是 false
        System.out.println(cc == dd);

        Integer ee = new Integer(1);
        Integer ff = new Integer(1);
        //false ====> 两个 Integer 比较(是 new 出来的),不管怎样都是 false
        System.out.println(ee == ff);

        System.out.println("============================================================");



        Integer aaa = 1;
        Integer bbb = new Integer(1);
        //false ====> 两个 Integer 比较,只要有一个是 new 出来的,不管怎样都是 false
        System.out.println(aaa == bbb);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值