<分区>
int.class
或 double.class
这样的类有什么用
例如你不能做类似的事情
Object val=getValue();
if(val instanceof int){
}
或
int i = new int();
是的,我知道这是不可能的,因为 int 不是 Object 类型。但是,需要那些原始类有什么用呢?我在 Oracle documentation. 中没有看到任何对此的明确解释
<分区>
int.class
或 double.class
这样的类有什么用
例如你不能做类似的事情
Object val=getValue();
if(val instanceof int){
}
或
int i = new int();
是的,我知道这是不可能的,因为 int 不是 Object 类型。但是,需要那些原始类有什么用呢?我在 Oracle documentation. 中没有看到任何对此的明确解释
最佳答案
它对反射(reflection)很有用。
例如,如果你想使用 Class.getDeclaredMethod
来查找一个将 int
作为参数的方法,你可以这样做:
//Find the method foo(int)
Method theMethod = theClass.getDeclaredMethod("foo", int.class);
关于java - Java 中的原始类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31011322/