Why doesn't double.class equal java.lang.Double.class?
Asked Answered
E

3

7

Since the primitive double is represented with the java.lang.Double class, shouldn't double.class equal java.lang.Double.class? (This of course also happens on other primitive types too)

System.out.println(double.class == Double.class);

Output:

Result: false
Ecbatana answered 31/7, 2015 at 21:35 Comment(4)
they're different class literalsLumpish
java.lang.Double wraps/boxes a double, but it’s not the same.Thermoscope
@Trojaner Because these are different types that don’t behave the same way. For example, you cannot define a List<double>, but you can define a List<Double>.Thermoscope
(Same reason as a double is not a Double.)Rolland
T
13

Double.class is the class object corresponding to the wrapper type 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.

Tanguay answered 31/7, 2015 at 21:37 Comment(0)
A
4

They are two distinct instances of 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.

Almost answered 31/7, 2015 at 21:49 Comment(0)
S
3

double is a primitive type, Double is a class type. There is a property on the Double class called TYPE:

For example System.out.println(double.class == Double.TYPE); prints true.

Solitude answered 31/7, 2015 at 21:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.