• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Pass by value or reference?

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Why the output is "OK" not "PK" as expected?
Thanks.
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because you are making str = PK, not s. You can resolve this two ways. Either make it edit the s string by making it static.



or by returning the str string.


[ April 02, 2006: Message edited by: Stephen Foy ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Java everything is pass-by-value. If you pass a reference to an object, that reference itself is passed by value. In other words, the method sees a copy of the pointer to the String object. If it changes its copy to point to another String, as here, this has no effect on the parameter back in the original method.
 
Qunfeng Wang
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Primitive arguments, such as an int or an array, are passed by value, the rest are passed by reference. When invoked, a method or a constructor receives the value of the variable passed in and the method cannot change its value."

This is a pagraph from java turorail. Is this a bug?
http://java.sun.com/docs/books/tutorial/java/javaOO/arguments.html
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Loius Wan:
"Primitive arguments, such as an int or an array, are passed by value, the rest are passed by reference. When invoked, a method or a constructor receives the value of the variable passed in and the method cannot change its value."

This is a pagraph from java turorail. Is this a bug?
http://java.sun.com/docs/books/tutorial/java/javaOO/arguments.html



Yes it is incorrect.
The lesson to be learned here is that Sun documentation is not authoritative - in fact, not even the language specification or vm specification is authoritative. The reference implementation is the only authority.

If you ever sit down to actually implement the spec., you'll get the gist
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java = pass by value.

The confusion happens because references are also passed by value.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even though references are pass by value, the parameter will "point to" the same object as the original reference variable. This means that you can modify the underlying object from a method and the change will be reflected in the caller. For exmaple,



However, in your example, you are creating a completely new object for the parameter to refer to. Also, Strings are immutable, so there is no way to amke a similar example using Strings.

Layne
 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Loius Wan:

Why the output is "OK" not "PK" as expected?
Thanks.



The method func has a void return type which means it won't return a value to the calling method.

Second - the method func is working with a formal parameter of type String called str. String s and String str are two different things. String str is dead when the method is finished executing, unless you assign it to some other class data member.

Third - the whole pass by value / pass by reference is easy to remember if you know that primitive data type variables hold the value of what ever you assign to that variable, while Reference data type variables hold the "memory address" (some people call this a pointer) to the value of whatever you assign to them. So when you pass a reference variable to a method, you pass the memory location of that reference variable and not the data in that reference variable. Make sense?

In your example, when you call the method func and pass it an actual parameter of s, what you are passing is the memory address of s and not the value in that memory address, which in this case is "OK".
 
leo donahue
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Layne Lund:
Even though references are pass by value, the parameter will "point to" the same object as the original reference variable. This means that you can modify the underlying object from a method and the change will be reflected in the caller.

Layne



Layne,

I know you know your stuff, but be careful how you use the word object, primitives aren't objects ( since your example was using an int primitive), and new people are easily confused when "we" know what we are trying to say but they are reading us literally.
[ April 04, 2006: Message edited by: leo donahue ]
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by leo donahue:

( since your example was using an int primitive)



By "object", he means the single instance of MyClass, to which there are two references. By "modifying the object" he means changing the value of one of its members. He didn't call an int an object.
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by leo donahue:
Third - the whole pass by value / pass by reference is easy to remember if you know that primitive data type variables hold the value of what ever you assign to that variable, while Reference data type variables hold the "memory address" (some people call this a pointer) to the value of whatever you assign to them.



I would hesitate to call the object a "value". A reference variable's value is the memory location of the object, not the object itself.

So when you pass a reference variable to a method, you pass the memory location of that reference variable and not the data in that reference variable. Make sense?



This is backwards. The value of the variable is passed, not the memory location of the variable. For a reference type the value is the memory location of an object on the heap.

(Yes, I know it can also be null, I'm assuming a 'context' in which the reference variable is not null.)
 
leo donahue
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ken Blair:


This is backwards. The value of the variable is passed, not the memory location of the variable. For a reference type the value is the memory location of an object on the heap.



Ken, you've contradicted yourself. If the value is the memory location of an object, what is the difference between what you just said and what I said?

Reference variables don't hold values, they hold references - which are memory addresses - and those addresses hold the actual value, and that is what I said. The value that is passed is the memory location of the object.

For primitive data types, the value that is passed is the actual value of the variable, so if you have an int variable such as:

int i = 10;

When you pass "i", you pass "10".

If you have a reference variable of type MyClass such as:

MyClass c = new MyClass();

When you pass "c", you pass the memory address of "c", not what is in "c" at "c's" memory address - whatever it is.

What if "c" had one million int members? Why would we want to pass all that stuff?

If you could pass what is in "c", the value held at the memory address, then why would we need reference variables at all?
[ April 04, 2006: Message edited by: leo donahue ]
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by leo donahue:

When you pass "c", you pass the memory address of "c", not what is in "c".



You do not pass the memory address of the variable "c". You pass the contents of the variable "c", which is the memory address of a MyClass object.

This statement is a repeat of what Ken was pointing out previously. The more we discuss something like this, the more careful you have to be with language. You're following the colloquial practice of using the name of the variable as a name for an object being referred to -- when we're having this kind of a discussion, you can't do that -- it leads to misunderstandings like this one.
 
leo donahue
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ernest Friedman-Hill:


You do not pass the memory address of the variable "c". You pass the contents of the variable "c", which is the memory address of a MyClass object.



Ok. I thought that is what I said.

Isn't "c" a reference variable of type MyClass? So what is the confusion about memory addresses?

int x = 30; what is in "x"? a memory address or the value of 30?

String str = "Java"; what is in str? a memory address or the value "Java"?

When you pass "str" to some method, are you passing "Java" or some memory address?
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by leo donahue:

Ok. I thought that is what I said.



I know that's what you thought you said -- it's clear to me that you understand the material perfectly and I didn't mean to imply otherwise. But in the dialect of English I (and I believe Ken, also) favor, "passing the address of 'c'" and "passing the address in 'c'" mean two entirely different things. If you use these interchangeably, then Ken and I -- and other speakers of this dialect -- will have a hard time understanding your precise meaning.
 
leo donahue
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought I was saying it just like this:
JavaRanch FAQ

"The method gets its own copy of the pointer, but it doesn't get its own copy of the object."

To me that says memory address. I'd like to understand the difference in the way other people view "of" and "in".
 
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"passing the address of 'c'"

To me that implies you are passing the address of the reference. Something analogous to a pointer to a pointer.

address --> address -->data

"passing the address in 'c'"

That is passing the address stored in c., which is the address where the data you need resides.

address -->data
[ April 04, 2006: Message edited by: Rusty Shackleford ]
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If your browser renders PRE blocks in monospace, this is a lovely ASCII-art diagram:


In the diagram above, the address "of" c is 0, while the address "in" c is 2. If you pass the variable c as a method argument, the "2" -- the value "in" c -- is copied. The value "0" -- the address "of" c -- is not relevant.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider that the variable "c" is a name associated with a value (some object address).

If you say "the address of c," then I would take this to mean the location of the name itself (probably on the stack). But if you say "the address in c," then I would take this to mean the associated value (a heap address for the object).
[ April 04, 2006: Message edited by: marc weber ]
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It all comes down to references having a name and objects not having a name.
Without a name, there is no simple way to describe an object, which leads to problems in communication. Of course, you could describe an object with indirection through its reference, but you must start with a name - that of the reference.

One cannot begin communication without an agreed common premise.
 
leo donahue
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ernest Friedman-Hill:

In the diagram above, the address "of" c is 0, while the address "in" c is 2. If you pass the variable c as a method argument, the "2" -- the value "in" c -- is copied. The value "0" -- the address "of" c -- is not relevant.



The book "Java Programming - from problem analysis to program design, 2nd Edition" by D.S. Malik says that reference variables are passed to methods using the "value of" reasoning. Anyone have this book? Chapter 7, page 391-392:


page 391:
Because a reference variable contains the address (that is, the memory location) of the actual data, both the formal and the actual parameters refer to the same object.

page 392:
Reference variables as parameters are useful in three situations:

1. When you want to return more than one value from a method.
2. When the value of the actual object needs to be changed.
3. When passing the address would save memory space and time, relative to copying a large amount of data.

 
reply
    Bookmark Topic Watch Topic
  • New Topic