2

Before I post my question, I have read the following excellent articles on java-pass-by-value. I am convinced I have understood it well.

  1. Is Java "pass-by-reference" or "pass-by-value"?
  2. http://www.javaworld.com/article/2077424/learn-java/does-java-pass-by-reference-or-pass-by-value.html

My question has to do with a side-by comparison of Java with other language that supports pass-by-reference(C++ may be).

In case of Java, you have a handle (reference) pointing to the object in location A. so object itself could be modified. But It is not possible to change the object location itself. I.e An object stored in memory address 0X945 cannot be changed to 0X948.

In languages such as C++, you can choose to pass-by-value or pass-by-reference. (It is in the hands of the programmer correct?). Hence it is possible to change the location of object in memory space correct?

P.S: I have good background on Java but on C++. so my views above may be wrong. It is claimed in the article 1, I cited above that there is no notion of pointers in Java. I dont know how far that is true? (why do NullPointerException exists then)

EDIT: consider this example:

void swap(Object A,Object B) {
  Object temp=B;
  Object B=A;
  Oject A=temp;
}

when I call the method in Java such as swap(A,B), nothing happens but in C++ (I presume), swap happens. which probably means I am changing the location of objects in memory correct?

Community
  • 1
  • 1
brain storm
  • 30,124
  • 69
  • 225
  • 393
  • Java is ***always*** pass by value. – Elliott Frisch Sep 17 '14 at 14:00
  • @ElliottFrisch: yes, that is totally engrained in my brain. but my question is it possible to change the location of object from memory Address A to memory address B. I think that is possible in pass-by-reference supported languages such as C++ correct? – brain storm Sep 17 '14 at 14:04
  • 1
    No. It's not possible in Java and it's not possible in C++. – Elliott Frisch Sep 17 '14 at 14:06
  • It is possible to change the location of an object in C++ if you pass a reference to a pointer. AFAIK no equivalent feature exists in java, however you could add a level of indirection -- pass (by value) a reference to an object which has a reference to the object-in-question. That would let you allocate a new instance of the target object and make the caller aware of it. – Dale Wilson Sep 17 '14 at 14:24
  • since I have no knowledge of C++, pardon my mistakes. but I thought a swap(Object A, Object B) works in C++, because you interchange the location of objects, which is not possible in Java. so in what way a pass-by-reference differ from pass-by-value then? – brain storm Sep 17 '14 at 14:27
  • please see my edit of question above – brain storm Sep 17 '14 at 14:31
  • You can't change the location of an object. In C++, you can change an argument to make it point to a different object, in Java you cannot. – Peter Lawrey Sep 17 '14 at 14:59
  • In C++ you would need to write `swap(Object&*A, Object&*B)` for it to work you expected otherwise `swap(Object*A, Object*B)` behaves the same as Java and `swap(Object A, Object B)` pass the *object* by value which you can't do in Java. – Peter Lawrey Sep 17 '14 at 14:59
  • @PeterLawrey: so in C++, do `pass-by-reference` means you pass the object itself, so that it could be reassigned in swap method? – brain storm Sep 17 '14 at 15:04
  • @brainstorm As the code is written C++ makes temporary copies of A and B and lets you change the copies which get thrown away when the function returns. The caller does not see any changes. – Dale Wilson Sep 17 '14 at 15:26

4 Answers4

2

In java even - references to objects are passed by value. i.e, everything is pass-by-value. Next,

you can choose to pass-by-value or pass-by-reference. (It is in the hands of the programmer correct?). Correct. But you can't do it in Java.

An object stored in memory address 0X945 cannot be changed to 0X948. You can't do this in both java and C++.

NullPointerException is thrown when you try to access a property / method of something which doesn't exist (is null). i.e, the reference points to null when an instance of the object is required.

 Object o = null;
 o.toString()  --> NPE. o points to null.
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
  • +1 for answering the actual questions. NullPointerException is thrown when you try to use a null value when a reference to an object is required. See the API docs for a list of some of the cases that can cause this: http://docs.oracle.com/javase/7/docs/api/java/lang/NullPointerException.html – Andy Thomas Sep 17 '14 at 14:07
  • so how is pass-by-reference differ then? An object stored in memory address can be changed to difference location in case of pass-by-reference but not by value. please correct.. – brain storm Sep 17 '14 at 14:15
  • No. In `pass-by-reference`, I will pass the reference to the object. So, you can *actually work on the original object*. In pass by value, you pass the value of the object (not the actual object's reference). In java, what you do is, you pass the reference by value, so you can access the actual object. Check [this](http://stackoverflow.com/questions/2027/pass-by-reference-or-pass-by-value) and [this](http://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value) – TheLostMind Sep 17 '14 at 14:19
  • even in pass by value you work on the original object. a copy of reference is passed, which still points to same object. I will look into the link you provided. but can you explain why a swap(Object A, Object B) works in C++, but not in Java? Thanks – brain storm Sep 17 '14 at 14:25
  • @brainstorm - It will work in java as well. Its just that you don't need the swap function. you could just do `temp=A` , `A=B`, `B=temp` in any method. Here you are just re-assigning and swapping *references* to objects. – TheLostMind Sep 17 '14 at 15:50
  • @TheLostMind: my point is about using swap function that is where pass-by-reference or pass-by-value happens.. – brain storm Sep 17 '14 at 15:53
1

so in C++, do pass-by-reference means you pass the object itself, so that it could be reassigned in swap method

In C++, pass by reference, swap(Object &A, Object &B) appears to be close to java's pass by value.

In Java Object A is a reference to an Object and is null by default. As Object is already a reference and so when this reference is copied, it is passed by value.

In C++, Object A is an instance of an Object and is always a unique object. As Object is an instance, you are passing by reference using Object& because the Object is not passed, but a reference to it.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • and in C++, you could still the pass the object itself by `swap(object* A, object* B)` correct? some how, I am missing something as how pass-by-reference and pass-by-reference-value differ when it comes to dealing with objects (with primitives it is clear). – brain storm Sep 17 '14 at 15:26
  • @brainstorm `Object*` is a pointer which might refer to be an Object. This pointer has to be non-const so you can't do `Object A, B; swap(&A, &B);` – Peter Lawrey Sep 17 '14 at 15:30
  • When you use `Object &a, Object &b` in C++, and then you do `a=b;` inside, you replace the "contents" of `a` with the contents of `b`. `a` still has the same address, but it is now a different object. I believe it's called "value semantics". There is no correponding semantics in Java. In java, `a=b;` means that the variable `a` is now refering to the same object as variable `b` - not the same thing at all. –  Sep 17 '14 at 15:41
  • Is it correct to think of pass-by-reference as passing the memory address of the location whereas pass-by-value as passing the copy of handle that points to object? I am still not seeing a distinction between pass-by-reference or pass-by-reference-value.. – brain storm Sep 17 '14 at 15:41
  • @brainstorm pass-by-value is passing a copy of what the value is. What is confusing is that `Object o` in Java is a reference. This means when you pass `p` you pass the reference by value. What you have to put out of your mind is that notion that when you see `Object p` it is an instance, because it is not in Java. – Peter Lawrey Sep 17 '14 at 16:02
0

Java is always pass by value, it's just when you are passing objects, the value passed is the location in memory so it can act like pass by reference.

Ross Drew
  • 8,163
  • 2
  • 41
  • 53
0

when I call the method in Java such as swap(A,B), nothing happens but in C++ (I presume), swap happens.

No it doesn't. "Nothing" also happens in C++.

A correct translation of the code to C++ would be:

void swap(Object *A,Object *B) {
  Object *temp=B;
  B=A;
  A=temp;
}

(Yes, the syntax for types is different between the languages. Namely, the pointer-to-Foo type is written as Foo * in C++ and Foo in Java; that's just a syntactical difference between the languages.)

newacct
  • 119,665
  • 29
  • 163
  • 224
  • I guess in C++, swap happens if you pass by ref such as swap(object &A, object &B). I think my phrasing of question is really bad. I will post a new question more clearly – brain storm Sep 18 '14 at 18:07