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?
Integer class is a wrapper class for the primitive type int which contains several methods to effectively deal with an int value like converting it to a string representation, and vice-versa. An object of the Integer class can hold a single int value.
The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field whose type is int .
Integer is a class and thus it can call various in-built methods defined in the class. Variables of type Integer store references to Integer objects, just as with any other reference (object) type.
A int is a data type that stores 32 bit signed two's compliment integer. On other hand Integer is a wrapper class which wraps a primitive type int into an object. 2. Purpose. int helps in storing integer value into memory.
A primitive becoming an Object
For primitives, there are Class objects available as constants named TYPE in the corresponding wrapper classes -- i.e. int.class is changed to java.lang.Integer.TYPE . For other types, the compiler creates a private member variable in the class being compiled to hold the Class object, and generates code to initialize that member using Class.forName() .
Found some discussion
And a nice discussion here and your example also covered in this link.
A few words from there :
how can a Class be a primitive? Let's confuse things a bit more. We can access the Class object representing a defined class by coding, say:
Equation.class // returns the Equation Class object
But, we can also say:
int.class
obtain a Class object whose name is "int". Note we have not sent the getClass() method to an object; we have used the reserved word for a built-in primitive type (int) and, using dot notation, accessed its class "field." And this returns a Class object!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With