您正在代码中进行隐式类型转换(自动装箱)。
该行:
a[0]=138;
实际上翻译为:
a[0] = Integer.valueOf(138);
创建一个整数的实例。问题是此方法缓存从 -128 到 127的整数(整数缓存有多大?)并为高于 127 的值创建新实例,因此 == 比较返回 false。
/**
* Returns an {@code Integer} instance representing the specified
* {@code int} value. If a new {@code Integer} instance is not
* required, this method should generally be used in preference to
* the constructor {@link #Integer(int)}, as this method is likely
* to yield significantly better space and time performance by
* caching frequently requested values.
*
* This method will always cache values in the range -128 to 127,
* inclusive, and may cache other values outside of this range.
*
* @param i an {@code int} value.
* @return an {@code Integer} instance representing {@code i}.
* @since 1.5
*/
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
注意 a[0] 的实际类型是整数,所以你可以写
c=a[0].equals(b[0]);
这将返回true。