I used IntelliJ's ability to convert Java code to Scala code which generally works quite well.
It seems that IntelliJ replaced all casts with calls to asInstanceOf
.
Is there any valid usage of asInstanceOf[Int]
, asInstanceOf[Long]
etc. for value types which can't be replaced by toInt
, toLong
, ...?
I do not know of any such cases. You can check yourself that the emitted bytecode is the same by compiling a class like
and using
javap -c Conv
to getwhere you can see that the exact same bytecode is emitted in each case.
Well,
toInt
andtoLong
are not casts. The correct conversion of type casting isasInstanceOf
indeed. For example: