【发布时间】:2015-07-01 08:19:06
【问题描述】:
以下代码为除-128 和127 之间的所有值返回false。这有什么特别的原因吗?我知道我必须使用equals,因为peek() 返回对该对象的引用,但我很想知道为什么它只适用于上述值范围。
public boolean test(int x) {
Stack<Integer> s1 = new Stack<Integer>();
Stack<Integer> s2 = new Stack<Integer>();
s1.push(x);
s2.push(x);
return (s1.peek() == s2.peek());
}
【问题讨论】:
-
Integer缓存 -128 到 127 之间的值以提高性能。