Integer int类型的 == 比较

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wzl1369248650/article/details/84540416
		Integer a = 1;
        int b = 1;
        Integer c = Integer.valueOf(1);
        Integer d = new Integer(1);
        System.out.println(a == b);
        System.out.println(a == c);
        System.out.println(c == b);
        System.out.println(a == d);
        System.out.println(c == d);

结果

true
true
true
false
false

Integer a = 1默认调用Integer.valueOf(1),而Integer.valueOf是有-128-127的缓存的,所以a == c为true;
Integer 和int类型的比较,Integer 默认调用intValue方法获取原始值,所以a == b为true;
Integer d = new Integer(1)是新创建的对象,所以a == d c ==d都为false;

猜你喜欢

转载自blog.csdn.net/wzl1369248650/article/details/84540416