Parameter Pass by Reference
Hi,
How to pass parametes by reference in java...? what is the keyword.
thanx.
-Arjun
[97 byte] By [
Arjun] at [2007-11-11 7:18:59]

# 1 Re: Parameter Pass by Reference
In java , u cannot pass an argument explicitly as reference using any keyword.
All primitive/fundamental data types are always passed as values only, while arrays and objects are passed always as references.
So if u want to pass a primityve type of data as refernce, convert into string and pass. Then in the function convert back to the type. Or else convert the values into equivalent objects of the respective wrapper calss and then pass the object.
eg.
add(Integer x, Integer Y) instead of add(int. int)
while calling convert your int variable to Integer type by calling the constructor of Integer class and to convert the Integer object to int, use intValue() method of that class.
# 3 Re: Parameter Pass by Reference
I know I am a little late but I would want to clarify a point here.
Java does not have a pass by reference. What is loosely referred to as a "pass-by-reference" is actually a pass-by-value. Java always passes by value. When you pass an object, you only pass a copy of the reference to that object. This would actually mean passing a reference by value. It is not the same as passing a reference.
Check these links
http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html
http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#38698
# 4 Re: Parameter Pass by Reference
Another way to say this ...
Arguments/parameters which are primitive data types are passed "by value"
Access to any other argument/parameter is given by a "shallow copy" of a "reference" to the object ... the "value" of the reference is the address where you get access to the object. You don't get your own copy of the object, you share access to the existing object.
nspils at 2007-11-11 22:41:50 >
