2

I recently started refactoring my Java code. It was all running fine until later on, I noticed that some of my objects lost "proper referencing", i.e. it became the case that the objects were "passed by value", and not "passed by reference". Note that I do understand that Java is always pass-by-value and passing by reference is only emulated by memory address passing (which is why I quote the two phrases).

My question is: is there a difference between

Object o = new Object();

and

Object o = makeMeAnObjectPlease();

where

public Object makeMeAnObjectPlease()
{
    Object c = new Object();
    return c;
}

And by difference, I mean will o after Object o = makeMeAnObjectPlease() refer to the same memory address as the one created inside makeMeAnObjectPlease()? And are there any more differences?

brain56
  • 2,659
  • 9
  • 38
  • 70
  • 1
    You might want to check out answers to this question: http://stackoverflow.com/questions/40480/is-java-pass-by-reference – Natix Feb 01 '13 at 01:53

4 Answers4

3

Yes, it will refer to the same object. No, there are no other differences. To take the example further:

Date d = makeMeAnObjectPlease();
System.out.println(d);
Date d2 = d;
changeMyObjectPlease(d2);
System.out.println(d);
System.out.println(d2);

Date makeMeAnObjectPlease() {
    return new Date();
}

void changeMyObjectPlease(Date date) {
    date.setTime(1234);
}

There's exactly one Date object in that code, though it's known by different names at different times. In the end, all the names point at the same object, thus in the end, when we print the object out by two different names, you'll see that they actually indicate the same object in memory, which was modified by the changeMyObjectPlease() method. An example of running the above is:

Thu Jan 31 19:29:29 CST 2013
Wed Dec 31 18:00:01 CST 1969
Wed Dec 31 18:00:01 CST 1969
Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
2

There is no functional difference between the two approaches.

In the first example, a new Object is created and the reference location for the object is stored in the Object reference variable o.

In the second example, the same process occurs for c. The value of c (the memory reference to the created object) is then copied into o. If both c and o were in scope at this point, any mutation done to o (by invoking methods or changing fields) would be reflected in c and vice versa.

To answer your question about how reference work in java, consider the following:

Object o,c;
o = c = new Object();

In this example, both o and c have the same value (a reference to an Object) and point to the same object. I can pass o into a method, mutate the object it points to and witness the changes by accessing the Object from c. However, if I assign a new object into the reference I passed in (like in the following)

public class Test{

    public static void main(String[] args){
        Object o,c;
        o = c = new Object();
        assignIntoTheReferencePlease(c);
        System.out.println(o == c);
    }

    public static void assignIntoTheReferencePlease(Object c){
        c = new Object();
    }
}

nothing happens (and the program prints true).

Why?

This is because what you said is true - all method calls and returns are passed by value. This means that the reference value of c is copied into the method variable c. Any assignments done to that variable will not affect the other variable in the outside scope because it isn't a true pointer.

I hope I answered your question.

Jesan Fafon
  • 2,234
  • 1
  • 25
  • 29
1

As others have said, as you show it, there is no difference at all.The only difference comes in what might be.

If, for instance you didnt know/control the definition makeMeAnObjectPlease, It could return any subclass of Object, and the code will still hold, whereas the 1st option will always return Object. It is for this abstraction that the 2nd style is commonly used for a Factory Pattern.

Karthik T
  • 31,456
  • 5
  • 68
  • 87
1

will o after Object o = makeMeAnObjectPlease() refer to the same memory address as the one created inside makeMeAnObjectPlease()?

We don't speak about memory addresses in Java. It will refer to the same object. How could it not?

And are there any more differences?

Only the extra method call, and whatever is inside it.

user207421
  • 305,947
  • 44
  • 307
  • 483