Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Call by Reference and Call by Value Question

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Fred.
 
Fred Hamilton
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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"
 
Marshal
Posts: 79642
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All problems in my mind is resolved.

Thanks a lot.
 
Fred Hamilton
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
reply
    Bookmark Topic Watch Topic
  • New Topic