3

In Java, when we pass a java object argument to method myMethod((MyObject) myObject), it is basically like passing a reference to object in C++ like myFunction(& myObject). In such cases java developer has pretty much the same freedom like C++ programmer, if he wants to pass a reference he just passes an object, if he wants to get it copied, he does it manually in method.

But here is my question:

Let's say i have three different arrays with specific type of objects MyObject

MyObject[] myObjectsOne

MyObject[] myObjectsTwo

MyObject[] myObjectsThree

I need to have them separately, but i also need one alternative array which would be like a summary of those three mentioned above - for example used as single object to display the content of all three.

Scenario: To create the fourth array, which would represent the values of first three without copying the values actually anywhere. (Not allocating extra memory for these values)

In C++ i would just create a table with references to corresponding objects in those three tables.

I have always thought that in Java it is just being made for us, but when i started thinking about it, i am not really sure.

We have now also: MyObject[] myObjectsFour

And after performing..

myObjectsFour[0] = myObjectsOne[0]; 

..does accessing myObjectsFour[0] and myObjectsOne[0] mean actually accessing the same object on memory level? Like i understand, it must be like that, if it was not myObjectsFour[0] = myObjectsOne[0].clone();

14
  • 3
    You would be accessing the same object. Commented Apr 11, 2013 at 15:46
  • 2
    in Java you pass arguments to methods by copies of references, so if you reassign the reference to a different object in the function, the original reference (callers') is not affected. Commented Apr 11, 2013 at 15:49
  • Also references in c++ can refer to stack objects and you need to be careful then. Managed languages keep references to objects on heap and does garbage collection. Commented Apr 11, 2013 at 15:49
  • You answered the quostion yourself, but you could have used the == compare to test it, since == always compares the references of objects :) Commented Apr 11, 2013 at 15:50
  • The best C++ (and C) analogy of Java references I've come up with is that they are like pointers without pointer arithmetic. The C++ concept of references doesn't really exist in Java. Commented Apr 11, 2013 at 15:52

2 Answers 2

5

I think there is a slight misunderstanding from the background text:

In Java, when we pass a java object argument to method myMethod((MyObject) myObject), it is basically like passing a reference to object in C++ like myFunction(& myObject).

Java is not a "pass-by-reference" language. It always passes by value. When you pass an object, you are passing a reference to that object by value. This is analogous in C++ when you pass a pointer by value.

For example, in Java:

public void myMethod(MyObject myObject) { myObject = null; } 

The object passed as "MyObject" is only a reference by value. So setting it to null does not affect the original object that was passed. Correspondingly:

public void myMethod(MyObject myObject) { myObject.myIntProperty = 15; } 

Accessing members of the object, as above, does so to the actual object, since it is still a reference to that object. If it helps, think of the passed in myObject as having a copy of the memory location that is the same as the memory location of the original object that was passed:

public void myMethod(MyObject myObject) {} /* Example memory address: 0x009FC12A in memory, a *copy* of the same address of the object that was passed */ 

With this mindset, let's take a look at your question. What does this yield:

myObjectsFour[0] = myObjectsOne[0]; 

Well, as Java assigns the reference of the element by value, myObjectsFour[0] now points to precisely the same object. This means:

myObjectsFour[0] = myObjectsOne[0]; myObjectsFour[0].myIntProperty = 15; 

Also changes the value of myObjectsOne[0].myIntProperty to be 15, just as above. However doing this:

myObjectsFour[0] = myObjectsOne[0]; myObjectsFour[0] = null; // (or anything else, for that matter) 

has no affect on myObjectsOne[0]. It's not the same variable by the statement, myObjectsFour[0] = myObjectsOne[0];. It's simply two variables that have a copy of the same reference.

Does that make sense?

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

1 Comment

Ofcourse it does, you wrote a decent answer. Thinking of passed object as of "a copy" of the reference to its memory address and not direct reference is vivid and makes very good sense why =null or =new Obj() on passed object won't affect the original one. Pass-by-value it is, but it would be way more clear if it was called pass-by-reference-value (for non-primitives).
1

Yes, you would be accessing the same objects.

As some people in the comments pointed out, Java references are more like C++ pointer than C++ references, just with the difference that you don't have to delete them and that you can't do pointer arithmetic.

When an object is inserted in a Java array or a Java collection, the data structure points to the original object, not a copy.

  • When you create a copy of an array or collection, the objects in it aren't copied. The new data structure points to the same objects.
  • When you change something on an object in an array or collection, it also changes in all other collections the object was added to, because they all point to the same object.
  • When you remove an object from an array or collection, it doesn't get deleted. Other pointers to it stay valid. (it gets deleted eventually by the garbage collector when no other pointers to it exist anymore).
  • When a collection or array gets deleted, the objects it points to will not be affected (unless the garbage collector notices that no other pointers to them exist).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.