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 pass 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 "pass by reference" Watch "pass by reference" New topic
Author

pass by reference

Edward Chen
Ranch Hand

Joined: Dec 23, 2003
Posts: 798
I have seen lots of discussion about pass by reference / value, do we have a simple and final conclusion about this ?

Here is my thinking

1. primitive type (like int) pass by its value
2. Object pass by its address value (reference), except immutable type (like Integer).

is it enough ? do I miss something ?
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

Edward Chen wrote:I have seen lots of discussion about pass by reference / value, do we have a simple and final conclusion about this ?

Here is my thinking

1. primitive type (like int) pass by its value
2. Object pass by its address value (reference), except immutable type (like Integer).

is it enough ? do I miss something ?


Agree- In Java its always pass-by-value.
In case of primitives- the copy of the value is passed.
In case of Object references- The copy of the reference is passed- which Indirectly is similar to pass by reference (something like pointer). The Object references actually hold the address of the instances, so whenever the reference variable is passed to a method- the copy of the address of the Instance gets passed. So any manipulations done using the passed value will affect the actual instance.

Coming to the Immutable types- If at all we wanted to change the value, we would have to create a new instance with the value we want.



Mohamed Sanaulla | My Blog
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3646
    
  16

Edward Chen wrote:2. Object pass by its address value (reference), except immutable type (like Integer).

No, immutable types have no special status. They are passed exactly the same way other objects are. The fact that you can't change their values doesn't mean that the object is a copy of the argument.

Simply said, all arguments are passed by value, meaning that objects are effectively passed by reference.
Edward Chen
Ranch Hand

Joined: Dec 23, 2003
Posts: 798
Stephan van Hulst wrote:Simply said, all arguments are passed by value

OK, I agree with this. Even Interger, it will pass by the address value, then why we can't change its original value in caller method ?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3646
    
  16

Because Integer is immutable. This is a property of the class, not of the Java language.

You can't change the value of an Integer regardless of whether you got it as a method argument, right?
Edward Chen
Ranch Hand

Joined: Dec 23, 2003
Posts: 798
Stephan van Hulst wrote:You can't change the value of an Integer regardless of whether you got it as a method argument, right?

But in the caller method, we can change Integer value, for example in code line 9.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18836
    
  40

Edward Chen wrote:
But in the caller method, we can change Integer value, for example in code line 9.


Where's line 9?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Edward Chen
Ranch Hand

Joined: Dec 23, 2003
Posts: 798
Henry Wong wrote:Where's line 9?

Henry


In the code blow, Line 10, I change the Integer value.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18836
    
  40

Edward Chen wrote:
In the code blow, I change the Integer value.


Basically, that line changes the integer reference to point to a different Integer object. You didn't change the value of the Integer object that the reference originally pointed to.

There is also some autoboxing in play here... that line actually translates to...



Henry
Edward Chen
Ranch Hand

Joined: Dec 23, 2003
Posts: 798
Henry Wong wrote:
You didn't change the value of the Integer object that the reference originally pointed to.

very confused. same name "myInt1" in Line 10, then which one is the "originally" ?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Edward Chen wrote:
Henry Wong wrote:
You didn't change the value of the Integer object that the reference originally pointed to.

very confused. same name "myInt1" in Line 10, then which one is the "originally" ?


There is only one reference variable, and it's named "myInt1". To start with it contains a reference to one particular Integer object. Later it is changed to contain a reference to a different Integer object. That is no different than having an int value which starts by containing one int value, and is later changed to contain a different int value.

You were probably under the common beginner misconception that Java objects have names. That isn't the case. It's variables which have names, not objects. And in case you still think that variables can contain objects, get rid of that misconception too. Variables can contain references to objects.
Edward Chen
Ranch Hand

Joined: Dec 23, 2003
Posts: 798
Paul Clapham wrote:
You were probably under the common beginner misconception that Java objects have names. That isn't the case. It's variables which have names, not objects. And in case you still think that variables can contain objects, get rid of that misconception too. Variables can contain references to objects.


After I did some research, I revise my statement.
1. primitive type pass by value,
2. object pass by value, actually the value of the object address.

As changing value or not, it is regard to immutable object, different topic from pass-by-value. Because Integer, String ... are immutable, when assigned value, it will get a new object with new value.

My understanding is right ?
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

Edward Chen wrote:
Paul Clapham wrote:
are immutable, when assigned value, it will get a new object with new value.


Agree with Edward. Just to add to it- The Immutable reference in the caller's method will be still referring to the earlier instance and the Immutable reference declared in the method declaration will point to the new object with new value.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

mohamed sanaullah wrote:The Immutable reference in the caller's method will be still referring to the earlier instance and the Immutable reference declared in the method declaration will point to the new object with new value.

Actually... when you say "the Immutable reference" it sounds very much as if you're talking about a reference which is immutable. Were you really talking about references to objects, where the objects are immutable? Because that's more accurate. Objects may be immutable if their state can't be changed. I'm not sure what it means for a reference to be immutable, except it might be talking about a final variable which contains a reference, but can't be changed to contain a different reference.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

Paul Clapham wrote:
mohamed sanaullah wrote:The Immutable reference in the caller's method will be still referring to the earlier instance and the Immutable reference declared in the method declaration will point to the new object with new value.

Actually... when you say "the Immutable reference" it sounds very much as if you're talking about a reference which is immutable. Were you really talking about references to objects, where the objects are immutable? Because that's more accurate. Objects may be immutable if their state can't be changed. I'm not sure what it means for a reference to be immutable, except it might be talking about a final variable which contains a reference, but can't be changed to contain a different reference.


Ah, sorry for the confusion. My bad. Didnt notice that "Immutable Reference" sounds totally different from what I meant. I meant- references to objects, where the objects are immutable
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: pass by reference