Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between passing by reference in Java and passing a pointer in C?

I have been studying Java for a few months and am now starting to learn C.

I am a little confused, I was under the impression that passing an object by reference and passing a pointer to that object were the same thing: I thought the difference was that in Java all passing of objects is done with pointers automatically, where as in C one has to actually sprinkle little asterisks and ampersands here and there. Recently, in conversation, I was assured that there was a difference!

What is the difference between passing by reference and passing a pointer?

like image 672
Ziggy Avatar asked Mar 01 '09 22:03

Ziggy


People also ask

What is pass by reference in Java?

Pass by Value: It is a process in which the function parameter values are copied to another variable and instead this object copied is passed. This is known as call by Value. Pass by Reference: It is a process in which the actual copy of reference is passed to the function. This is called by Reference.

Does Java pass by pointer?

To sum up: Java has pointers, and the value of the pointer is passed in. There's no way to actually pass an object itself as a parameter. You can only pass a pointer to an object.

What is the difference between pass by value and pass by pointer in C?

The difference between pass-by-pointer and pass-by-value is that modifications made to arguments passed in by pointer in the called function have effect in the calling function, whereas modifications made to arguments passed in by value in the called function can not affect the calling function.

Are pointers passed by reference in C?

There is no such thing as reference in C. Passing a pointer to a function will not copy the object that the pointer is pointing to.


2 Answers

I thought the difference was that in Java all passing of objects is done with pointers automatically, where as in C one has to actually sprinkle little asterisks and ampersands here and there.

Conceptional, that's quite right. If we are pedantic (and that's a good thing), we can even say objects are not passed at all in Java. What is passed is only ever the "pointer", which in Java is called the reference. All indirection is done automatically. So when you do "objref->foo" instead of "objref.foo" in C and can't use the dot because you work with a pointer, in Java you can still use the dot because it doesn't know anything else to access members anyway. In C, you can pass the pointer (and here, it is actually called pointer) and you can pass the object itself, in which case a copy is passed. In C, you access the object that a pointer refers to by indirection using the star or "->". In Java, the only way objects are accessed anyway are using the dot (objref.member).

If we are pedantic again (now more important again), neither in Java nor in C there is "pass by reference". What we pass in Java is the reference/pointer to the object and in C we either pass a copy of the object (obvious case) or we pass again just a copy of a pointer to the object. So in both cases - Java and C - what we pass are the addresses of the objects. Not talking about primitive java types, which are copied in both Java and C - even though you can pass their address in C too, there is no difference between an aggregate type (i.e a struct) and a "primitive type" in C in that regard.

like image 34
Johannes Schaub - litb Avatar answered Oct 11 '22 13:10

Johannes Schaub - litb


Neither Java nor C has pass-by-reference. They are both strictly pass-by-value.

Pass-by-reference semantics mean that when you change the value of the parameter in the method, the caller will see that change in the argument.

Now, you may be thinking: "But that's the case in Java! If I change an object during the method, the caller sees that change." The object isn't the parameter. The parameter is just the variable - and if you change the value of that variable, the caller won't see that. For example:

public void foo(Object x)
{
    x = null;
}

public void caller()
{
    Object y = new Object();
    foo(y);
    // y is still not null!
}

If the parameter were really passed by reference, y would be null afterwards. Instead, the value of y is just a reference, and that reference is passed by value. It's confusing because the word "reference" is in both terms, but they're different things.

You may want to look at my article about C# parameter passing to see what would be possible if Java did have pass-by-reference semantics, as C# does when (and only when) you use the ref keyword.

You may also want to look at the comments to my Stack Overflow answer related to the topic.

like image 145
Jon Skeet Avatar answered Oct 11 '22 13:10

Jon Skeet