| Author |
Pass By Values / Pass By reference
|
Sarath Koiloth Ramath
Ranch Hand
Joined: May 07, 2008
Posts: 52
|
|
Hi frnds, I read the k&B passing the variables and object.But land up with lots of doubts.Can any one please help me to understand the idea behing the pass by reference and pass by values.Is java using pass by values Or java using both.please give a good description Thank
|
J 4 Java
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12907
|
|
Java is strictly pass-by-value. What confuses people is that Java passes references by value too, which is not the same as pass-by-reference. See this JavaRanch Campfire Story: Pass-by-Value Please [ May 22, 2008: Message edited by: Jesper Young ]
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Tusshar Fasate
Ranch Hand
Joined: May 21, 2008
Posts: 81
|
|
Hi Sarath, Java use only pass by value. and it doesnt use pass by reference. here example static void ranch(int a) { int b=b+a } in...main { int x=9; ranch(x); } here value of x is copied to variable a and whatever operaions made on a will not be reflected in x. But its little confusing about array or any object refernce. static void ranch(Test t)//Test is any class { } in main... { Test t1=new Test(); ranch(t1); .... ...} here also only one object is created and reference is sent to method ranch so whatever operation made in ranch method will reflect to original object.its also call by value but bit of confusing. Tushar... Pune India
|
 |
Sarath Koiloth Ramath
Ranch Hand
Joined: May 07, 2008
Posts: 52
|
|
Hi Tusar, I think i got it.i will try to explain. While passing a primitive variable ,Java is passing the copy of the variable.when we pass an object, the reference(that may be an address reference,but pointing to the same object) is passing.hence we say pass by value.So any change made will be reflected in the original object.I am correct...
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9939
|
|
Originally posted by Sarath Koiloth Ramath: So any change made will be reflected in the original object.
it depends on what you mean by 'any change'. if i do this:
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Sarath Koiloth Ramath
Ranch Hand
Joined: May 07, 2008
Posts: 52
|
|
Ok Thanks..I got it... Thanks once again tushar,Jesper ,and fred...
|
 |
 |
|
|
subject: Pass By Values / Pass By reference
|
|
|