Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference between classOf[T] and Class[T]

Tags:

scala

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

like image 799
Xiaohe Dong Avatar asked Oct 13 '14 11:10

Xiaohe Dong


1 Answers

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).

like image 147
Alexey Romanov Avatar answered Nov 11 '22 12:11

Alexey Romanov