How to explain a shallow copy in Java compared to passing by value?

From what I understand with Java, everything is passed by value. However, when you start talking about objects that contain other objects, everything becomes a little interesting, and I would like to be able to report it better and make sure that I fully understand. Therefore, I believe that only the primitives inside the object are actually copied, on the other hand, any objects that it contains ... the values โ€‹โ€‹of these objects are not copied, but are only copies of these links.

So, if we had some kind of object with these members

public class SomeObject { private String name; private String address; private List<String> friends; public SomeObject(String name, String address, List<String> friends) { this.name = name; this.address = address; this.friends = friends; } } 

And we did it:

 List<String> friendsList = new ArrayList<String>(); friendsList.add("bob"); friendsList.add("joe"); SomeObject so1 = new SomeObject("mike", "123 street", friendsList); friendsList.add("zoe"); friendsList.add("rick"); SomeObject so2 = new SomeObject("david", "300 ave", friendsList); System.out.println(so1.getName() + " " + so1.getAddress()); for (String friend : so1.getFriends()) { System.out.println(friend); } System.out.println(so2.getName() + " " + so2.getAddress()); for (String friend : so2.getFriends()) { System.out.println(friend); } 

Our conclusion is this:

 mike 123 street bob joe zoe rick david 300 ave bob joe zoe rick 

Despite the fact that we created 2 separate objects, they both maintain a link to the original list of friends. That's why I worked a bit when people say that Java always passes by value.

+4
source share
10 answers

Java always passes parameters by value. In your case, an object reference is passed by value. See this article for how and why.

http://javadude.com/articles/passbyvalue.htm

+3
source

The link is passed by value, so the pointer to the list is copied, not the list itself.

+10
source

The link to the list is passed by value, not by the list itself, that is, you cannot change friendsList inside the SomeObject constructor, but you can change its contents.

For example, if the constructor was like this:

 public SomeObject(String name, String address, List<String> friends) { this.name = name; this.address = address; this.friends = friends; friends = new ArrayList<String>(); } 

You will still get the same result, although you have assigned a new friends list.

+6
source

I heard that the Java memory model is described as passing a value by reference. Any "primitive" types (int, for example) are passed by value. Everything that extends the object is also passed in the value ... but the value of the object is a reference to its location on the heap .

+1
source

Java is passed by value. The fact is that what you called the object is actually a reference to the object. The value of which was transmitted.

+1
source

Everything is passed by value, but the object itself is NEVER transmitted. Only its link is passed by value.

Think about the objects stored in the repository, for each object you have an identifier that indicates where the object is located. So, now when you pass the object from the method to another, you just pass the identifier.

+1
source

What is passed by value is a link to the list, not the list itself. If you know a little C ++, this is like copying a list pointer. 2 objects have two different links that link to the SAME list. This is an explanation of this conclusion.

+1
source

The following varInt contains the value "2"

 int varInt = 2; 

And varObj in the following cases: value ", which is a reference to a new object:

 Object varObj = new Object(); 

So, if you call methods as follows -

 someVar.method1(varInt); // the value 2 is passed here someVar.method2(varObj); // the value which is the reference is passed here 

So Java always has a Pass-By-Value.

+1
source

Java does not pass by value. Only primitives are passed by value, objects are passed by reference. Since you are using the same list for both lists of friends, friends are the same.

0
source

Only primitives in Java are passed by value, objects and arrays are referenced.

0
source

All Articles