pascal_lalonde
asked on
What is the keyword for modifiable method parameter in Java?
Hello experts,
An easy question from a newbie to Java. In C sharp, I use the keyword ref or out
when one method parameter is not a copy but the true object passed to the method.
How does it work in Java? PLease post a snippet of code.
Thank you!
An easy question from a newbie to Java. In C sharp, I use the keyword ref or out
when one method parameter is not a copy but the true object passed to the method.
How does it work in Java? PLease post a snippet of code.
Thank you!
All method parameters are passed by *value*. There is no pass by reference in Java
If the parameter is a "primitive variable" (int, byte, long, basically any lowercase type) then Java automatically passes it by value. This cannot be changed.
If the parameter is a "object variable" (JFrame, Integer, basically any uppercase type) then Java automatically passes it by reference. This cannot be changed.
If you need to pass an integer by reference, you can make an object to wrap the integer around.
If the parameter is a "object variable" (JFrame, Integer, basically any uppercase type) then Java automatically passes it by reference. This cannot be changed.
If you need to pass an integer by reference, you can make an object to wrap the integer around.
>>then Java automatically passes it by reference. This cannot be changed.
It doesn't. It passes it by value. If it did, you *would* be able to change it - that's the definition of 'by reference'. It *is* a reference, but it's a copy of one.
It doesn't. It passes it by value. If it did, you *would* be able to change it - that's the definition of 'by reference'. It *is* a reference, but it's a copy of one.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
CEHJ your answer is right but imcomplete. objects gave a complete answer and easy to understand for a newbie. I mentionned I am a newbie and that I wanted a snippet of code. Thank you both.
pascal_lalonde, can you tell us why you ignored all comments there but one (part of which didn't even seem relevant)?
ASKER
Hello CEHJ,
I ignored the comments from sciuriware and Thomas4019 because they contain an error. No method parameters are passed by reference in Java. Then only your comments and those from object remain.
I indicated above how I discriminated between both of you. The answer if fully relevant anc complete.
I wanted to give points for a full answer not a partial and fast answer.
Regards.
I ignored the comments from sciuriware and Thomas4019 because they contain an error. No method parameters are passed by reference in Java. Then only your comments and those from object remain.
I indicated above how I discriminated between both of you. The answer if fully relevant anc complete.
I wanted to give points for a full answer not a partial and fast answer.
Regards.
glad I could help :-)
unless those are simple values, then those are copies.
There is no keyword for it,
and a ReadOnly parameter doesn't exist in JAVA.
;JOOP!