• Post Reply Bookmark Topic Watch Topic
  • New Topic

simple question  RSS feed

 
Joel Oslow
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int[] ref1 = {2, 5, 4};
int[] ref2 = {3, 2, 1};
ref2[1]++;
ref2 = ref1;
ref2[2] += ref1[0];
System.out.print(Arrays.toString(ref1));

Ok why is the output [2,5,6] rather than [2,5,4]? It doesn't seem to me that you are changing the value of the ref1 array.
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I am not convinced “simple question” accurately tells us what the discussion is about. And you should always tell us where the question comes from, to avoid possible copyright problems.

That sort of question is often designed to confuse the reader. What you should do is write the contents of the two arrays with a soft pencil. Also write the two references and which array they point to, possibly with arrows. Erase or change any values as appropriate. Go through the individual steps and you can see how the values in the array change.

An array is a mutable reference type. Even if you do not change the reference, you can always change the contents of the array.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's look at how this works.

First of all, you have to understand that variables of non-primitive types in java are references. They point to an object somewhere in memory. Variables of array types, such as ref1 and ref2 in this code, are references to array objects (arrays are objects).

int[] ref1 = {2, 5, 4};
int[] ref2 = {3, 2, 1};

Here, two arrays are created and the variables ref1 and ref2 refer to the two arrays.

ref2[1]++;
ref2 = ref1;

In the first of these two lines, you increment the element at index 1 in the second array. But then you make ref2 refer to the same array object as what ref1 refers to. Note that we loose the reference of the array that ref2 initially referred to. The line with the increment has essentially no effect, since we forget about that array object that we just modified in the second line.

ref2[2] += ref1[0];
Now we increment the element at index 2 of the array that ref2 refers to, with the value at index 0 of the array that ref1 refers to.

But note that ref2 and ref1 refer to the same array object! So whatever you change through ref2 is also seen when you look at the array through ref1.

Since ref1[0] == 2, the array will now contain {2, 5, 6}.
 
Joel Oslow
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh! Thanks! This mutation only works with non-primitive types.
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It may not work with all reference types; some cannot change their contents after they are instantiated.
You are not reassigning ref1, but you are altering the contents of the object ref1 points to. That code snippet shows the danger of two references pointing to the same object, which I relieve is called an alias. Not to be confused with aliasing in GUI displays of oblique lines.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joel Oslow wrote:Oh! Thanks! This mutation only works with non-primitive types.

Correct. And, as I hope you know, Java arrays are objects.

Indeed, the fact that arrays are mutable is the source of a lot of common (and often difficult to spot) errors. You might find this page on defensive copying worth a read.

Winston
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: . . . You might find this page on defensive copying worth a read.

Winston
Nice link, just not sure about the Hungarian Notation.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!