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

Pass by value or reference?

Qunfeng Wang
Ranch Hand

Joined: Jan 28, 2005
Posts: 433

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


To be or not to be. It's a question.
Stephen Foy
Ranch Hand

Joined: Oct 17, 2005
Posts: 143
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 ]

Stephen Foy - Microsoft Application Development Consultant
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Qunfeng Wang
Ranch Hand

Joined: Jan 28, 2005
Posts: 433
"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
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
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


Tony Morris
Java Q&A (FAQ, Trivia)
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14339
    
  22

Java = pass by value.

The confusion happens because references are also passed by value.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
leo donahue
Ranch Hand

Joined: Apr 17, 2003
Posts: 327
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".


Thanks, leo
leo donahue
Ranch Hand

Joined: Apr 17, 2003
Posts: 327
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
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
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

Joined: Apr 17, 2003
Posts: 327
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
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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

Joined: Apr 17, 2003
Posts: 327
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
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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

Joined: Apr 17, 2003
Posts: 327
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".
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
"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 ]

"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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 ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
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

Joined: Apr 17, 2003
Posts: 327
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Pass by value or reference?