All the primitive data types are passed by value.
Objects are passed by reference. Does that hold true for all data types?
Steve
James Carman, President<br />Carman Consulting, Inc.
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)
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
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.
Steve
James Carman, President<br />Carman Consulting, Inc.
Objects are passed by reference. Does that hold true for all data types?
Originally posted by Surasak Leenapongpanit:
In RMI, Object can be passed by reference.
James Carman, President<br />Carman Consulting, Inc.
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.
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.
Steve
So for me, saying that the reference is passed by value is equivalent to saying that the object is passed by reference.
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
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.
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.
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.
James Carman, President<br />Carman Consulting, Inc.
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.
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
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.
Consider Paul's rocket mass heater. |