I am having doubt on System.err.print() execution control. The order in which statements are printed is differs for each execution. I do have 2 questions.
Following is the code
public class AutoBoxingProblems {
public static void main(String[] args) {
problem1();
problem2();
}
private static void problem2() {
System.out.println("Problem2");
// Be very careful, Even values are same but two different objects, this will give you equal
Integer i1 = 100;
Integer i2 = 100;
if (i1 == i2) {
System.err.println("i1 and i2 is equal ==> Problem2");
} else {
System.err.println("i1 and i2 is not equal ==> Problem2");
}
}
private static void problem1() {
System.out.println("Problem1");
// Be very careful, Even values are same, this will give you unequal
Integer i1 = 260;
Integer i2 = 260;
if (i1 == i2) {
System.err.println("i1 and i2 is equal ==> Problem1");
} else {
System.err.println("i1 and i2 is not equal ==> Problem1");
}
}
}
//Output
//Some times
Problem1
Problem2
i1 and i2 is not equal ==> Problem1
i1 and i2 is equal ==> Problem2
//Some times
Problem1
i1 and i2 is not equal ==> Problem1
i1 and i2 is equal ==> Problem2
Problem2
Question1: Why in each execution, print statements order differs?
Question2: Why one method prints the values are equal and other method says not equal? (To compare values, we should use 'equals' only. But why '==' operator behaves weird?)