aspose file tools*
The moose likes Java in General and the fly likes Pass by value & pass by reference data types Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Pass by value & pass by reference data types" Watch "Pass by value & pass by reference data types" New topic
Author

Pass by value & pass by reference data types

Naina Si
Ranch Hand

Joined: Nov 05, 2003
Posts: 134
All the primitive data types are passed by value.
Objects are passed by reference. Does that hold true for all data types?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

All the primitive data types are passed by value.
Objects are passed by reference. Does that hold true for all data types?


In Java, all parameters are pass by value, be they primitives or Objects (and there are no other data types). This can be a bit confusing to understand, since what is being passed by value in the case of Objects is the reference. It is often believed to mean the same thing as pass by reference, but it is not. Have a look at this page:
http://java.sun.com/docs/books/tutorial/java/javaOO/arguments.html
About half way down the page is a 'Pass By Value' heading that hopefully explains it well enough.


Steve
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
Here's a good code example which illustrates that object references are passed by value...



Here, you're passing in the reference variable "s" to the someMethod() method. Inside the method, you're changing what the reference points to. But, the original reference of the calling code still points to its original value. So, you're passing a reference by value.


James Carman, President<br />Carman Consulting, Inc.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Steve Luke:


In Java, all parameters are pass by value, be they primitives or Objects (and there are no other data types). This can be a bit confusing to understand, since what is being passed by value in the case of Objects is the reference. (snip)

In my opinion, the first sentence here is somewhat contradictory to the second sentence. Objects are NOT passed by value; their *references* are. So essentially, objects are passed by reference because you only manipulate references to objects within any Java program.

Layne


Java API Documentation
The Java Tutorial
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Originally posted by Layne Lund:

In my opinion, the first sentence here is somewhat contradictory to the second sentence. Objects are NOT passed by value; their *references* are. So essentially, objects are passed by reference because you only manipulate references to objects within any Java program.

Layne


Let us quote James Gosling:
From "The Java Programming Language Second Edition", by Ken Arnold and James Gosling:

Some people will say incorrectly that objects in Java are "pass by reference." The term pass by reference properly means that when an argument is passed to a function, the invoked function gets a reference to the original value, not a copy of its value. If the function modifies its parameter, the value in the calling code will be changed because the argument and parameter use the same slot in memory. [...] There is exactly one parameter passing mode in Java--pass by value--and that helps keep things simple.


That, combined with James' earlier reply are adequate explanation I think.
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
<opening can of worms>This reminds me of a conversation I had with someone who said "I heard there are no pointers in Java."</opening can of worms>
Surasak Leenapongpanit
Ranch Hand

Joined: May 10, 2002
Posts: 341

Objects are passed by reference. Does that hold true for all data types?


In RMI, Object can be passed by reference.
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
Originally posted by Surasak Leenapongpanit:


In RMI, Object can be passed by reference.


Nope. They're still passed by value if you follow the previous examples/arguments.
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
The trick behind 'everything is pass by value' is that you never hold on to an Object, ever. You're variables only hold a reference to an Object. So it is never correct to say that you get an object, rather you get a reference to an object and store the reference in a variable.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
The quote from James Gosling doesn't refute that objects are not passed by reference. In fact, I think it asserts this. First let's understand the difference between an object and a reference. In Java, we often declare reference variables such as



Here str is a reference to a String object. The object contains the information about the string, including the characters, information where to find the methods, etc. When we call a method, such as

The reference to the object is passed by value. If the method changes the state of the object via method calls on the reference variable or setting member fields directly, that change is reflected in the method that called it. This can conceivably fit Mr. Gosling's description of how pass-by-reference works.

So for me, saying that the reference is passed by value is equivalent to saying that the object is passed by reference.

Layne
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Firsy let me say I am not arguing that my first post was not confusing... That is why I refered to James' in my last post as a better explanation

Originally posted by Layne Lund:
So for me, saying that the reference is passed by value is equivalent to saying that the object is passed by reference.


Second, I think when the authors of the Java Language Specs says (in Java) it is wrong to say Objects are passed by reference, then perhaps you whould think they know what they are talking about.

Third, the problem here is you are not using the term 'pass by reference' correctly here. As Gosling says: "The term pass by reference properly means that when an argument is passed to a function, the invoked function gets a reference to the original value, not a copy of its value. If the function modifies its parameter, the value in the calling code will be changed..." (emphasis mine)

In the String example, a reference to the String is passed to the method. The method modifies the reference it gets to refer to a different String. In the calling code, the String does not change.

As has been said, the String is not passed (at all), the reference is passed. When you change the value passes in (the reference) nothing happend to the object in the calling code. Therefore the object is not passed by reference. (the object is not passed at all, and the reference is passed by value).

Originally posted by Layne Lund:
The quote from James Gosling doesn't refute that objects are not passed by reference. In fact, I think it asserts this.If the method changes the state of the object via method calls on the reference variable or setting member fields directly, that change is reflected in the method that called it. This can conceivably fit Mr. Gosling's description of how pass-by-reference works.


No. In this case you are not modifying the parameter to the method (the reference). You are modifying the object at the location the reference refers to.
[ March 11, 2005: Message edited by: Steve Luke ]
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1821



Now, what is the expected result? The problem is that there are three possibilities, but only two terms to describe them!

Pass-by-value provides the "Value is 10" result.

Pass-by-value-of-reference provides the "Value is 20" result.

Pass-by-reference provides the "Value is 30" result.


As Layne said, the objects are passed by reference, but the references/pointers are passed by value. I modified the object in line 11. I modified the parameter in line 12.

I understand what the writers of the JLS are trying to say, but I think that they could have said it better. As I said, the problem is that the terminology is lacking, not the concepts.


So for me, saying that the reference is passed by value is equivalent to saying that the object is passed by reference.

I agree. By we have to keep in mind that the references are what is passed, not the object. Technically speaking, references are passed-by-value. Conceptually speaking, objects are passed by reference. You use of the terminology will depend on whether you look at the technical or conceptual side of Programming. A document like the JLS has to look at the technical side, so that's the terminology it used.
[ March 11, 2005: Message edited by: Joel McNary ]

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Steve Luke:
Second, I think when the authors of the Java Language Specs says (in Java) it is wrong to say Objects are passed by reference, then perhaps you whould think they know what they are talking about.


I'm sure they know what they are talking about. However, I agree with Joel that they could have said it better. I understand the intent of what you say and apparently my translation differs slightly. The main difference I see is that we may be using terms in a slightly different manner. Perhaps this is partly due to my background with C and C++. In particular, C uses pointers as a pass-by-reference mechanism which works nearly indentical to references in Java.

In the String example, a reference to the String is passed to the method. The method modifies the reference it gets to refer to a different String. In the calling code, the String does not change.


Perhaps my example using a String was bad since String objects are immutable anyways. However, as Joel's example above shows more clearly, you can change the value of the object that the reference "points" to.

No. In this case you are not modifying the parameter to the method (the reference). You are modifying the object at the location the reference refers to.


Exactly.

Perhaps we can clarify this with an example in a language that uses "proper pass-by-reference". In my experience with other languages, primarily with C and C++, pass-by-reference works identically with the way we see it working in Java as we are discussing here.

Layne
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
I think we all realize here what is going on with respect to how objects are passed back and forth in Java. Yes, it is true that one could say "objects are passed by reference in Java", because we're passing around an object reference rather than the actual object. The reason that folks (Gosling et. al) would rather see it the other way is so that there is really only one mechanism for passing parameters in Java, pass-by-value. That sounds a lot better than saying "well, for primitives, we pass them by value, but for objects, we pass them by reference." That makes the language sound like it has multi-personality disorder. When, in fact, the language is quite elegant and sane and it's all of us who are crazy for sitting here for two day splitting hairs! But, it is a fun way for us to show off how much we know about Java.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by James Carman:
Yes, it is true that one could say "objects are passed by reference in Java", because we're passing around an object reference rather than the actual object. The reason that folks (Gosling et. al) would rather see it the other way is so that there is really only one mechanism for passing parameters in Java, pass-by-value.


Actually the reason is that "passing the reference of an object by value" and "passing an object by reference" *are not* the same thing at all. Joel's example showed that in a very obvious way, I think - the two methods of parameter passing would lead to very different results with his code!


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by James Carman:
When, in fact, the language is quite elegant and sane and it's all of us who are crazy for sitting here for two day splitting hairs! But, it is a fun way for us to show off how much we know about Java.


Here! Here! I think I'll just have to agree to disagree as far as the use of terminology goes

Layne
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Pass by value & pass by reference data types