Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use/purpose of primitive type classes?

I recently learned that there are Class representations for the primitive types in the JVM. For example, int.class, double.class, and even a void.class.

What I don't understand is why these are there. They don't seem to serve any functional role. Using reflection, I searched through the classes, and they have no constructors, no methods, and no fields. For all intents and purposes, they seem empty and useless. The primitive type variables are not even instances of their respective classes, as indicated by the following returning false:

int a = 3;
int.class.isInstance(a);

So why do they exist? They must serve some purpose, maybe for the compiler or something, but whatever it is is completely beyond me. There is even an explicit reference to int.class in the Integer API (and likewise for each primitive type and its respective wrapper Object). I haven't been able to find any reference to their existence, much less their use, in the JLS.

like image 834
asteri Avatar asked Nov 02 '12 14:11

asteri


People also ask

What is the purpose of primitive data types?

Primitive types are the most basic data types available within the Java language. There are 8: boolean , byte , char , short , int , long , float and double . These types serve as the building blocks of data manipulation in Java. Such types serve only one purpose — containing pure, simple values of a kind.

Why does Java use primitive types?

Instead of create variables using new, Java can use primitive types to create automatic variables that are not references. The variables hold the value, and it's place on the stack so its much more efficient. Java determines the size of each primitive type.

What is the use of byte primitive?

The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters. They can also be used in place of int where their limits help to clarify your code; the fact that a variable's range is limited can serve as a form of documentation.

How primitive data types can be used as objects?

The primitive data types are not objects so they do not belong to any class. While storing in data structures which support only objects, it is required to convert the primitive type to object first which we can do by using wrapper classes.


2 Answers

What I don't understand is why these are there.

Consider the following:

public int foo() {
    return 0;
}

...

Method method = someClass.getDeclaredMethod("foo");
Class<?> clazz = method.getReturnType();

Without a Class representation of int, what would the above return? It shouldn't return Integer.class as they're not the same thing. (Imagine trying to distinguish between methods which were overloaded, one with an int and one with an Integer parameter.)

I've used these classes before to provide default values for arguments when calling them via reflection. Based on the parameter type, I've used null for any reference type, and some (boxed, obviously) primitive value for each of the primitive types.

like image 111
Jon Skeet Avatar answered Oct 17 '22 15:10

Jon Skeet


It is a cheap ass solution that turns out badly.

Before 1.5, Java types can be categorized as

java type
    primitive type
    reference type
        class type (including interface)
        array type

Then ideally, java reflection should provide 5 concepts mirroring these 5 types. But they used a single Class to represent them all, including primitive and array types. So a Class does not necessarily mean a class.

That's still manageable. But after 1.5, Java types become more complicated, so a new Type is introduced. Unfortunately, instead of having a new and clean hierarchy that directly mirror language spec, they decides to make Class a subtype of Type; not only the old mess is brought in, it spawns some new mess, and the whole Type hierarchy is unintelligible.

like image 30
irreputable Avatar answered Oct 17 '22 14:10

irreputable