Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing call object representing a primitive type

I have read that in order to access the call object representing a primitive type, I can do this:

Class intClass = int.class;

But how do primitive types have classes to represent them? They are primitive, which should mean they have no class. Why does the example above work, and what class contains int (Integer class maybe)?

like image 943
KarimS Avatar asked Aug 02 '15 19:08

KarimS


People also ask

How do you check if an object is a primitive type in Java?

The java. lang. Class. isPrimitive() method can determine if the specified object represents a primitive type.

Is an object a primitive data type?

In JavaScript, a primitive (primitive value, primitive data type) is data that is not an object and has no methods or properties. There are 7 primitive data types: string. number.

How can we use primitive data types as objects?

An object of Boolean is created. In the above example, we have created variables of primitive types ( int , double , and boolean ). Here, we have used the valueOf() method of the Wrapper class ( Integer , Double , and Boolean ) to convert the primitive types to the objects.

What are primitive objects in Java?

Primitives simply represent a value, like the number seven or the boolean value of false. They have no methods and no complementary properties. Also, you can't do much with them other than inspect their value or perhaps change the value that they hold. But they are not, by any means, objects.


2 Answers

As the javadoc for the Class class states

Instances of the class Class represent classes and interfaces in a running Java application. An enum is a kind of class and an annotation is a kind of interface. Every array also belongs to a class that is reflected as a Class object that is shared by all arrays with the same element type and number of dimensions. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects.

A Class object simply provides some metadata and factory methods for the type it represents.

For example, Class#isPrimitive() will tell you if the represented type is a primitive.

The class Class and its instances are, among other things, used for reflection.

Let's say you had the a class like

public class Example {
    public long add(int first, long second) { // for whatever reason
        return first + second;
    }
}

and you wanted to invoke the add method, given only its name and parameter types. The following would fail

Class<?> exampleClass = Example.class;
exampleClass.getMethod("add", Integer.class, Long.class);

because the parameter types are not Integer and Long, they are int and long.

You'd have to do something like

Class<Example> exampleClass = Example.class;
Method addMethod = exampleClass.getMethod("add", int.class, long.class);
Example instance = exampleClass.newInstance();
addMethod.invoke(instance, 42, 58L);
like image 193
Sotirios Delimanolis Avatar answered Sep 18 '22 02:09

Sotirios Delimanolis


If you look at the field summary for the Integer class, you will find that the primitive type int is actually represented by the class instance TYPE. Therefore, int.class would equal Integer.TYPE.

Here's a link to Javadocs where you can find the TYPE class instance.

like image 35
deezy Avatar answered Sep 19 '22 02:09

deezy