Forums Register Login

Java Generics - Casting to "null's class"

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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.
You guys wanna see my fabulous new place? Or do you wanna look at this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 2524 times.
Similar Threads
Generics doubt
Generic + Reflection - Why following code fails ?
Generic Singleton Class
Should @SuppressWarnings be used?
Transforming a class instance to fit into the generic bracket
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 13:33:51.