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)?
The java. lang. Class. isPrimitive() method can determine if the specified object represents a primitive 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.
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.
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.
As the javadoc for the Class
class states
Instances of the class
Class
represent classes and interfaces in a running Java application. Anenum
is a kind of class and an annotation is a kind of interface. Every array also belongs to a class that is reflected as aClass
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
, anddouble
), and the keywordvoid
are also represented asClass
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);
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.
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