I was trying sample code and some got result which I don't understand:
Map<Integer,Integer> map = new HashMap<>();
map.put(1, 2);
System.out.println(map.get(1));
Integer k = map.get(1);
k++;
System.out.println(map.get(1));
The result:
2
2
But as Integer is an object, the change should get reflected in the map value as well? So why is the value not changing?
Integer(and all other boxed types) is immutable.k++returns a new Integer with the value 3, the original Integer stays 2.