I am facing trouble with the following piece of code. values and minStack are both stacks initialised, and the top values of both the stacks are same. However, the first code snippet fails to go into the if condition while the latter does. I am unable to debug this.
public void pop() {
System.out.println(values.peek()); // prints -1024
System.out.println(minStack.peek()); // prints -1024
if(values.pop() == minStack.peek()) {
// Condition not fulfilled
minStack.pop();
}
}
However, the following works as expected.
public void pop() {
int valTop = values.pop();
int minTop = minStack.peek();
if(valTop == minTop) {
minStack.pop();
}
}
Please help as I am unable to figure out the reasoning behind it.