-1

I've tried this in multiple languages (ex. python and js) and I get the same general result everytime:

int[] arr = {1, 2, 3, 4, 5}; 
int[] other = arr; 
other[4] = 2;
System.out.println("other[4]: " + other[4]);
System.out.println("arr[4]: " + arr[4]);
int x = 7;
int y = x;
y = 888;
System.out.println("x: " + x);
System.out.println("y: " + y);

Why does other change arr when modified, but, like it should, y does not change x when modified?

5
  • 6
    "Why do the arrays change" That's where you're wrong - you only have one array. Commented Apr 10, 2018 at 14:59
  • 1
    Please don't tag-spam, not least because the answers can be different for different languages. As your examples are Java, I've removed the Python tag. Commented Apr 10, 2018 at 14:59
  • 1
    int[] other = arr; doesn't create a second array. It gives you a second variable referring to the same array as the first variable. Any changes you make to the array are naturally visible through both variables, as they both point the same place. Commented Apr 10, 2018 at 14:59
  • What you have here in arr is a pointer to an array, then you set other to be a pointer to the same array. Accessing the array by either pointer will yield the same value. Commented Apr 10, 2018 at 15:00
  • Slightly offtopic, but IMHO, the only real answer would be: go and program in C until you understand what's going on with the memory. Commented Apr 10, 2018 at 15:23

4 Answers 4

1

The main reason for your confusion is that you are mistaking Reference Types and Value types. Take a look at the docs

This is a long discussion, but in a nutshell: integers are primitive types (also called value types e.g. here), so there's a specific space in memory for x and y in your example. arrays are reference types, so there is only one array in memory and two objects pointing at it.

Sign up to request clarification or add additional context in comments.

1 Comment

One array object, two references pointing to it.
1

Java has three types of variable: Primitive type, Object type and null type. One thing needs to clear that Java is always passed by value. Assigning a primitive on another for example int a = b; value of the primitive variable b is assigned to A. In case of assigning of objects it also follows the same rule: passed by value. But in the later case value is reference to the object. Reference variables are not pointers like in C and C++, they are just a handle to object so that one can access them and make some changes on object's state.This is why the values of the array is changed. Any array in Java is an object. java.lang.Object works as supertype in every Java array and thus it inherits all functions in the Object API.

Comments

0

It's because only references are copied, not contents. To make two arrays independent, you should allocate new memory for the destination, and then copy all of the elements.

In short, arr and other are pointing the same memory, because you copied the reference of the memory pointed by arr to other.

Below is a diagram.

Address 0x1000 :  1
Address 0x1004:   2
Address 0x1008:   3
Address 0x100C:   4
Address 0x1010:   5

Address 0x4000(arr): 0x1000
Address 0x4008(other): 0x1000

So arr and other are pointing to the smae address 0x1000, which was allocated firstly when you allocate arr.

x and y just have their primitive values.

0x4020(x): 7
0x4024(y): none

Becomes:

0x4020(x): 7
0x4024(y): 7

After y=x;. And then it becomes:

0x4020(x): 7
0x4024(y): 888

After y=888;

The difference is that array variables in many languages virtually points to the address of the actual data. The reason is: If we copy all of the data whenever we assign an array to another, it will cost a lot of performance hit.

2 Comments

@T.J.Crowder thanks, edited mine.
Why am i downvoted?
0

Basically the data types are different and therefore behave differently.

At least in Javascript, an int value is a primitive data type, while an array is an object.

See this article for better explanation: https://codeburst.io/explaining-value-vs-reference-in-javascript-647a975e12a0

1 Comment

Apparently, the tags have been modified so that it is no longer a question about JavaScript. While a comparison between Java and JS or Python might be helpful, this posting does not answer the question as it is now.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.