java integer equals ==_比较两个Integer,为什么必须使用equals?不用==

equals和==的区别对于基本类型,比如int、long,进行判等,只能使用==,比较的是直接值,因为基本类型的值 就是其数值。

对应用类型,比如Integer、Long和String,进行判等,需要使用equals进行内容判等。因为引用类型的直接值是指针,使用==的话,比较的是指针,也就是两个对象在内存中的地址,即比较它们是不是同一个对象,而不是比较对象的内容。

总结:比较值的内容,除了基本类型只能使用==外,其他类型都需要使用equals。

测试 Integer用例

1、使用==对两个值127的直接赋值的Integer对象判断

Integer a = 127;

Integer b = 127;

System.out.println(a == b);//true

为什么结果是true呢?

编译器会把Integer a = 127转换为Integer.valudOf(127)。查看源码得知:

这个转换在内部其实做了缓存,是的两个Integer指向同一个对象,所以 == 返回true.

public static Integer valueOf(int i) {

if (i >= IntegerCache.low && i <= IntegerCache.high)

return IntegerCache.cache[i + (-IntegerCache.low)];

return new Integer(i);

}

其中 IntegerCache.low = static final int low = -128;

IntegerCache.high = 127

2、使用 == 对两个值 为128的直接赋值的Integer对象判等

Integer c = 128;

Integer d = 128;

System.out.println(c==d); //false

为什么会返回false呢?

默认情况下会缓存[-128,127]的数值,而128处于这个区间之外。

那么怎样让Integer缓存【-128,1000】呢?

可以设置JVM参数:-XX:AutoBoxCacheMax=1000

private static class IntegerCache {

static final int low = -128;

static final int high;

static final Integer cache[];

static {

// high value may be configured by property

int h = 127;

String integerCacheHighPropValue =

sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");

if (integerCacheHighPropValue != null) {

try {

int i = parseInt(integerCacheHighPropValue);

i = Math.max(i, 127);

// Maximum array size is Integer.MAX_VALUE

h = Math.min(i, Integer.MAX_VALUE - (-low) -1);

} catch( NumberFormatException nfe) {

// If the property cannot be parsed into an int, ignore it.

}

}

high = h;

cache = new Integer[(high - low) + 1];

int j = low;

for(int k = 0; k < cache.length; k++)

cache[k] = new Integer(j++);

// range [-128, 127] must be interned (JLS7 5.1.7)

assert IntegerCache.high >= 127;

}

private IntegerCache() {}

}

3、使用==对一个127的直接赋值的Integer 和 另一个通过new Integer声明的值为127的对象判等

Integer e = 127;

Integer f = new Integer(127);

System.out.println(e == f);//false

结果为false,为什么呢?

new 出来的Integer始终是不走缓存的新对象。比较一个新对象和一个来自缓存的对象,结果肯定不一样,因此返回false;

4、使用 == 对两个通过new Integer声明的 值为127的对象判等

Integer g = new Integer(127);

Integer h = new Integer(127);

System.out.println(g == h);//false

结果为false,为什么呢?

new 出来的Integer始终是不走缓存的新对象。比较两个新对象,结果肯定不一样,因此返回false;

5、使用 == 对一个值为128的直接赋值的Integer对象和另一个职位128的int基本类型判等

Integer i = 128;

int j = 128;

System.out.println(i == j);//true

结果为true,为什么呢?

Integer i = 128;是先拆箱再比较,比较的肯定是数值而不是引用,因此返回true。

总结:

只需要记得比较Integer的值请使用equals,而不是==,对于基本类型的int的比较当然只能使用==

Integer的误用

比如一个请假申请的状态,有这么一个枚举定义了请假的状态和对于状态的描述

public enum ProcessEnum {

POST(1,"已提交"),AGREE(2,"同意"),REJECT(3,"拒绝");

private Integer status;

private String desc;

ProcessEnum( Integer status, String desc ) { this.status = status;

this.desc = desc;

}}

在业务代码中,使用了 == 对枚举和入参ProcessQuery中的status属性进行判等;

public void compare(Integer status){

ProcessEnum processEnum = ProcessEnum.AGREE; System.out.println(processEnum.status == status);

}

因为枚举和入参中的status都是包装 类型,所以通过==判等肯定是有问题;

作者:码农的一天

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值