• 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

Unsuccessful downcast

 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This code snippet throws cannot cast (java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList) error? Do you know the reason?

 
Sheriff
Posts: 67682
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should be coding to interfaces, not implementations.

So:should be
Then remove the cast and see what happens.
 
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

Then remove the cast and see what happens.

I understand it works but I am not asking how to fix it but why I cannot use this:


Arrays.AsList returns List reference so I am asking why I cannot downcast it and save to ArrayList<String> varable?
 
Bear Bibeault
Sheriff
Posts: 67682
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does the method documentation say that it returns an ArrayList?
 
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
No it returns a List reference, but Can't I downcast it to ArrayList<String> ?
 
Bear Bibeault
Sheriff
Posts: 67682
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. The method returns a List implementation. It doesn't say which implementation will be used. Your code assumes, incorrectly, that an ArrayList will be used.

Your code can make no assumptions about the List implementation. All it knows is that the class used will implement List.
 
Bartender
Posts: 1845
10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could, if it was an ArrayList.
But it's not actually an ArrayList.
Or specifically not a java.util.ArrayList. It's java.util.Arrays$ArrayList
It's a different implementation of the List interface.
But should you really care?

In practice you very rarely want to downcast from an Interface to an Implementation.
Can you do anything with an "ArrayList" that you can't do with a "List"?

 
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

Can you do anything with an "ArrayList" that you can't do with a "List"?


You can't do anything with List because it is an interface.

Or specifically not a java.util.ArrayList. It's java.util.Arrays$ArrayList


What is the difference between does two?
 
Bear Bibeault
Sheriff
Posts: 67682
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Losty wrote:

Can you do anything with an "ArrayList" that you can't do with a "List"?


You can't do anything with List because it is an interface.



Ummm, incorrect. When you have the reference to the interface, you can perform any action defined by that interface. Not sure where you got that idea, but it's 100% wrong. Check out the java.util.List interface and see all that you can do with it.

What is the difference between does two?


They are completely different classes. They both implement List, but other than that, they are distinct classes.
reply
    Bookmark Topic Watch Topic
  • New Topic