What happened
What happens logically after the first assignment is this:
No matter what you used new Integer(1)
or simply 1
, the result is the same.
When you called the method muda
, what happens is this:
And finally, when the second assignment occurs, we have a new value for n
:
When the method muda
ends, the variable n
and the value 3
are discarded and the state remains the same as the first image.
What happens to an array
When using an array, you are actually referencing an object that contains several values:
By passing it on to the method muda
, you will have two variables pointing to the array:
When changing an element, the two variables remain pointing to the same array, but the internal state of the array is affected, changing one of its values.
Finally, after execution of the method, the variable k
remains intact and unchanged, however, the internal state of the object it refers to was affected:
Using a wrapper object
Danne’s response tells how you can use an object whose internal value can be affected.
I’m not going to draw this because it would be exactly the same sequence using the vector.
In fact, many people use vectors or maps as an easy way to modify parameters, but this is not very common or good practice.
Considerations
Immutable objects such as Integer
and String
and references that cannot be affected in the methods (such as C pointers) exist for a very simple reason: maintain the mental health of the developers.
I say this because although it leaves the language more restricted, it also avoids numerous problems where values are unexpectedly modified by obscure methods. In a language that claims to be easy and universal, this is a small price to pay.
Also, the basic solution for this case is extremely simple. Just return the modified object:
public static void main (String[] args) {
Integer k = 1;
Integer k1 = incrementa2(k);
System.out.println(k1);
}
public static Integer incrementa2(Integer n) {
return n + 2;
}
I did not understand why a change in the variable "n" will result in no longer referencing the same memory location that the variable "k" refers to. When I use an array, for example, and make changes to it, these changes happen in the same memory location and so I get the modified array in main().
– Vinicius
Why are you not changing the variable
n
, and create a new instance of it. Whenever you use the=
to define a value for a variable, you are creating a new instance of it. This instance is only valid for the current scope. Normally we change arrays in another method by changing some position of it, which is allowed because we are using the same instance, but if you try to define a new array using the=
, you will see that it will remain unchanged when you exit the method in which you defined this new array.– dnr1
I performed a quick test to check. I created an integer array (
int[]
) and populated with 20 values. After that, I passed this vector as a parameter to a method and changed the value of index 19 in this way:inteiros[19] = valor;
, what works. Soon after, I changed this method and created a new vector that way:inteiros = new int[20];
, and in this case the vector remained unchanged upon returning to the methodmain
.– dnr1