2016-03-02 101 views

回答

0

与原始int相反,整数是一个对象。您的比较是比较Integer类型的两个对象。我有点惊讶,你会得到“错误的真实”。如果你想尝试:

System.out.println(i1.intValue() == i2.intValue()); 
System.out.println(i3.intValue() == i4.intValue()); 

你应该得到预期的结果。

+0

你也应该阅读:http://stackoverflow.com/questions/1700081/why-does-128-128-return-false-but-127-127-return-true-when-converting-to-整合 – Eashi

+0

这不回答这个问题。 – erickson

2

Integer实例的一个值的范围(至少-128 – 127)的高速缓存,当隐式转换的intInteger时使用。

在这种情况下,128不在缓存中,因此代表该值的每个Integer对象都是新的且不同的。

另一方面,值127,保证在缓存中,因此重复获得Integer的同一个实例。

相关问题