The following code returns false
for all values except between -128 and 127. Is there any particular reason for this? I know I have to use equals
because peek()
returns a reference to the object, but I'm curious to know why it works only for the above range of values.
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());
}