Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object by Reference vs. Reference by Value

I read this comment here: Passing a String by Reference in Java?

Yes, it's a misconception. It's a huge, widespread misconception. It leads to an interview question I hate: ("how does Java pass arguments"). I hate it because roughly half of the interviewers actually seem to want the wrong answer ("primitives by value, objects by reference"). The right answer takes longer to give, and seems to confuse some of them. And they won't be convinced: I swear I flunked a tech screen because the CSMajor-type screener had heard the misconception in college and believed it as gospel. Feh. – CPerkins Aug 13 '09 at 14:34

Can someone please explain, in terms that a new programmer can grasp, what is the difference between saying:

"In Java primitives are passed by value and objects are passed by reference."

and:

"In Java nothing is passed by reference and references are passed by value."?

Are both of these statements true in some sense? I don't want to invite a rant parade, but this sounds like a really important concept, and one I still do not completely understand.

like image 830
Steve the Maker Avatar asked Feb 27 '12 16:02

Steve the Maker


People also ask

What is the difference between by reference and by value?

In the case of Call by Value, when we pass the value of the parameter during the calling of the function, it copies them to the function's actual local argument. In the case of Call by Reference, when we pass the parameter's location reference/address, it copies and assigns them to the function's local argument.

Is value objects different from reference objects?

A reference is an address (pointer) to where the value of the object resides. A value is the actual value the binary representation. If you assign a value you are making a copy of the value into the new variable. If you assign a reference you are just passing the address of where the value is saved.

Are objects called by value or reference?

Arguments are passed by assignment in Python. The assignment just creates references to objects. Hence, Immutable objects passed as arguments are referred as in Call by Value and Mutable objects, when passed as arguments are referred as Call by Reference.

What is the difference between passing a value object by reference and a reference object by value?

Passing a variable To make it short: pass by value means the actual value is passed on. Pass by reference means a number (called an address) is passed on which defines where the value is stored.


2 Answers

I believe the misconception lies in the fact that a variable can not contain an object to begin with. If you grasp that, then obviously variables can only contain references to objects (or primitive values). The step from there to realizing that references are passed by value (just as primitive values) is quite small.

There is a really easy test you can do to figure out if a language supports pass by reference. Ask yourself if you can write a swap function in the language, i.e. something that works like

x == A, y == B

swap(x, y);

x == B, y == A

As a Java programmer you realize quickly that you can't implement this in Java, thus you (correctly) draw the conclusion that Java does not have pass by reference.

Returning to your sentences:

  • In Java primitives are passed by value and objects are passed by reference.

This is false. I would argue that you can only pass something that is contained in a variable, and as I stated above, a variable can't contain an object, thus you can't pass an object at all in Java.

  • In Java nothing is passed by reference and references are passed by value.

This is true.

like image 87
aioobe Avatar answered Oct 04 '22 11:10

aioobe


Things like this are always easier when drawn. Consider the following two variables, one being a primitive-type and one being a reference-type:

    int i    = 5;
    String s = "test";

somewhere in memory there is an entry for i that looks like this:

  i
-----
| 5 |
-----

similarly there is also an entry for s in memory but it refers to a location on the heap since s is a reference-type variable and objects are stored on the heap:

                            ----------- 
 s                 |------->|  "test" |
-----              |        |---------|
| --|--------------|        |         |
-----                       |         |
                            |         |
                            |---------|

So the value of s is the reference to that String object that is sitting on the heap, so if s were passed to a method:

printString(s);

public void printString(String arg)
{
   System.out.println(arg);
}

The value that actually gets copied into the arg parameter is that reference to s on the heap:

                            ----------- 
 s                 |------->|  "test" |<-----|
-----              |        |---------|      |
| --|--------------|        |         |      |
-----                       |         |      |
                            |         |      |
                            |---------|      |
 arg                                         |
-----                                        |
| --|----------------------------------------- 
-----

Hope this helps.

like image 27
Hunter McMillen Avatar answered Oct 04 '22 12:10

Hunter McMillen