如何在Scala中获取Java的int.class?

San*_*dro 7 java reflection scala

我正在使用Scala与Java的反射API进行一些工作.但我似乎无法弄清楚如何在Scala中访问Java中的内容:int.class,float.class,boolean.class.

基本上是表示原始数据类型的类对象.

那么什么是int.class的Scala版本?

Eug*_*ako 15

Welcome to Scala version 2.10.0-20120430-094203-cfd037271e (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_25).
Type in expressions to have them evaluated.
Type :help for more information.

scala> classOf[Int]
res0: Class[Int] = int

scala> classOf[Integer]
res1: Class[Integer] = class java.lang.Integer
Run Code Online (Sandbox Code Playgroud)


Chr*_*s B 14

int.class,float.class等不存在.等效的盒装类型每个都有一个称为TYPE基本类型的静态字段.你是这个意思吗?

例如对于int/Integer:

http://docs.oracle.com/javase/6/docs/api/java/lang/Integer.html#TYPE

如果是这样,你就像在Java中一样从Scala引用它:

scala> Integer.TYPE
res0: java.lang.Class[java.lang.Integer] = int
Run Code Online (Sandbox Code Playgroud)