Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Generics - Casting to "null's class"

 
Bob Hunt
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I came across an interesting situation.
I've got a method that gets a class as a parameter so the caller method doesn't have to cast, because the called method will have the same return type as the class parameter's type. Here is a simplified version of that.



When I pass I get 10. When I pass "null" I still get 10. When I write I get a compiler error, of course.

My question: What happens when I pass null as a class and the JVM try to cast 10 to null in runtime?
 
Greg Charles
Sheriff
Posts: 2985
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't really know how Java handles null in that case, but I do know it's not at run time. In Java, both casting and generics are seen by the compiler only. They have no effect on the running program.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic