• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

change array values

 
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a doubt about the change of the values of an array.
Suppose you have this code:



The solution is said that when the method adjust is invoked is changed the value of the array a1.
Why does this happen? At that moment I'm working on a copy of the array?
I would have said that after the invocation of the method the array a1 would have the values {1,2,3} while the array a2 would have the values {1,2,4} but is not so.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Definitely read this great article, especially the part about reference variables.

If you still have doubts after carefully reading this article, don't hesitate to post a reply.
 
John Lerry
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I read the article but I still have doubts.
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.
The method adjust takes as input the variable a1 (a copy of the variable), goes to change one of its values and returns the modified variable (a copy of the variable) whose values will be copied into the variable a2.
If all this is true then the values of a1 will be {1,2,3} (do not change just because a copy of the variable is passed to the method adjust) while the values of a2 will {1,2,4} and instead isn't so because as a result the exercise for both variables the values {1,2,3}.

Where am I wrong?
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).

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
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).



yes, because it is defined within a method.

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?





1 2 3
1 2 4
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Lerry wrote:
1 2 3
1 2 4


Unfortunately that's not correct! Can you explain why you think that should have been the output? It might lead us to the cause of your misunderstanding of this concept.
 
John Lerry
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 16
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Arrays are objects and thus variables that create them have reference values. Therefore when you pass an array to adjust(),
you are passing the reference of the array (the route to take to get to the object in memory).



You at no point during this create more than one Array object. You create three reference variables, but
only one object

You don't need an a2 variable at all in this, try and figure out why this is...

Hope it helps,
Ash
 
John Lerry
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The speech is clear to me, however, I do not understand why the print of a2 (ok, no need) should not return 1,2,4.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).

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
    Ranch Hand
    Posts: 145
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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).



    yes, sure. mistake with the translation.

    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.
     
    Roel De Nijs
    Sheriff
    Posts: 11604
    178
    Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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.


    Your understanding/analysis is wrong! Let me explain how it works.

    In this code snippetonly 1 (one) array is created in total! No 2 arrays, just 1 ("uno" in Italian ) array and 2 reference variables refering to this array.

    This is what's happening line by line
  • line1: a new array is created with elements 1, 2 and 3 and this array is assigned to reference variable a1.
  • line2: the reference variable a1 is assigned to the reference variable a2, so the value of reference variable a1 (which is the memory address of the array) is assigned to the reference variable a2 => you have 1 array and 2 reference variables (a1 and a2) which refer to exactly the same array
  • line3: in the array a2 is referring to (remember: a1 is also referring to this array), the 3rd element (with index 2) is changed to 4
  • line4: the array a1 is referring to (and which was changed by using reference variable a2 because both reference variables refer to exactly the same array) is printed --> 1,2,4
  • line5: the array a2 is referring to (and was changed) is printed --> 1,2,4


  • Hope it helps!
    Kind regards,
    Roel
     
    John Lerry
    Ranch Hand
    Posts: 145
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Now I begin to understand, but I have a doubt.
    If the code was this:



    the speech would be the same?
    or the output would be this?

    1
    4


    The doubt comes from the fact that I have always in mind the fact that, because Java pass ALWAYS by value, a statement like this:


    is to indicate that the variable a2 is given a copy of the variable a1 which therefore is not changed.


     
    Roel De Nijs
    Sheriff
    Posts: 11604
    178
    Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    John Lerry wrote:The doubt comes from the fact that I have always in mind the fact that, because Java pass ALWAYS by value


    That's true, Java always uses pass-by-value! I think you should definitely re-read this excellent article once more. And even 2, 3 or maybe 4 times until you completely understand how the mechanism works. I can't explain it better than in this article, but I give it another try.

    With a primitive you pass the value of this primitive. So in your example, on line2 the value of a1 (which is 1) is assigned to a2. So both values have now 1 as their value. On line3 4 is assigned to a2, so now the value of a2 is 4, but the value of a1 is still 1 (because a copy was passed). So line4 prints 1 and line5 prints 4.

    Now with reference variables pass-by-value is also used. So in this code the value of a1 is copied and assigned to a2. But (and this is very, very, very important and probably the cause of your doubts) the value of a1 is NOT the complete array, it's just the memory address of this array! That's why both a1 and a2 refer to the same array and only 1 array is created.

    That's why both primitive and reference variables use pass-by-value (as it's always in Java), but the value is different: for primitives it's simply the actual value (6, 10.15, 'c' depending on the primitive data type); but for reference variables the value is not the array or the object, it's the memory address of this array or object. So assume the {1,2,3} array is stored on memory address 25145263, then the value of a1 is 25145263 (and not the actual array). When you assign a1 to reference variable a2, the value of a1 (25145263) is copied and assigned to a2. So the value of a2 is 25145263, exactly the same as a1.

    Hope it helps!
    Kind regards,
    Roel
     
    John Lerry
    Ranch Hand
    Posts: 145
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I understand the concept.
    Fantastic explanation.
     
    Ranch Hand
    Posts: 789
    Python C++ Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
     
    Roel De Nijs
    Sheriff
    Posts: 11604
    178
    Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    John Lerry wrote:I understand the concept.
    Fantastic explanation.


    Glad to hear you understand the concept! And thanks for the compliment. Really appreciated!

    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


    Although this is completely true, it's more complicated than just "primitive wrapper objects being treated as primitives". First of all, this code compiles because of the autoboxing feature (as of Java 5). Secondly, primitive wrapper classes are (just like the String class) immutable. So when b++ is executed, there happens a lot behind the scenes, as shown in this code snippet:So as you can see a new primitive wrapper instance is created and assigned to b.
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic