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.
- Is Java "pass-by-reference" or "pass-by-value"?
- 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?