I have very simple code
public static void main(String[] args) {
Integer num = new Integer(1);
Integer num1 = num;
num++;
System.out.println(num);
System.out.println(num1);
}
}
I expected both num and num1 value as 2 as num points to the same object as num1, but surprisingly the value of num is 2 and num1 is 1. The increment operator is creating a new object instance and assigning it to num reference.
Any reasons for it?