Maybe a stupid question but I can't see the answer. Let's say we have this simple example:
Integer a = 10;
a+=4;
From what I have read and tried behind the scenes is happening something like this:
Integer a = 10;
a = new Integer (a+4);
- Why does Java need to create a new instance and then override the previous reference value with a new one?
- Does Integer (and other reference types variables (Byte, Character)) follow the same principle as Strings?
- Is not this a performance issue if you want to change the value of a variable and you expect to keep the current reference and to update only the value and what you get is a brand new object?
- Reflect the image below (in big lines) reflect what happens?