misty
misty

Reputation: 123

Connection between Class and primitive types?

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.

Upvotes: 1

Views: 178

Answers (2)

xenteros
xenteros

Reputation: 15862

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 primitives into Wrappers 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.

Upvotes: 1

P.Carlino
P.Carlino

Reputation: 681

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

Upvotes: -1

Related Questions