File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

compiler inconvertible types vs ClassCastException

 
Radu Grigoras
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

A few days ago a stumbled upon tricky SCJP question and I couldn't find the answer . Why the compiler won't let me cast a String object as it would for an ArrayList object?


Thanks.
 
Steve Luke
Bartender
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should note that the type of the reference that the Compiler does not Catch is List, not ArrayList. Do you see why that may make a difference?
 
Campbell Ritchie
Sheriff
Pie
Posts: 47286
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Radu Grigoras
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks

Exactly that the difference:

interface Interf {}
...
Interf i =new Interf(){};
A a= (A)i;//class cast ex
a= (A) new Interf(){}; //compiler inconvertible types

Thanks .
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So do you understand why it behaves that way? Or are you still having trouble with it?
 
Radu Grigoras
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it's clear. Thanks
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic