I have this code
"123".getClass.asInstanceOf[Class[String]]
"123".getClass.asInstanceOf[classOf[String]]//compilation error, classOf not defined
However, I can use classOf in this way
println(classOf[String])
I am little confused here, what is the difference between classOf[T] and Class[T]
Many thanks in advance
Class[T]
is a type; classOf[T]
is a value of this type. So you can't use classOf[String]
as a type parameter (between [
and ]
), just as you can't write "123".getClass.asInstanceOf[new Object]
; and you can't use Class[T]
as a normal argument (between (
and )
), just as you can't write println(String)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With