This week's book giveaway is in the General Computing forum.
We're giving away four copies of Learning Regular Expressions and have Ben Forta on-line!
See this thread for details.
Win a copy of Learning Regular Expressions this week in the General Computing forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Unsuccessful downcast  RSS feed

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • 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?

 
Author and ninkuma
Marshal
Posts: 66806
168
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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: 90
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Posts: 66806
168
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does the method documentation say that it returns an ArrayList?
 
John Losty
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No it returns a List reference, but Can't I downcast it to ArrayList<String> ?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66806
168
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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: 90
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Posts: 66806
168
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!