java 布尔值.class?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1019208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
boolean.class?
提问by Dave
I noticed the other day that I can call boolean.class, but not integer.class (or on other primitives). What makes boolean so special?
前几天我注意到我可以调用 boolean.class,但不能调用 integer.class(或其他原语)。是什么让布尔值如此特别?
Note: I'm talking about boolean.class, not Boolean.class (which would make sense).
注意:我说的是 boolean.class,而不是 Boolean.class(这是有道理的)。
Duh: I tried integer.class, not int.class. Don't I feel dumb :\
Duh:我试过 integer.class,而不是 int.class。我不觉得自己很笨吗:\
回答by glmxndr
Not integer.classbut int.class. Yes you can. JRE 6 :
不integer.class但是int.class。是的你可以。JRE 6:
public class TestTypeDotClass{
public static void main(String[] args) {
System.out.println(boolean.class.getCanonicalName());
System.out.println(int.class.getCanonicalName());
System.out.println(float.class.getCanonicalName());
System.out.println(Boolean.class.getCanonicalName());
}
}
outputs
输出
boolean
int
float
java.lang.Boolean
回答by Tom Hawtin - tackline
You can do int.class. It gives the same as Integer.TYPE.
你可以做到int.class。它给出与Integer.TYPE.
int.class.isPrimitive(), boolean.class.isPrimitive(), void.class.isPrimitive(), etc., will give a value of true. Integer.class.isPrimitive(), Boolean.class.isPrimitive(), etc., will give a value of false.
int.class.isPrimitive()、boolean.class.isPrimitive()、void.class.isPrimitive()等将给出 的值true。Integer.class.isPrimitive()、Boolean.class.isPrimitive()等,将给出 的值false。
回答by jitter
Well you can do something like int.classas well
那么你可以这样做int.class,以及
System.out.println(int.class);
The .class keyword was introduced with Java 1.1 to have a consistent way to get the class object for class types and primitive data types.
.class 关键字是在 Java 1.1 中引入的,它以一致的方式获取类类型和原始数据类型的类对象。
回答by Bill the Lizard
boolean isn't special. You can call
布尔值并不特别。你可以打电话
int.class
for example. All of the primitive types have this literal. From Sun's Tutorial:
例如。所有原始类型都有这个字面量。来自 Sun 的教程:
Finally, there's also a special kind of literal called a class literal, formed by taking a type name and appending ".class"; for example, String.class. This refers to the object (of type Class) that represents the type itself.
最后,还有一种特殊的字面量,称为类字面量,由类型名称和附加“.class”组成;例如,String.class。这是指代表类型本身的对象(类型为 Class)。
回答by bedla.czech
Maybe dumb continuation, but why is possible to assign boolean.class to Class<Boolean>, although hashCodes are different?
也许是愚蠢的延续,但为什么可以将 boolean.class 分配给 Class<Boolean>,尽管 hashCodes 不同?
final Class<Boolean> c = boolean.class;
System.out.println("c := "+c);
System.out.println("boolean.class := "+boolean.class);
System.out.println("Boolean.class := "+Boolean.class);
System.out.println("boolean.class == Boolean.class := "+(boolean.class == Boolean.class));
System.out.println("boolean.class.equals(Boolean.class) := "+boolean.class.equals(Boolean.class));
System.out.println("boolean.class.hashCode := "+boolean.class.hashCode());
System.out.println("Boolean.class.hashCode := "+Boolean.class.hashCode());

