• Post Reply Bookmark Topic Watch Topic
  • New Topic

Generic method ClassCastException  RSS feed

 
Ranch Hand
Posts: 175
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With generics, I would assume that the compiler is supposed to protect me from ClassCastException. In the code below, why can't the compiler see that the cast to an Integer is potentially dangerous?

 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Bishara wrote:With generics, I would assume that the compiler is supposed to protect me from ClassCastException. In the code below, why can't the compiler see that the cast to an Integer is potentially dangerous?


In general, generics and casting (explicitly) don't mix. If you are finding that you need to explicitly cast while using generics, you may be using it incorrectly. I would recommend not using casting with generics -- or if you must use casting, assume that generics may be broken.

Henry
 
Greenhorn
Posts: 16
AngularJS Chrome jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're forcing a cast at line 3. Not sure why casting and generics have been mixed up.

-- Srikkanth
 
Joe Bishara
Ranch Hand
Posts: 175
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:if you must use casting, assume that generics may be broken

Thanks.
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are passing a String and then trying to cast it. How are you going to cast a String to anything else (except Object)?

If you want to cast to an Integer, you are saying that the parameter will always be an Integer. The only thing you can cast to Integer is Integer. So change the parameter type to Integer.
 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since the code given is relatively trivial, I think the specifics are beside the point. Henry explains it best: generics and casting don't mix. Why would you declare in the method signature that "t can be just about anything" and then turn around and say in the method, "but I know it's really going to be an Integer"?
 
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's no different than doing:


For both this code, and the generics one above, you are essentially casting an Object to an Integer.
And neither one is illegal, and both will result in a ClassCast...
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say that programming and casting reference types don't mix. Unfortunately there are instances where casting is unavoidable.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I would say that programming and casting reference types don't mix. Unfortunately there are instances where casting is unavoidable.

I agree. Casting should always be treated as suspicious unless otherwise proven necessary.
 
Joe Bishara
Ranch Hand
Posts: 175
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks @Campbell and @Junilu for your expert advice. This thread and this thread have made me realize just how evil casting can be. I will avoid it unless it is unavoidable.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!