aspose file tools*
The moose likes Java in General and the fly likes pass-by-value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "pass-by-value" Watch "pass-by-value" New topic
Author

pass-by-value

shivani anand
Ranch Hand

Joined: Dec 28, 2000
Posts: 155
Is their anyway to pass-by-value (other than using clone()) in Java as it is done in C++??
shivani
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

All Java supports is pass by value. That is, when you pass a primitive variable, you pass its contents, not the variable itself. When you pass a reference, you pass its contents, i.e., the address the reference contains.
How is pass-by-value different in C++?
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
anil bisht
Ranch Hand

Joined: Nov 04, 2000
Posts: 81
hi
primitives are passed by value and the objects are passed by references.. and i think there is no way u can pass an object by value..
anil
Sri Bala
Ranch Hand

Joined: Mar 06, 2001
Posts: 63
Sometime back we were discussing this. Everything's passed by value in java. You can simulate passing by reference in C++ by putting the type you want to pass inside an array or an object. Here's the link. http://www.javaranch.com/ubb/Forum33/HTML/001874.html
ryan burgdorfer
Ranch Hand

Joined: Jan 24, 2001
Posts: 219
Anil,
What do you mean there is no way you can pass an object by value? It is true that when you pass an object, you are passing a reference to the object...but that IS the value of what you are passing...a reference to the object.
Thus, as Michael said above: In Java, everything is pass-by-value.
There is an article here at the Ranch that explains it much better than I ever could: http://www.javaranch.com/campfire/StoryPassBy.jsp
------------------
  • Ryan Burgdorfer
  • Java Acolyte in
  • Columbus, OH USA


<UL TYPE=SQUARE><I><LI>Ryan Burgdorfer<BR><LI>Java Acolyte</I></UL>
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
In c and Java, everything is pass-by-value. In c++ and Pascal, you can pass-by-value or pass-by-reference.
See detailed discussion, references from Sun and other sources, example code, and the exception of Object-pass-by-its-contents in Java(RMI) here:
Pass by Value, etc
Thanks!
Roseanne
Join our SCJD Study Group when certified
Max Rahder
Ranch Hand

Joined: Nov 06, 2000
Posts: 177
The terminology gets a little confusing when the thing you're passing is an object reference.
Everything in Java is pass by value in the sense that the contents of the original variable are copied into the parameter. If the original variable is a reference, then the bits in the reference variable (not the object at the other end of the reference, but the address itself) are copied into the parameter.
If you want to act on a copy of the object, then (as the original note points out) you would have to use the clone method to create a copy of the object. Don't forget that for classes you create you'll have to override clone to have it do what you want. Not all classes implement clone.
If you pass a reference, and the called routine affects the object at the other end of the reference, then the calling routine "sees" that change -- so you can get the same results as a call by reference routine.
Java can be a bit of a pain when you're dealing with primitives, or immutable objects, but that's life when using Java!
Brian Schaefer
Greenhorn

Joined: Feb 20, 2001
Posts: 5
Yup, that last answer by Max is the correct one. If you can mess with the object that the caller has a reference to, that is "pass by reference." Just because someone writes an article titled "Everything is pass by value." doesn't make it so. In fact, one of the articles referred to above has a terrible example using strings --- it makes a difference because strings are immutable.
Peter Tran
Bartender

Joined: Jan 02, 2001
Posts: 783
From The Java Programming Language, by James Gosling et al. 3rd edition (pg. 56):

Some people will say incorrectly that objects are passed "by reference." In programming language design, 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. The Java programming language does not pass objects by reference; it passes object references by value. Because two copies of the same reference refer to the same actual object, changes made through one reference variable are visible through the other. There is exactly one parameter passing mode -- pass by value -- and that helps keep things simple.

-Peter
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
When we code in C, you pass pointers to a method, the value of pointer (address) will never change, but the value the pointer points to will change. In language theory, C is a only-pass-by-value language. Actually, this is exactly Java does. Java Object reference is equivalent to C pointer, the difference is you cannot dereference it, which makes java safer, but less flexible.
Pass by Value
In Java methods, arguments are passed by value.
When invoked, the method receives the value of the variable passed in. When the
argument is of primitive type, pass-by-value means that the method cannot change
its value. When the argument is of reference type, pass-by-value means that the
method cannot change the object reference, but can invoke the object's methods
and modify the accessible variables within the object.
This is often the
source of confusion--a rogrammer writes a method that attempts to modify the
value of one its arguments and the method doesn't work as expected. Let's look
at such method and then investigate how to change it so that it does what the
programmer originally intended.
Quotation from The Java Tutorial

Roseanne
[This message has been edited by Roseanne Zhang (edited March 20, 2001).]
Andy Ceponis
Ranch Hand

Joined: Dec 20, 2000
Posts: 782
I have been reading about "passing by value/reference" for the past few days. I was having trouble understanding the object part as well. Thanks for the explanations guys/gals. It really helped me understand exactly what is going on as well as the correct terminology of what is going on.
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
Since in C, you can dereference a pointer, you actually can prove the pointer is passed by value. A simple test can prove that, you dereference the pointer inside/outside the method call, you will get two different values, which proves inside the method, what we are using actually a copy of the pointer, not the pointer itself.
That is exactly what pass-by-value means. You make a copy of the parameter (argument), and use it inside of the methods. The value of the parameter (argument) will not be affected outside of the method.
In Java, value of an object reference is the reference. That is the source of confusion. However, if you read it twice, it only makes sense. Doesn't it?
Roseanne
[This message has been edited by Roseanne Zhang (edited March 20, 2001).]
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
When you pass-by-reference, you do NOT make a copy of the parameter, you use the parameter itself!!!
These two will never mix!!!
Roseanne
[This message has been edited by Roseanne Zhang (edited March 20, 2001).]
shivani anand
Ranch Hand

Joined: Dec 28, 2000
Posts: 155
Thanks for replying.
By-passing with value I meant how objects in java are passed by value. Like in C++ objects can be passed by value as well as reference(or address). If you put &(addressof operator) in front of parameter in C++ then that means object is passed by reference.
Hope I am clear.
shivani
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
I knew exactly what you meant. However, Java and C languages simply do not support pass-by-reference. They only support pass-by-value.
C++ and Pascal languages support both pass-by-value and pass-by-reference.
Ada language supports probably three, pass-by-value, pass-by-reference, and pass-by-name. Fortran language only support one, pass-by-reference. As far as I could recall, I might be wrong on these two. Don't have time and desire to check them out anymore.
Those are language designers' choices. As a language user like you and me, we cannot do anything about it. What we can do is vote by our keyboard, select the language we like to code...
Roseanne
[This message has been edited by Roseanne Zhang (edited March 21, 2001).]
shivani anand
Ranch Hand

Joined: Dec 28, 2000
Posts: 155
What is this pass-by-name? Never heard about it.
shivani
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
I only roughly remember something about it. I'd better pick-up a Programming Languages book before answering the question to avoid misleading. Then, I realized you could do the same thing by yourself too, if you were really interested.
A lazy answer by Roseanne
[This message has been edited by Roseanne Zhang (edited March 21, 2001).]
Steve Fahlbusch
Bartender

Joined: Sep 18, 2000
Posts: 571
    
    7

Roseanne,
It is Algol that supports pass-by-name. Below
is a link that discusses pass by name in a very nicely.
http://www.cs.sfu.ca/~cameron/Teaching/383/PassByName.html
And in some instances c performs pass by reference.
And it's not really a lazy answer, you just didn't feel like tracking it down at the moment :-)
Steve


[This message has been edited by Steve Fahlbusch (edited March 21, 2001).]
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
Steve wrte:
"And in some instances c performs pass by reference."
Can you give an example?
As far as I was educated and experienced, that was not true. I'm aware of some common misunderstanding on pointer-pass techniques.
C++ is a superset of C, not subset of C.
Thanks!
Roseanne
[This message has been edited by Roseanne Zhang (edited March 22, 2001).]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
This is a Java Program!!!

Button button = new Button("OK");
changeLabel1(button);
System.out.println(button.getLabel()); // will print OK
changeLabel2(button);
System.out.println(button.getLabel()); // will print Cancel

public void changeLabel1(Button button) {
button = new Button("Cancel");
}
public void changeLabel2(Button button) {
button.setLabel("Cancel");
}

Questions?
And yes, Java always passes by value.
[This message has been edited by Thomas Paul (edited March 22, 2001).]
[This message has been edited by Thomas Paul (edited March 23, 2001).]


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
It looks Java to me.
shivani anand
Ranch Hand

Joined: Dec 28, 2000
Posts: 155
Thomas,
Is that C program?? I Know C++ but not C. Do they use system.out.println() in C also??
shivani
Peter Haggar
author
Ranch Hand

Joined: Jan 03, 2001
Posts: 106
To try to sum this up:
Objects in Java are passed by reference. However, all parameters are passed by value. You never pass an object in Java, only a reference. And when you pass the reference, you pass a copy of it, thus you are passing by value.
Peter Haggar
------------------
Senior Software Engineer, IBM
author of: Practical Java


Senior Software Engineer, IBM
author of: Practical Java
Steve Fahlbusch
Bartender

Joined: Sep 18, 2000
Posts: 571
    
    7

c passes-by-refernce when a pointer to a function is passed
this is done resolve the fact that multiple dereferencing of a
function pointer is the same as directly referencing a
function pointer. By the way, I am told that
this mechanism was the idea behind reference
variables (that always pass by reference) in
c++.

[This message has been edited by Steve Fahlbusch (edited March 22, 2001).]
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
Steve
I think that you are wrong on both C and C++ lanuages . I will stop here. Since this is a JavaRanch. There is no need to argue on C/C++ anymore. But be careful when you go to an interview, that will screw you up. Since some interviewer will ask you this question if you claim you know C/C++. I have at least been asked twice.
Roseanne
[This message has been edited by Roseanne Zhang (edited March 22, 2001).]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Objects in Java are passed by reference.
Ack! No! Java does not pass objects! Java passes object pointers by value!!!
Is that C program?? I Know C++ but not C. Do they use system.out.println() in C also??
*Banging head on desk* No, it's a new-fangled language named after some island near Australia.
Peter Haggar
author
Ranch Hand

Joined: Jan 03, 2001
Posts: 106
Originally posted by Thomas Paul:
Ack! No! Java does not pass objects! Java passes object pointers by value!!![/b]

I wasn't saying that Java passes the actual object, which is why I said that Java passes objects by reference. Java passes the reference to the object that lives in the heap, not the object itself. This is a different argument than pass-by-value/pass-by-reference which relates to how parameters are passed. As has been said many times, and is correct, Java passes all parameters by value.
I was pointing out that some of the confusion about parameter passing in Java results from the correct, but sometimes confusing, statement": "Objects are passed by reference." The fact that Java passes objects by reference does not mean that Java passes parameters by reference. Parameters can be object references and Java passes object references by value.
Peter Haggar
------------------
Senior Software Engineer, IBM
author of: Practical Java
[This message has been edited by Peter Haggar (edited March 23, 2001).]
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
Want to see where Java is and what Java looks like?
A Peek Look of Java, Indonesia
Enjoy!
Roseanne
[This message has been edited by Roseanne Zhang (edited March 23, 2001).]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Peter, I think it is a lot less confusing for beginners if you explain that Java always passes by value. When I teach my students I always stress to them that object names are actually pointers and once I show them my little example they have a clear understanding. I think anyhting else just leads to confusion.
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
Thomas
Agree with you 100% !!!
Peter
The more jargon you are using, the more confusing you are getting. I'm sorry to say, it also confuses yourself too.
Beautiful thing does not have to be complicated. Simple is beautiful.
Albert Einstein's Relativity is beautiful, and simple. The whole theory is based on one sentence. "Speed of light is constant relative to any inertial reference system." The theory expanded a little later, made it even simpler.
Thanks!
Roseanne
[This message has been edited by Roseanne Zhang (edited March 23, 2001).]
Peter Haggar
author
Ranch Hand

Joined: Jan 03, 2001
Posts: 106
Originally posted by Roseanne Zhang:

Peter
The more jargon you are using, the more confusing you are getting.

Didn't mean to be confusing....not using jargon either...just stating the facts.

I'm sorry to say, it also confuses yourself too.

Sorry, but I'm not confused. This issue usually trips up C++ programmers moving to Java. I agree that passing parameters by value is the simple solution and am glad that is the way Java does things. C++, with two parameter passing mechanisms, is not as simple.
Peter Haggar
------------------
Senior Software Engineer, IBM
author of: Practical Java
Nikhil Kumar
Greenhorn

Joined: Dec 17, 2000
Posts: 9
The thread is so interesting, I couldn't help opining.
Passing parameters by value, reference or name is a programming language semantics. Here is a language independent representation.
Procedure Declaration:
declare myProcedure with parameter X
Procedure Call:
call myProcedure using A
Following can happen depending on the way parameter passing is implemented in a programming language:
pass-by-value:
After the call to it, the procedure will have the value of A in X.
pass-by-reference:
After the call to it, the procedure will have a reference to A in X.
pass-by-name:
After the call to it, the procedure will have the name of A in X.

In Java, the language specification requires implementation of parameter passing using pass-by-value semantics.
Let's not confuse pass-by-reference with passing object-reference. We are talking about apples and oranges here. It helps to think that, what is contained in A is a handle to the object, not necessarily a pointer or a direct reference. It is the value of A i.e. a copy of the handle that gets passed to the procedure. The variable X in the procedure contains a copy of this handle.
Hope that brings the thread a little closer to closure.
Thanks,
Nikhil.
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

And I thought the original question had a straightforward answer. ;-)
I was intrigued by this notion of a "language independent" defintion for pass-by-value, which struck me as overkill. In a polymorphic language, it makes sense (to me, anyway) that the word under stress here is "value," and not the terms "primitive" and "reference."
A primitive behaves, more or less, as a register. So a primitive's value is some content, a literal, that adheres to its type definition. An object reference is also a register; it has some content, an address in memory, that also corresponds to its type definition, i.e., the correct kind of reference.
An object reference is indirect, but that doesn't distinguish it behaviorally from a primitive, or direct reference. Their contents are the things that get copied and passed into a called method's parameter list.
It seems to me if we had a truly "universal" definition for pass-by-value, we'd have missed the point, as each language is free either to define value as it likes or conform to a common definition. I think it's the exercise of the former that makes different languages able to emphasize different strengths.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide

[This message has been edited by Michael Ernest (edited March 29, 2001).]
 
jQuery in Action, 2nd edition
 
subject: pass-by-value