I am confused about when to use primitive vs. non-primitive(?) types (i.e. int vs. Integer) in Java. I realize that in some places you can't use primitive types (for example when making use of generics). But what about in "normal" code? Is there a performance penalty for using non-primitive types? What about when working with Android?
***My question is very similar to this question, which was discovered by one of the posters below. The answers to the linked question give additional insights into this question, which are not covered below.
*** "non-primitive" types are officially referred to as reference types.
As an OO purist, you would likely shun the primitives altogether and damn the performance costs and lack of postfix operators. (Yes, there is a performance cost.) You may also adopt this approach simply from extensibility considerations as a designer (without necessarily being hung up on purity.)
As a practical matter (outside of theoretical and aesthetic questions), use the primitives everywhere you can and use the object version where you can't use primitives. (You already mentioned one such case. The language and APIs will drive this decision.)
As a performance freak, you would likely shun the object versions and you may not really care too deeply if you step on a few OO golden rules and sacrosanct no-goes: performance is king and you make your decisions accordingly.
I'd recommend option 2 as a good place to start until you develop your own dogmatic preferences! :)
My view: Using Integer as parameters or return values allows one thing that primitive ints don't allow: Using
null
. But is this a good idea? I think it rarely ever is.As far as performance is concerned: The compiler will optimize your code to some degree, so that is most of the time not a real concern.
Non-primitive types are objects. They have to be dynamically allocated, garbage collected, and checked for null-ness (although some of these operations may get removed by an optimizing compiler). Reading their actual value requires loading from a pointer. Primitive types are values. They generally take up less space and are faster to access.
A good rule of thumb is, use primitive types unless you need polymorphism, in which case use the corresponding object.
There is a slight penalty for converting between the types (autoboxing). Also
int
will have a bit less overhead so I would always go withint
if you can.Also see this question: When to use primitive and when reference types in Java
In Java,
int
is a primitive data type, whileInteger
is a Wrapper class.int
, being a primitive data type has less flexibility. We can only store the binary value of an integer in it. SinceInteger
is a wrapper class forint
data type, it gives us more flexibility in storing, converting and manipulating integer data.Integer
is a class and thus it can call various in-built methods defined in theclass
. Variables of typeInteger
store references toInteger
objects, just as with any other reference (object) type.You can find a more detailed explanation here.
Short answer: An
int
is a number; anInteger
is a pointer that can reference an object that contains a number. UsingInteger
for arithmetic involves more CPU cycles and consumes more memory. Anint
is not an object and cannot passed to any method that requires objects (just like what you said about Generics).