File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes passing by reference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "passing by reference" Watch "passing by reference" New topic
Author

passing by reference

kiruthigha rajan
Ranch Hand

Joined: Dec 29, 2011
Posts: 69
I was going thorugh an artical in javaworld and where i came across this program. can any one help me in under standing this below concept ?


The out put of the above program is
1.X: 0 Y:0
2.X: 0 Y:0
3.X: 100 Y:100
4.X: 0 Y:0 //why this is not X:100 and Y:100
thanks in advance

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

There is no pass-by-reference in Java. Java only passes by value.

Primitives are passed by value.
References are passed by value.
Null is passed by value.

Nothing is passed by reference.

Objects are not passed at all, not by reference and not by value.

Note that there is a huge difference between passing an object by reference (which Java does not do) and passing a reference by value (which Java does do).

In the tricky() method, arg1 and arg2 are local variables, and since there's no PBR, changing the value of those local variables does not affect the value of the caller's copies of those variables.

So, we create two Point objects. We pass copies of our references to those point objects to tricky(). tricky() then changes the contents of one of those point objects through its local variable. Next, tricky() swaps its local variables, so that they point to each others' original objects. This does not affect the caller's copy of those variables.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7064
    
  16

kiruthigha rajan wrote: Y:100
4.X: 0 Y:0 //why this is not X:100 and Y:100

Quick history (and possibly programming) lesson:
1. 'reference' does NOT mean the same thing in all languages; and therefore the term "pass by reference" is ambiguous.
2. "Pass by value" is generally better than "pass by reference" (sic), because what the method gets is a copy of the thing passed to it. Thus, I could write:and, no matter how many methods I call, x will still equal 32 unless I assign the result of one of those methods back to it. This means that the value of x is entirely under my control (or at least of the code that defines it), and not by any method called on it (a major consideration in big projects, believe me).

Getting back to 'pass by reference': What it usually (and certainly in C/C++ terms) means is: 'pass by memory address', and in C it comes in the form of the '&' operator (which does not exist in Java), viz:
public void naughtyMethod(&someObject) {..

The thinking behind this was (back in the days when memory was expensive) that if someObject happened to be something like a big array, passing its memory address was much faster than copying the entire structure.

Since Java doesn't allow you access to the memory address of an object, it cannot possibly provide this type of behaviour; however, it does provide (in fact, it is based on providing) an alternative: a reference.
A Java reference is NOT a memory address, even though it may seem like one; it is the language's way of accessing a Java object. In practise, it may be a memory address, but it's really better not to think of it that way, and if you ever write code that relies on preconception of how you think it does it, expect it to FAIL. Maybe not now; but it will.. How the language accesses objects is none of your business - and shouldn't be.

Finally (sorry), back to your question "why this is not X:100 and Y:100?" - because you are not looking at the object you think you are. Have another look at the code.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Winston Gutkowski wrote:
kiruthigha rajan wrote: Y:100
4.X: 0 Y:0 //why this is not X:100 and Y:100

Quick history (and possibly programming) lesson:
1. 'reference' does NOT mean the same thing in all languages;


Agree.

Java's "reference" is much more like C/C++'s "pointer" than it is like C++'s "reference".


and therefore the term "pass by reference" is ambiguous.


Disagree. But then, I might not have as much experience with other languages as you.

Getting back to 'pass by reference': What it usually (and certainly in C/C++ terms) means is: 'pass by memory address',


That's a little low-level, and can blur the lines with C's passing pointers by value. I like the description of an "alias" of the variable. The key point, whatever terminology you use, is that the variable inside the called method is effectively the same variable as the one in the caller, so that whatever you do to the local variable, it's as if you had done it to the caller's variable.

and in C it comes in the form of the '&' operator


Ah, yes, that would be that confusion I mentioned. :-)

C does not have pass-by-reference. Using the & operator as you describe simply passes the variable's address by value.


kiruthigha rajan
Ranch Hand

Joined: Dec 29, 2011
Posts: 69
i didnt understand the program part..can you please explain more precisely
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
kiruthigha rajan wrote:i didnt understand the program part..can you please explain more precisely

Jeff's explanation was pretty precise. Can you be a bit more precise about what you're still not sure about ?
kiruthigha rajan
Ranch Hand

Joined: Dec 29, 2011
Posts: 69
Joanne Neal wrote:
kiruthigha rajan wrote:i didnt understand the program part..can you please explain more precisely

Jeff's explanation was pretty precise. Can you be a bit more precise about what you're still not sure about ?


i didnt understand this line:
Next, tricky() swaps its local variables, so that they point to each others' original objects. This does not affect the caller's copy of those variables.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

kiruthigha rajan wrote:
Joanne Neal wrote:
kiruthigha rajan wrote:i didnt understand the program part..can you please explain more precisely

Jeff's explanation was pretty precise. Can you be a bit more precise about what you're still not sure about ?


i didnt understand this line:
Next, tricky() swaps its local variables, so that they point to each others' original objects. This does not affect the caller's copy of those variables.


Initially, in main(), we have reference variables pnt1 referring to some Point object and pnt2 referring to some other Point object. Let's call those objects Obj1 and Obj2.

Additionally, in tricky(), we have reference variable arg1 referring to the same Obj1 as main()'s pnt1 and arg2 referring to the same Obj2 as main's pnt2.

Then, in tricky(), we do this:


That says:
1) Take the value in arg1 and copy it into temp. Now arg1, temp, and pnt1 all refer to the same Point object (Obj1).
2) Take the value in arg2 and copy it into arg1. Now arg1, arg2, and pnt2 all refer to the same Point object (Obj2). Note that putting a different reference value into arg2 does not affect the reference value that's in pnt2, because Java is strictly pass-by-value.
3) Take the value in temp (a reference to Obj1) can copy it into arg2.

Now pnt1, temp, and arg2 all refer to Obj1; and pnt2 and arg1 refer to Obj2.

You can imagine it as if pnt1, pnt2, arg1, arg2, and temp are pieces of paper. On pnt1 we wrote the memory address where Obj1 is stored and on pnt2 we wrote the memory address where Obj2 is stored. When we called tricky(), we looked at the address written on pnt1 and copied it onto arg1, and copied the address written on pnt2 onto arg2. Then, inside tricky(), we did some erasing and copying of arg1, arg2, and temp, but none of that affected what was written on the pnt1 and pnt2 papers.

(Note, however, that strictly speaking, references are not necessarily the "memory address" of an object per se. The spec just says that they "refer to an Object" or something like that, with details left up to the implementation. For the purposes of understanding this issue, it's a reasonable model to use.)
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7064
    
  16

kiruthigha rajan wrote:i didnt understand the program part..can you please explain more precisely

You might do well to have a look at the Cup Size page in the Campfire stories section, and its Pass-by-value companion (read the 'Cup size' page first though). They may help to explain it better if you're still having problems, and there are quite a few examples.

Winston
kiruthigha rajan
Ranch Hand

Joined: Dec 29, 2011
Posts: 69
Jeff Verdegan wrote:
Then, in tricky(), we do this:


That says:
1) Take the value in arg1 and copy it into temp. Now arg1, temp, and pnt1 all refer to the same Point object (Obj1).
2) Take the value in arg2 and copy it into arg1. Now arg1, arg2, and pnt2 all refer to the same Point object (Obj2). Note that putting a different reference value into arg2 does not affect the reference value that's in pnt2, because Java is strictly pass-by-value.
3) Take the value in temp (a reference to Obj1) can copy it into arg2.


thanks alot for the marvelous explanation..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: passing by reference
 
Similar Threads
Doubt regarding StringBuffer
Java "pass by reference" or "pass by value"?
Pass by value
Need explanations of inheritance
a question about memory pointers