Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Unsuccessful downcast  RSS feed

 
John Losty
Ranch Hand
Posts: 76
  • 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?

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
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: 76
  • 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: 66207
151
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: 76
  • 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: 66207
151
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.
 
Stefan Evans
Bartender
Posts: 1836
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: 76
  • 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: 66207
151
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!