mutate array in JAVA

public class example
{
    public static void main(String[] args)
    {
        int[] MyArray = new int[10];
        MyArray[1] = 5;
        int[] NewArray = MyArray;
        NewArray[1] = 10;    
        System.out.println(MyArray[1]);
    }
}

Why does the system print out 10 instead of 5?

It seems like whatever changes we made to element in NewArray, MyArray will change along, why?

I copy this pattern to initiate like int, string variables but they don't behave like this above, why? thanks guy, I am new to CS programming.

Jon Skeet
people
quotationmark

It seems like whatever changes we made to element in NewArray, MyArray will change along, why?

You aren't changing NewArray. You're changing the array that the value of NewArray refers to. It's like this:

  • NewArray is a variable (an unconventionally named one, but hey)
  • The value of NewArray is a reference
  • The reference refers to an object

It's really, really important to differentiate between variables, references and objects. Don't feel worried that it didn't just come to you immediately - this is a stage a lot of people go through when they first encounter a language like Java.

The values of MyArray and NewArray both refer to the same array, so if you make changes to the array via one variable, you still see the change when you then look at the array via a different variable.

I copy this pattern to initiate like int, string variables but they don't behave like this above, why? thanks guy, I am new to CS programming.

For int, that's a value type - the value of an int variable is the number itself, not a reference to an object.

For String, I suspect you were changing the value of the variable to refer to a different String object - not changing the contents of the String object itself (which you can't - strings are immutable in Java). For example:

String x = "first";
String y = x;
x = "second"; // This doesn't change the value of `y`

I like to think of variables as pieces of paper. For value type variables, the value is just written on the piece of paper:

int i = 10;

is like having a piece of paper with the name i, and a value of 10 on it.

For classes and arrays (which are reference types) the value on the piece of paper isn't the object - it's a way of navigating to the object. A bit like having someone's home address written on a piece of paper.

Suppose you have two pieces of paper with the same address written on them. If you rub one of them out and write a different address, that doesn't change anything about what will happen if someone looks at the other piece of paper and goes to that house. That's like the String example above.

The array example, however, is more like this: I write my home address on two pieces of paper, and give them to Fred and Ginger. Fred reads the address, visits my home, and paints the front door red. (He's modifying the object - my house - without changing his piece of paper at all.) Now if Ginger reads the address on his piece of paper and visits my home, he'll see a red front door.

people

See more on this question at Stackoverflow