Using int vs Integer
Solution 1
the Integer class is provided so that values can be boxed/unboxed in a pure OO manner. use int where appropriate unless you specifically need to use it in an OO way; in which case Integer is appropriate.
However, very different things are going on under the covers here. An int is a number; an > Integer is a pointer that can reference an object that contains a number.
...
An int is not an object and cannot passed to any method that requires objects. A common case is in using the provided collection classes ( List , Map , Set ) - though it is possible to write versions of these classes that provide similar capabilities to the object versions. The wrapper classes ( Integer , Double , etc) are frequently required whenever introspection is used (such as in the reflection API).
A better description of when to use one vs. the other:
Choosing between int and Integer
I'll start with how these types should be used before going into detail on why.
- Prefer
int
for performance reasons- Methods that take objects (including generic types like
List<T>
) will implicitly require the use ofInteger
- Use of
Integer
is relatively cheap for low values (-128 to
- because of interning - use
Integer.valueOf(int)
and not new Integer(int)
- Do not use
==
or!=
with Integer types- Consider using
Integer
when you need to represent the absence of a value (null)- Beware unboxing Integer values to int with null values
Solution 2
If you can use int
do so. If the value can be null
or is used as an Object e.g. Generics, use Integer
Usually it doesn't matter which one you use but often int
performs slightly better.
Solution 3
This approach is not good in practice, use int
whenever possible. Usage of Integer
indicates that this particular variable can be null
(or it was inside a collection, damn generics...) - which is not the case.
Also using Integer
introduces an extra overhead of boxing and unboxing.
Solution 4
This is a potential disaster waiting to happen in large projects. The coder here forgot that each Integer is actually a new object, and, to use it as an int, there has to be boxing and unboxing all the time. Not only is it inefficient, it's also not going to run as expected. You are best advised to always use int where possible, and only use Integer for placing these values into lists, containers or for database storage. Remember, comparing Objects using >, < and == means something else than when you are using the same operators to compare primitives.
Solution 5
Dont use it only to loop. Use the primitive type int (better performance), which is not the same as the Class Integer.
The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field whose type is int.
In addition, this class provides several methods for converting an int to a String and a String to an int, as well as other constants and methods useful when dealing with an int.
So, use Integer if you need to call the Integer functions or need to assign a null value to it.
You will also need to use Integer instead of int for generics like List
developer
Java Developer very much interested on Java releated technologies
Updated on July 08, 2022Comments
-
developer almost 2 years
I came across a class using Integer variables to capture size to be used in a for loop. Is this good practice or should we use the int primitive data type?
Integer size = something.getFields().size(); for (Integer j = 0; j < size - 1; ++j)
-
ametren almost 12 yearsAlso it's trivial to change back & forth using
Integer#intValue(Integer)
andInteger#valueOf(int)
-
Vishy almost 12 yearsI think you mean
Integer.intValue()
;) With auto-boxing and unboxing introduced in Java 5.0, you don't need to do this. -
Sergey Kalinichenko almost 12 yearsIn many cases
int
performs not just slightly, but a whole lot better, especially in tight loops :) -
Andrew Carr almost 12 yearsYou answer is very good. Please note the performance benefit in mobile/resource-restricted applications of using primitive values.
-
Simple-Solution over 7 years"An int is not an object and cannot passed to any method that requires objects." How about doSomething(911) ?? public Object doSomething (Object o) { // do something!!! return o; }
-
Sam Sirry over 4 yearsNoob question: Isn't this equivalent to: int n = Integer.parseInt("10").intValue; ?
-
Muhammad Shahab over 3 yearsWhy not use == or != with Integer types ? Won't it auto unbox to int ?