Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does int.class mean

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?

like image 668
Hariharan Avatar asked Aug 16 '13 11:08

Hariharan


People also ask

What does class int mean?

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.

What does int class mean in Java?

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 .

Is an int a class?

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.

What is Integer [] in Java?

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.


1 Answers

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!

like image 106
Suresh Atta Avatar answered Sep 26 '22 13:09

Suresh Atta