[ACCEPTED]-Java Primitive Types: int vs. Integer-types

Accepted answer
Score: 22

Short answer: An int is a number; an Integer is a 6 pointer that can reference an object that 5 contains a number. Using Integer for arithmetic 4 involves more CPU cycles and consumes more 3 memory. An int is not an object and cannot 2 passed to any method that requires objects 1 (just like what you said about Generics).

Score: 5

Non-primitive types are objects. They have 10 to be dynamically allocated, garbage collected, and 9 checked for null-ness (although some of 8 these operations may get removed by an optimizing 7 compiler). Reading their actual value requires 6 loading from a pointer. Primitive types 5 are values. They generally take up less 4 space and are faster to access.

A good rule 3 of thumb is, use primitive types unless you need 2 polymorphism, in which case use the corresponding 1 object.

Score: 3

There is a slight penalty for converting 3 between the types (autoboxing). Also int will have a 2 bit less overhead so I would always go with 1 int if you can.

Also see this question: When to use primitive and when reference types in Java

Score: 3

In Java, int is a primitive data type, while 11 Integer is a Wrapper class.

int, being a primitive 10 data type has less flexibility. We can only 9 store the binary value of an integer in 8 it. Since Integer is a wrapper class for int data 7 type, it gives us more flexibility in storing, converting 6 and manipulating integer data. Integer is a class 5 and thus it can call various in-built methods 4 defined in the class. Variables of type Integer store 3 references to Integer objects, just as with any 2 other reference (object) type.

You can find 1 a more detailed explanation here.

Score: 2

As an OO purist, you would likely shun the 19 primitives altogether and damn the performance 18 costs and lack of postfix operators. (Yes, there 17 is a performance cost.) You may also adopt 16 this approach simply from extensibility 15 considerations as a designer (without necessarily 14 being hung up on purity.)

As a practical 13 matter (outside of theoretical and aesthetic 12 questions), use the primitives everywhere 11 you can and use the object version where 10 you can't use primitives. (You already 9 mentioned one such case. The language and 8 APIs will drive this decision.)

As a performance 7 freak, you would likely shun the object 6 versions and you may not really care too 5 deeply if you step on a few OO golden rules 4 and sacrosanct no-goes: performance is king 3 and you make your decisions accordingly.

I'd 2 recommend option 2 as a good place to start 1 until you develop your own dogmatic preferences! :)

Score: 0

My view: Using Integer as parameters or 6 return values allows one thing that primitive 5 ints don't allow: Using null. But is this a 4 good idea? I think it rarely ever is.

As 3 far as performance is concerned: The compiler 2 will optimize your code to some degree, so 1 that is most of the time not a real concern.

More Related questions