views:

107

answers:

5

Why to use primitive types in java instead of Wrapper classes? I want to know that already we have wrapper classes in java, then why we need to use primitive types? What are the importance of primitive types in java?

+4  A: 

Your question is backwards. The primitive types are more fundamental than their wrappers.

Really the only useful thing that wrappers give you is the ability to be treated as a subclass of Object (so that they can be put into collections and so on). All the really useful stuff (like arithmetic and ordering) is provided by the primitive type.

Note that while you can say stuff like:

Integer i = Integer.valueOf(4);
Integer j = Integer.valueOf(2);
Integer k = i + j;

this is just a convenience. Underneath, the last line becomes something like:

Integer k = Integer.valueOf(i.intValue() + j.intValue());

so that the arithmetic occurs on the primitive values. (This convenience is known as boxing/unboxing.) There is a performance penalty to this, so that on my machine, this loop:

for (int i=0; i<10000000; i++) { }

is about 10 times faster than this loop:

for (Integer i=0; i<10000000; i++) { }
Simon Nickerson
The question is actually valid in a way - yes, in practice, in Java the wrapper classes are defined in terms of primitive types, and the latter are "magic". But it could just as well be that the classes themselves are "magic", and there are no value-typed primitives at all.
Pavel Minaev
A: 

performance, primitive classes map to the processor without having to go through as many operations.
In the case of boolean, int and float - you can make them volatile and ensure that they do not require synchronization (Reads and writes are atomic)

Romain Hippeau
A: 

Performance....

Enrique
+1  A: 

In Java, every object has some space overhead. It depends on the JVM, and the size of a "word" on the target architecture (64-bit versus 32-bit), but I usually estimate that every object has about 40 bytes of "overhead" beyond what is needed for its fields.

So, for example, an array of 1024 bytes is one object, with 40 bytes of overhead; it has a length member (an int) which needs 4 bytes, and it needs 1 byte for each element, for a total of around 1k.

An array of 1024 Byte instances has the overhead of the array itself, its length member, and then about 41 bytes for every Byte instance (40 bytes of overhead, plus 1 byte for data). Altogether, that's over 41k! Much more than a primitive byte[].

Since the wrapper types are immutable, there have been suggestions for clever tricks to make primitive data look like wrapped object instances, but so far, none of these mechanisms have been implemented.

erickson
A: 

[humor] To save typing! This:

    int i = 4;
    int j = 38;
    int k = i + j;

is shorter to type than:

    Integer i = new Integer(4);
    Integer j = new Integer(38);
    Integer k = i + j;

[/humor]

In reality, the other answers say it better than I could.

GreenMatt
@matto1990: Didn't get the point of your edit, as either your version or mine would work. As such, I'd rather my post be left alone. But hey, thanks for helping me get the Cleanup badge!
GreenMatt
Sorry should have explained. With some Integers (bellow 100 I think it is) the Integer class pre-generates them and keeps them in memory. Hence if you call Integer.valueOf(4) you will receive a class that is in memory already. Calling new Integer(4) will create a new instance of Integer and store that in memory as well. It's only a tiny thing and in practice doesn't matter at all really. Just saw it and thought I'd change it. Take a look here in the Java docs: http://download.oracle.com/docs/cd/E17476_01/javase/1.5.0/docs/api/java/lang/Integer.html#valueOf(int)
matto1990
@matto1990: Thanks for the explanation, it triggered a dim memory. However, the code in your edit is longer than mine, so it proves the point even better! :-)
GreenMatt
Exactly. For situations like this primitives are perfect :)
matto1990