19

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.

Community
  • 1
  • 1
tjb
  • 11,480
  • 9
  • 70
  • 91
  • Another difference in addition to the ones mentioned below is that null cannot be assigned to a primitive type – tjb Oct 29 '11 at 11:16
  • Yet Another difference is that Object can be cast to a reference type i.e. Float, but not to a primitive (float) – tjb Mar 30 '12 at 07:24

6 Answers6

22

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

eggie5
  • 1,920
  • 18
  • 25
  • 4
    And if I'm not mistaken an int is allocated on the stack while Integer on the heap... – eggie5 Jun 24 '11 at 23:08
  • 2
    I believe the reference goes on the stack, but the actual integer value itself goes on the heap. – cyngus Jun 24 '11 at 23:56
5

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.

Heatsink
  • 7,721
  • 1
  • 25
  • 36
3

In Java, int is a primitive data type, while Integer 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. Since Integer is a wrapper class for int 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 the class. Variables of type Integer store references to Integer objects, just as with any other reference (object) type.

You can find a more detailed explanation here.

Don Brody
  • 1,689
  • 2
  • 18
  • 30
3

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

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

Community
  • 1
  • 1
Jack Edmonds
  • 31,931
  • 18
  • 65
  • 77
2

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! :)

alphazero
  • 27,094
  • 3
  • 30
  • 26
  • It would seem that the people who created the JVM allow for operator overloading, just not that we can use. For Integer we can use operators. – Nicholas Jul 05 '11 at 14:26
0

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.

mkro
  • 1,902
  • 14
  • 15