I thought I understand concept of class(object) Class, but reading about it in Java API, I found this:
The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects.
The phenomenon on autoboxing
and unboxing
is what you're looking for. In java there are some primitives for comfort purposes. They all have wrapper
classes. These are: Integer
, Double
, Boolean
etc.
Autoboxing is responsible for wrapping primitive
s into Wrapper
s each time the Wrapper
is expected but a primitive
is passed. On the other hand unboxing comes. When it's a primitive
expected but Wrapper
passed unboxing will manage to extract the proper value.
It's well described here
Example:
Integer one = new Integer(1);
int i = one.intValue();
void printInteger(int i) {
System.out.println(i);
}
printInteger(one);
No exception will be thrown - one
will be unboxed to int
and printed.
The difference is that the primitives are just zones of memory and when you are using the keyworks you are telling to compiler how 'to see' these areas. While with the correspondend objects such as Integer or Character are objects that have methods to work with these types and they are seen as such as objects
The Void class is an uninstantiable placeholder class to hold a reference to the Class object representing the Java keyword void.
– Suresh Atta