[英]Why doesn't double.class equal java.lang.Double.class?
Since the primitive double is represented with the java.lang.Double class, shouldn't double.class equal java.lang.Double.class? 由于原始double用java.lang.Double类表示,所以double.class不应该等于java.lang.Double.class吗? (This of course also happens on other primitive types too) (这当然也发生在其他原始类型上)
System.out.println(double.class == Double.class);
Output: 输出:
Result: false
Double.class
is the class object corresponding to the wrapper type Double
. Double.class
是与包装器类型Double
对应的类对象。 double
is actually not a class, but double.class
is the object used in reflection to indicate that an argument or return type has primitive type double
. double
实际上不是一个类,但double.class
是反射中使用的对象,表示参数或返回类型具有基本类型double
。
They are two distinct instances of Class<Double>
. 它们是Class<Double>
两个不同实例。 This doesn't normally happen (for ordinary objects), but for wrapper classes, that's exactly the way to distinguish between fields of the primitive type and the wrapper type in reflection. 这通常不会发生(对于普通对象),但对于包装类,这正是区分基本类型的字段和反射中的包装类型的方法。
double
is a primitive type, Double
is a class type. double
是原始类型, Double
是类类型。 There is a property on the Double
class called TYPE
: Double
类上有一个名为TYPE
的属性:
For example System.out.println(double.class == Double.TYPE);
例如System.out.println(double.class == Double.TYPE);
prints true. 打印真实。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.