This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

compiler inconvertible types vs ClassCastException

 
Radu Grigoras
Greenhorn
Posts: 3
  • 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
Posts: 4181
21
IntelliJ IDE Java Python
  • 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
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Radu Grigoras
Greenhorn
Posts: 3
  • 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
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it's clear. Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic