John Lerry wrote:I have two instance variables (representing two arrays of type long), the first variable is called a1 and inside are inserted values {1,2,3}, the second variable will contain the values generated by the invocation of adjust method on the variable a1.
John Lerry wrote:Where am I wrong?
Roel De Nijs wrote:
John Lerry wrote:I have two instance variables (representing two arrays of type long), the first variable is called a1 and inside are inserted values {1,2,3}, the second variable will contain the values generated by the invocation of adjust method on the variable a1.
If this is about the code snippet from your original post, reference variable a1 is a local variable (not an instance variable).
Roel De Nijs wrote:
John Lerry wrote:Where am I wrong?
Let's try 1 step at a time.
What's the output of this little program?
John Lerry wrote:
1 2 3
1 2 4
John Lerry wrote:I thought to row 3 there was the change of the third value in the variable a2 and then the next System.out.println went to print the variable with the changed values.
Roel De Nijs wrote:
John Lerry wrote:I thought to row 3 there was the change of the third value in the variable a2 and then the next System.out.println went to print the variable with the changed values.
Row 3 There is no row 3 at all. You have line 3 (the statement a2[2] = 4;). And in the array you have 3 elements (ranging from index 0 to index 2 inclusive).
Roel De Nijs wrote:
But based on the output you proposed (which was not correct), I think I can see inside your head and I know the reason of your misunderstanding of this concept. You think:
line1: a new array is created with elements 1, 2 and 3 and this array is assigned to reference variable a1. line2: another array is created which is an exact copy of the array a1 is referring to (and thus contains the same 3 elements). we now have 2 arrays. line3: in the array a2 is referring to, the 3rd element (with index 2) is changed to 4 line4: the array a1 is referring to (and which wasn't changed and thus still has original values) is printed --> 1,2,3 line5: the array a2 is referring to (and was changed) is printed --> 1,2,4
Is my understanding of your thoughts correct?
John Lerry wrote:
Roel De Nijs wrote:
But based on the output you proposed (which was not correct), I think I can see inside your head and I know the reason of your misunderstanding of this concept. You think:
line1: a new array is created with elements 1, 2 and 3 and this array is assigned to reference variable a1. line2: another array is created which is an exact copy of the array a1 is referring to (and thus contains the same 3 elements). we now have 2 arrays. line3: in the array a2 is referring to, the 3rd element (with index 2) is changed to 4 line4: the array a1 is referring to (and which wasn't changed and thus still has original values) is printed --> 1,2,3 line5: the array a2 is referring to (and was changed) is printed --> 1,2,4
Is my understanding of your thoughts correct?
Yes, it is exactly the analysis that I would do.
John Lerry wrote:The doubt comes from the fact that I have always in mind the fact that, because Java pass ALWAYS by value
John Lerry wrote:I understand the concept.
Fantastic explanation.
Guillermo Ishi wrote:Primitive wrappers are still treated is primitives although they are objects. Is it just something to memorize, or is there more to it?
Integer a = new Integer(0):
Integer b = a;
b++;
//a is still 0
Consider Paul's rocket mass heater. |