Hi
Simple types (like int, char, double ...) are passed by value (if you
change the value of the argument, the change is not propagated to the
calling code).
... public void dummy( int myInt ){
myInt++;
}
...
int i = 10;
dummy( i );
// i is still 10
...
Objects are passed by "reference". In java, each object is referenced by
a "handle". A "handle" is an integer value uniquely identifying the
object. So if you change the value of the argument, the change is not
propagated to the calling code. On the other hand, all the changes done
to the object (to the values of its members) are, of course, propagated.
(In fact there is a unique mechanism: the reference of the object is
passed as value.)
... public void dummy( person p ){
p.setFirstName("James");
p = new Person("John", "Smith");
}
...
Person prs = new Person("John", "Doe");
dummy(prs);
// Here the person is the same instance as the the one created here
// but it would contain "James Doe"
...
(Of course, you cannot change the members of some classes, like the
java.lang.String).
Hope it helps,
Mihai
On 23/04/2012 07:07, raghavendra wrote:
Does Java supports pass by reference or pass by value?Plse explain
with example...
thanks
raghavendra
--
You received this message because you are subscribed to the Google Groups
"JPassion.com: Java Programming" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/jpassion_java?hl=en.