I read that primitive Java types (boolean
, byte
, char
, short
, int
, long
, float
, and double
) and the keyword void
are also represented as class Class
objects. Then it means that int
is an object of class Class
then how come following statement doesn't throw error because .class
is only used with class name?
Class c = int.class
int
is a numeric type.int.class
is a class literal.A class literal is an expression consisting of the name of a class, interface, array, or primitive type, or the pseudo-type
void
, followed by a.
and the token class.
ClassLiteral
:
TypeName {[ ]} . class
NumericType {[ ]} . class
boolean {[ ]} . class
void . class
Moreover,
Class<Integer> intClass = int.class;
according to
The type of
p.class
, where p is the name of a primitive type (§4.2), isClass<B>
, whereB
is the type of an expression of typep
after boxing conversion (§5.1.7).