Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Arrays.asList() Vs new ArrayList()

 
Sathi Krishna
Ranch Hand
Posts: 51
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difference between following two code snippets:

1. List<T> list = new ArrayList<T>(Arrays.asList(arrayObj));
2. List<T> list = Arrays.asList(arrayObj);
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The key is in what Arrays.asList() creates. See the API: Which is here. From the method description you should recognize the major difference between the two.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1 gives you a reference to an ArrayList instance
2 gives you a reference to an instance of a class that implements the List interface.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic