wood burning stoves 2.0*
The moose likes Java in General and the fly likes Call by Reference and Call by Value Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Call by Reference and Call by Value Question" Watch "Call by Reference and Call by Value Question" New topic
Author

Call by Reference and Call by Value Question

alp carsikarsi
Ranch Hand

Joined: Dec 27, 2005
Posts: 40
Hello everybody,

I have a question. If someone says "In some programming languages there are two ways to pass arguments to methods. One is "Call by Reference" and the other is "Call by Value". But Java hasn't got this property. " What would you say ?

Best Regards.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

alp carsikarsi wrote:Hello everybody,

I have a question. If someone says "In some programming languages there are two ways to pass arguments to methods. One is "Call by Reference" and the other is "Call by Value". But Java hasn't got this property. " What would you say ?

Best Regards.


I would agree. Java has only one way to pass arguments to a method. Which way is that?


Steve
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Yeah, technically there is only one way, like Steve says. Since Steve didn't give the answer, I won't either.

IMO it is a bit of semantics. Hard to explain without giving the answer, but to say "there is only one way" is technically correct but leaves a lot unsaid. Just as there are essentially two data types, primitives and reference types, there are two ways of interpreting that "one way" in which arguments are passed.
alp carsikarsi
Ranch Hand

Joined: Dec 27, 2005
Posts: 40
I read some articles and found this one.
From "DEITEL Java How to Program International Edition 5th Edition" :

".... arrays are passed to methods by reference - a called method can access the elements of the caller's original arrays. The name of an array variable is actually a reference to an object that contains the array elements and the lenght field, a constant that indicates the number of elements in the array.
..
...
Although entire arrays and objects referred to by individual elements of reference-type arrays are passed by reference, individual array elements of primitive types are passed by value exactly as simple variables are. "

In conclusion, it says that in Java both call bye reference and call by value is possible.

Are they wrong ? Could you be more clear ?
I need an technical explanation
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
I wouldn't get hung up on the semantics too much. It's what happens in practice that is really important.

With primitives as arguments, you are passing values, and those values are copies of the actual data.

With objects, including arrays, you are also passing values, but those values are references to the object (data), rather than the object (data) itself.

That's the nutshell version. There's more being said here than is immediately obvious. Make sure you understand that with primitives, the original data in the calling method cannot change. With objects, the original data in the calling method most certainly can change.

That's my way of looking at it, someone else probably has another way.
alp carsikarsi
Ranch Hand

Joined: Dec 27, 2005
Posts: 40
Thanks a lot Fred.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Sure. Also consider that there are basically two types of variables, primitive variables and reference variables. With primitives, the contents of the variables are values, furthermore those values are the actual data. With reference variables (object variables) the contents are also values, but those values are not the actual object data, rather they are references (pointers) to the actual object data.

It's done this way in part because with primitives, we know how much memory will be required for the data. We don't know in advance how much memory will be require by an object of a certain type
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

alp carsikarsi wrote:I read some articles and found this one.
From "DEITEL Java How to Program International Edition 5th Edition" :

".... arrays are passed to methods by reference - a called method can access the elements of the caller's original arrays. The name of an array variable is actually a reference to an object that contains the array elements and the lenght field, a constant that indicates the number of elements in the array.
..
...
Although entire arrays and objects referred to by individual elements of reference-type arrays are passed by reference, individual array elements of primitive types are passed by value exactly as simple variables are. "

In conclusion, it says that in Java both call bye reference and call by value is possible.

Are they wrong ? Could you be more clear ?
I need an technical explanation


Can you point to he reference that says that? Because it most certainly is wrong to say that arrays are passed by reference. Everything in Java is pass by value, as Fred said. The difference between 'passing a reference by value' like Java does, and 'pass by reference' can be seen in this code:


The output is:


If Java had 'pass by reference' then we would be able to change the reference - assign it to point to another Object, and have that change reflected in the calling method. The output would be:


Instead, Java passes the reference by value. Which means the method gets a copy of the reference. The method can change the copy all it wants, but it does not affect the reference the calling method has.

It seems like we may be talking semantics but it has a lot of effects on the expected behavior of methods and parameter passing.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Steve Luke wrote:

...

Instead, Java passes the reference by value. Which means the method gets a copy of the reference. The method can change the copy all it wants, but it does not affect the reference the calling method has.

It seems like we may be talking semantics but it has a lot of effects on the expected behavior of methods and parameter passing.


Point taken Steve, we are in agreement but, when we look at a situation where we are passing a value that is a reference, and we say that this is not pass by reference. Then it's kind of a fine line whether or not it is semantics. I guess "pass a reference" really means something different than "pass by reference"
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
Steve Luke wrote:Can you point to the reference that says that?
I have the Deitel book, but I have the 6th edition: p 304:
When an argument to a method is an entire array or an individual array element of a reference type, the called method receives a copy of the reference. However, when an argument to a method is an individual array element of a primitive type, the called method receives a copy of the element's value.
. . . p 306:
Unlike some other languages, Java does not allow programmers to choose pass-by-value or pass-by-reference--all arguments are passed by value.
. . .
Although an object's reference is passed by value, a method can still interact with the references object by calling its public methods using the copy of the object's reference.
Although it is possible to confuse the two means of passing from the Deitel book, the newer edition on page 306 is quite clear about pass-by-value. I could probably find a 5th edition in our library, if that is actually helpful.
[edit]5th edition no longer in our library[/edit]
alp carsikarsi
Ranch Hand

Joined: Dec 27, 2005
Posts: 40
All problems in my mind is resolved.

Thanks a lot.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Campbell Ritchie wrote:
Steve Luke wrote:Can you point to the reference that says that?
I have the Deitel book, but I have the 6th edition: p 304:
When an argument to a method is an entire array or an individual array element of a reference type, the called method receives a copy of the reference. However, when an argument to a method is an individual array element of a primitive type, the called method receives a copy of the element's value.
. . . p 306:
Unlike some other languages, Java does not allow programmers to choose pass-by-value or pass-by-reference--all arguments are passed by value.
. . .
Although an object's reference is passed by value, a method can still interact with the references object by calling its public methods using the copy of the object's reference.
Although it is possible to confuse the two means of passing from the Deitel book, the newer edition on page 306 is quite clear about pass-by-value. I could probably find a 5th edition in our library, if that is actually helpful.
[edit]5th edition no longer in our library[/edit]


We all agree on the means. Most books are pretty good at describing the how. But the point remains that is not the means of passing that confuses most people, It's the terminology. People are confused about the meaning of words "Pass by reference" and "Pass a reference", not by the way the actual way the passing takes place.

edit: unless the confusion is about the meaning of the word reference itself, perhaps that is more likely.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Call by Reference and Call by Value Question
 
Similar Threads
java pass by copy, or pass by reference?
Exam Cram: page#112, Chapter #6, Question#3
Books on internals of Java
java parameter passing
Does java working in which basis?