Using int vs Integer

JavaIntegerIntPrimitive

Java Problem Overview


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) 

Java Solutions


Solution 1 - Java

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.

Java Int vs Integer > 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 of Integer > - Use of Integer is relatively cheap for low values (-128 to > 127) 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 - Java

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 - Java

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 - Java

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 - Java

Promote primitive types always where it is possible.

primitive types can not be used as GenericArgument or a null.

Solution 6 - Java

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

Solution 7 - Java

When there is a need of using objects, you have to use the Wrapper classes, like Integer, Double, Float, etc...

eg:

 int n = Integer.parseInt("10");

Here we are converting the string to an integer (Primitive type) , but method parseInt(String str) works only on Wrapper classes (ie Object), so we used it... you will find many more use of it in java.

Solution 8 - Java

There can be a performance penalty due to boxing and unboxing, as you incur the overhead of converting between a primitive and a reference type.

However, the Integer class adds additional methods that can be very useful.

Solution 9 - Java

You can't use == or != with Integer types as it'll compare their "pointer location".

In order for auto-unboxing to kick off, at least one of the elements must be int.

If you are comparing Integer1 == Integer2, what actually happens is that Object.equals gets called as in: Integer1.equals(Integer2) which (unless overridden) compares the references.

(This is meant to be a reply to @Muhammad Shahab but I cannot add comments yet)

Solution 10 - Java

I normally use Integer because of purely OO. Performance of int is definitely far better than its object counterpart but it is noticeable only when you are looping millions time in the loop.

IMHO, If performance is paramount importance in you application and you want to squeeze even single nano second then use int without any brain. But if OO and readability is main concerned then use Integer.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondeveloperView Question on Stackoverflow
Solution 1 - JavaMike McMahonView Answer on Stackoverflow
Solution 2 - JavaPeter LawreyView Answer on Stackoverflow
Solution 3 - JavaTomasz NurkiewiczView Answer on Stackoverflow
Solution 4 - JavaEwaldView Answer on Stackoverflow
Solution 5 - JavaDamian Leszczyński - VashView Answer on Stackoverflow
Solution 6 - JavaSullyView Answer on Stackoverflow
Solution 7 - JavaKumar Vivek MitraView Answer on Stackoverflow
Solution 8 - JavadckrooneyView Answer on Stackoverflow
Solution 9 - JavaDavit Escobedo View Answer on Stackoverflow
Solution 10 - JavaRakesh ChauhanView Answer on Stackoverflow