File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Java Generics - Casting to Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java Generics - Casting to "null Watch "Java Generics - Casting to "null New topic

Java Generics - Casting to "null's class"

Bob Hunt

Joined: Mar 26, 2010
Posts: 5

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

Joined: Oct 01, 2001
Posts: 2968

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.
I agree. Here's the link:
subject: Java Generics - Casting to "null's class"
jQuery in Action, 3rd edition