File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

List Assignment vs. List.addAll()

 
Tom Henricksen
Author
Ranch Hand
Posts: 136
Eclipse IDE jQuery Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difference between these two methods of getting a list.


And this code.


Is there any difference in these. I ask as I tried the first approach with problems. When I tried the second it worked.

Thanks,
Tom
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The list you create with "new" in the first version is simply discarded when you make the assignment. Assigning to a Java variable in Java always completely discards any existing value without affecting it in any way. In this first version, you end up with localList pointing to the exact same List that object.getAList() returned -- meaning that if "object" still has a reference to that List, code in "object" could modify that list, and the changed would be visible via localList, and vice-versa, because they reference the same object.

In the second version, the elements of "object"'s list are put into a new list. Now either object can chnage their own list independently, without affecting the other one.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The difference in pictures:



 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic