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.
Steve
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
Steve
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.
I have the Deitel book, but I have the 6th edition: p 304:Steve Luke wrote:Can you point to the reference that says that?
. . . p 306: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.
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.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.
Campbell Ritchie wrote:
I have the Deitel book, but I have the 6th edition: p 304:Steve Luke wrote:Can you point to the reference that says that?
. . . p 306: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.
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.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.I could probably find a 5th edition in our library, if that is actually helpful.
[edit]5th edition no longer in our library[/edit]
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |