• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Basics of Arrays.asList()

 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I don't understand why the line nr. 9 doesn't compile?

What is the return type of the Arrays.asList() method?
 
Ranch Hand
Posts: 127
2
Monad Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you consult the Java docs you'll note that Arrays.asList(...) returns a List<T>.



from Arrays
 
Gerard Gauthier
Ranch Hand
Posts: 127
2
Monad Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also note: ArrayList<T> is not fix in size while Arrays.asList(...) is fixed size.
 
John Losty
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can a List be returned? I thought List is an interface.

The reference to a List cannot exist since it cannot be created with
 
Sheriff
Posts: 17341
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A variable declared as a List can be assigned a reference to any object that implements List. In other words, any object that implements List is a List and therefore can be assigned to a reference variable of that type.

It is good practice to declare a reference variable using an interface type like List, if possible. This makes your code rely on the contract of behavior defined by the interface, not the implementation.

This is actually the basis of design principles like the Interface Segregation Principle and the Dependency Inversion Principle from SOLID. In particular, the DIP says that you should "depend on abstractions, not concretions." A List is an abstraction whereas an ArrayList is a concretion (a specific implementation).
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Losty wrote:
What is the return type of the Arrays.asList() method?



As said by Junilu, it returns a List<Integer> in the examples you give.
The actual underlying type is irrelevant, and quite likely not public so you wouldn't be able to declare your reference as one anyway.

Indeed, looking at the Arrays source code and it does use its own private class.
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:Indeed, looking at the Arrays source code and it does use its own private class.



And the private class is named ArrayList.

But it isn't java.util.ArrayList. It is java.util.Arrays.ArrayList.

 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yessss.
It wouldn't have been my choice of name!
I got a bit confused when I looked at the code, and even went to look at the ArrayList source code to see if there was a constructor I'd never noticed.
 
John Losty
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so java.util.Arrays.ArrayList implements List?
 
Junilu Lacar
Sheriff
Posts: 17341
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this implementation: http://www.docjar.com/html/api/java/util/Arrays.java.html

and in http://www.docjar.com/html/api/java/util/AbstractList.java.html

so, yes, it implements List.
 
Saloon Keeper
Posts: 5174
207
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not surprising, otherwise OP would have faced an Exception in his opening post code (line 8).
 
Any sufficiently advanced technology will be used as a cat toy. And this tiny ad contains a very small cat:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic