tl;dr
Unless you have a very special use-case, just use:
Integer
for business object values, member variables on a class.
int
for large amounts of raw data.
Just use Integer
For most common business-oriented Java apps, common practice is to just use the 32-bit int
or Integer
without much further thought, unless you know you will have values over 2 billion in which case use long
or Long
. On modern conventional hardware, there is little reason to fret over using short
/Short
.
Special cases
But since you asked “is there anything else to take into consideration at time to chose”, here are three special cases: enforcing limits, porting C code, and alternative hardware.
Enforce limit
If you want to enforce the limits of a short
or Short
, use those types. Choose these when you know you should have only values less than approximately 32,000, and want the compiler or runtime JVM to enforce that.
For example, add one to 101:
short oneOhOnePlusOne = ( (short) 101 + (short) 1 ) ;
System.out.println( "oneOhOnePlusOne: " + oneOhOnePlusOne ) ;
102
Next, try to exceed the limit to see the compiler enforce the limit.
short shortMaxPlusOne = ( Short.MAX_VALUE + (short) 1 ) ;
System.out.println( "shortMaxPlusOne: " + shortMaxPlusOne ) ;
error: incompatible types: possible lossy conversion from int to short
See this code run live at IdeOne.com.
Porting C code
These various numeric types were likely put in Java to make easier to port C code, easier both both practically and psychologically. The inventors of Java were well aware that during that era most every attempt at a new programming language failed with the criticism that it was “not like C”. Thus Objective-C (an inspiration for Java), and thus the monstrosity that is C++.
So, indeed, if you are porting C code, use the matching type to replicate behavior.
By the way… Technically, C does not actually define the size of its numeric types. In practice virtually every implementation of C uses the sizes as seen in Java.
FYI, even the most modern languages such as Swift and Kotlin have built-in numeric types for 8, 16, 32, and 64 bit integers.
More restrictive hardware
If there is any chance your app might run on other hardware not based on x86-64, then you may want to use these types. Alternate implementations of Java for such hardware might optimize better for the smaller types.
Primitive versus Object
Array of primitives are preferable for saving memory allocated
First of all, don't stress out on this. Don't fall into the trap of premature optimization. On conventional hardware with most common apps, any savings of memory on using primitives versus objects and arrays versus collections will be insignificant. Use the type (primitive/object) and structure (array/collection) appropriate to your coding context.
My practice:
- In my code, objects.
Use only objects (Integer
, not int
) within my own classes. I have two reasons. Firstly, I am among the camp of Object fans that wish Java were pure OOP without any primitives. (Indeed, there is research ongoing to see if primitives can virtually disappear in far-future versions of Java.) Secondly, I find when I used a primitive, I end up needing to use it in a context demanding objects, such as with collections.
- With others’ code, use primitives if they do.
Outside my own classes, I do not force the issue, as undue auto-boxing is senseless. If external code (not my own) is using primitives, I use primitives in that context.