Method parameters doubt?
posted 18 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hi Guys,
Consider the code below,
The above code prints 1, 3
And the code below,
prints 3, 1
Can you guys please explain this?
Thanks in advance.
Consider the code below,
The above code prints 1, 3
And the code below,
prints 3, 1
Can you guys please explain this?
Thanks in advance.
SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
posted 18 years ago
Notice in the first code that there is nothing within the method that has any effect on the array references sent to it.
In the second code, the elements of the arrays are actually swapped.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Originally posted by Jothi Shankar Kumar Sankararaj:
Hi Guys,
Consider the code below,
The above code prints 1, 3
And the code below,
prints 3, 1
Can you guys please explain this?
Thanks in advance.
Notice in the first code that there is nothing within the method that has any effect on the array references sent to it.
In the second code, the elements of the arrays are actually swapped.
posted 18 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I got the answer, arrays are passes by reference values.
Thanks keith.
Thanks keith.
SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Keith Lynn
Ranch Hand
Posts: 2412
posted 18 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
No, all parameters are sent by value.
When you use an array reference (or any other reference) as a parameter to a method, what gets sent to the method is a copy of the reference.
So that means when a method begins executing, the formal parameters of the method point to the same objects that the actual parameters do.
However, if you change where the formal parameter points, that does not change anything about where the actual parameter points.
[ December 06, 2006: Message edited by: Keith Lynn ]
When you use an array reference (or any other reference) as a parameter to a method, what gets sent to the method is a copy of the reference.
So that means when a method begins executing, the formal parameters of the method point to the same objects that the actual parameters do.
However, if you change where the formal parameter points, that does not change anything about where the actual parameter points.
[ December 06, 2006: Message edited by: Keith Lynn ]
posted 18 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hello Frenz..
Am also confused that java is pass by reference but its not...it is by PASS BY VALUE
Explanation:
when we pass parameters to a method...we are actually passing the "COPY OF REFERENCE VALUE i.e in bits"
so whatever the variable we are just passing the size(in bits) of that type.
Harish..
Am also confused that java is pass by reference but its not...it is by PASS BY VALUE
Explanation:
when we pass parameters to a method...we are actually passing the "COPY OF REFERENCE VALUE i.e in bits"
so whatever the variable we are just passing the size(in bits) of that type.
Harish..
Harish Paravasthu
| pie. tiny ad: The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |










