| Author |
Regarding reference variable
|
kannan vinayagam Duraiswamy
Ranch Hand
Joined: Jan 12, 2009
Posts: 52
|
|
Code 1 :
In the above program the reference variable 'a' is added to the list 'l'.
So when processing the list, the list return the 'reference' which points the value "a".
Q : am i right?.
Then,
code 2 :
here the reference variable 'a' is reused again at line 1.
i.e., pointing the reference variable 'a' to a value "b" or else to a new string object "b".
again i add the reference variable to the list 'l'.
at last the list 'l' has two (same)reference variable.
so while processing the list 'l', how did the first value "a" is retrived as the reference varaible 'a' is pointed to a new string object "b" ?.
|
Kannan.DV
|
 |
amitabh mehra
Ranch Hand
Joined: Dec 05, 2006
Posts: 98
|
|
|
I think its because the actual object is added to the list and not the reference that points to that object.
|
 |
kannan vinayagam Duraiswamy
Ranch Hand
Joined: Jan 12, 2009
Posts: 52
|
|
i.e, the 'object' are stored in the list not the 'reference variable' .
|
 |
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
|
|
In the above program the reference variable 'a' is added to the list 'l'.
No. Java is pass by value, so a copy of the reference variable is added to the list. After the call to list.add, there will be two references to the String - 'a' and the one in the ArrayList.
Armed with that knowledge, what is happening in the rest of the program should now make sense. Let us know if it doesn't.
|
Joanne
|
 |
 |
|
|
subject: Regarding reference variable
|
|
|