As a test, I wrote this code:
object Ambig extends App {
def f( x:Int ) { println("Int" ) }
def f( x:String ) { println("String") }
f( null.asInstanceOf[Int ] )
f( null.asInstanceOf[String] )
f(null)
}
I was expecting to get an error on that last invocation of f(), saying that it was ambiguous. The compiler accepted it, and produced this output:
Int
String
String
Now I’m guessing that this has to do with the fact that Int is not an AnyRef, so the only version of f that works for f(null) is f(x:String). But then, if an Int can’t be null, what does null.asInstanceOf[Int] mean? The repl says it’s of type Int:
scala> :type null.asInstanceOf[Int]
Int
but I don’t really see how that works. After all, if I try to cast a String to an Int, all hell breaks loose:
scala> "foo".asInstanceOf[Int]
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at scala.runtime.BoxesRunTime.unboxToInt(Unknown Source)
...
Of course that’s to be expected — “foo” can’t be made into an Int. But neither can null, so why does casting null to an Int work? Presumably boxing in some form, but the type is still Int, which can’t be null…
What am I missing?
The behaviour of casting
nullto anIntdepends on the context in which it is done.First of all, if you cast a
nullto anInt, it actually means a boxed integer, whose value isnull. If you put the expression in a context where the expected type isAny(which is translated toObjectbehind the scene, because in the JVM bytecode, there is no way to refer to a primitive type and a reference type with the same reference), then this value is not converted further – that is whyprintln(null.asInstanceOf[Int])printsnull.However, if you use this same boxed integer value in a context where a primitive
Int(Javaint) is expected, it will be converted to a primitive, andnullis (as a default value for reference types) converted to0(a default value for primitive types).If a generic method does this cast, then, naturally, you get a
nullback.However, if this method is specialized, then its return type is
Int(which is a primitive integer in this case), so thenull: Anyvalue has to be converted to a primitive, as before.Hence, running:
produces: