To be or not to be. It's a question.
Stephen Foy - Microsoft Application Development Consultant
To be or not to be. It's a question.
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
Tony Morris
Java Q&A (FAQ, Trivia)
Originally posted by Loius Wan:
Why the output is "OK" not "PK" as expected?
Thanks.
Thanks, leo
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
Thanks, leo
Originally posted by leo donahue:
( since your example was using an int primitive)
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.
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?
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.
Thanks, leo
Originally posted by leo donahue:
When you pass "c", you pass the memory address of "c", not what is in "c".
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.
Thanks, leo
Originally posted by leo donahue:
Ok. I thought that is what I said.
Thanks, leo
"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
"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
Java Q&A (FAQ, Trivia)
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.
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.
Thanks, leo