Long vs Integer, long vs int, what to use and when?
Long
is the Object
form of long
, and Integer
is the object form of int
.
The long
uses 64 bits. The int
uses 32 bits, and so can only hold numbers up to ±2 billion (-231 to +231-1).
You should use long
and int
, except where you need to make use of methods inherited from Object
, such as hashcode
. Java.util.collections
methods usually use the boxed (Object
-wrapped) versions, because they need to work for any Object
, and a primitive type, like int
or long
, is not an Object
.
Another difference is that long
and int
are pass-by-value, whereas Long
and Integer
are pass-by-reference value, like all non-primitive Java types. So if it were possible to modify a Long
or Integer
(it's not, they're immutable without using JNI code), there would be another reason to use one over the other.
A final difference is that a Long
or Integer
could be null
.
There are a couple of things you can't do with a primitive type:
- Have a
null
value - synchronize on them
- Use them as type parameter for a generic class, and related to that:
- Pass them to an API that works with
Object
s
Unless you need any of those, you should prefer primitive types, since they require less memory.
- By default use an
int
, when holding numbers. - If the range of
int
is too small, use along
- If the range of
long
is too small, useBigInteger
- If you need to handle your numbers as object (for example when putting them into a
Collection
, handlingnull
, ...) useInteger
/Long
instead
An int
is a 32-bit integer; a long
is a 64-bit integer. Which one to use depends on how large the numbers are that you expect to work with.
int
and long
are primitive types, while Integer
and Long
are objects. Primitive types are more efficient, but sometimes you need to use objects; for example, Java's collection classes can only work with objects, so if you need a list of integers you have to make it a List<Integer>
, for example (you can't use int
in a List
directly).
Integer is a signed 32 bit integer type
- Denoted as
Int
- Size =
32 bits (4byte)
- Can hold integers of range
-2,147,483,648 to 2,147,483,647
- default value is 0
Long is a signed 64 bit integer type
- Denoted as
Long
- Size =
64 bits (8byte)
- Can hold integers of range
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
- default value is 0L
If your usage of a variable falls in the 32 bit range, use Int
, else use long
. Usually long is used for scientific computations and stuff like that need much accuracy. (eg. value of pi).
An example of choosing one over the other is YouTube's case. They first defined video view counter as an
int
which was overflowed when more than 2,147,483,647 views where received to a popular video. Since anInt
counter cannot store any value more than than its range, YouTube changed the counter to a 64 bit variable and now can count up to 9,223,372,036,854,775,807 views. Understand your data and choose the type which fits as 64 bit variable will take double the memory than a 32 bit variable.