In java int, float and etc., are primitive types. Wrapper classes are used in case we need to use it with generics. But still the following declaration works in java,
Class<Integer> intClass=int.class
How can we call int.class
even though it is a primitive type?
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.
represented as Class objects.
So
System.out.println(int.class);
will printint
whereasSystem.out.println(Integer.class);
will printclass java.lang.Integer
.int.class
is same type asClass<Integer>
as per the specifications.From Docs:
From JLS 15.8.2:
A primitive becoming an Object
Found some discussion
And a nice discussion here and your example also covered in this link.
A few words from there :
But, we can also say: