If an Int can't be null, what does null.asInstanceOf[Int] mean?
Solution 1:
The behaviour of casting null
to an Int
depends on the context in which it is done.
First of all, if you cast a null
to an Int
, it actually means a boxed integer, whose value is null
. If you put the expression in a context where the expected type is Any
(which is translated to Object
behind 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 why println(null.asInstanceOf[Int])
prints null
.
However, if you use this same boxed integer value in a context where a primitive Int
(Java int
) is expected, it will be converted to a primitive, and null
is (as a default value for reference types) converted to 0
(a default value for primitive types).
If a generic method does this cast, then, naturally, you get a null
back.
However, if this method is specialized, then its return type is Int
(which is a primitive integer in this case), so the null: Any
value has to be converted to a primitive, as before.
Hence, running:
object Test extends App {
println(null.asInstanceOf[Int])
def printit(x: Int) = println(x)
printit(null.asInstanceOf[Int])
def nullint[T] = null.asInstanceOf[T]
println(nullint[Int])
def nullspecint[@specialized(Int) T] = null.asInstanceOf[T]
println(nullspecint[Int])
}
produces:
null
0
null
0
Solution 2:
Here's the thing: asInstanceOf
doesn't have to make sense. What this method does is to tell the compiler to STOP MAKING SENSE, and trust what you are saying.
Now, if you want to know why it returns 0, that's because asInstanceOf
works on AnyRef
, not on AnyVal
. When applied to an AnyVal
, it will use the boxed version instead, and a boxed null
has value 0.
Solution 3:
It looks like it's just automatically converting it to zero:
scala> null.asInstanceOf[Int]
res0: Int = 0
And, of course, 0, unlike null, can be an Int
.