File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Regarding reference variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Regarding reference variable " Watch "Regarding reference variable " New topic
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: 3493
    
  13
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regarding reference variable