Using int vs Integer

I came across a class using Integer variables to grab the size to be used in a for loop. Is this good practice or should we use the primitive data type int?

Integer size = something.getFields().size(); for (Integer j = 0; j < size - 1; ++j) 
+43
java integer int primitive
May 16 '12 at 17:34
source share
9 answers

The Integer class is provided so that values ​​can be boxed / unpacked using the pure OO method. use int if necessary, unless you need to use it in an OO way; Integer is suitable in this case.

Java Int vs Integer

However, there are very different things. Int is a number; a> An integer is a pointer that can refer to an object containing a number.

...

int is not an object and cannot be passed to any method that requires objects. A special case is the use of the provided collection classes (List, Map, Set) - although you can write versions of these classes that provide similar capabilities for object versions. shell classes (Integer, Double, etc.) are often required whenever introspection is used (for example, in the reflection API).

The best description of when to use one of them:

Choosing between int and Integer

I will start by how these types should be used before entering in detail on why.

  • Prefers int for performance reasons
  • Methods that take objects (including generic types such as List<T> ) implicitly require the use of Integer
  • Using Integer relatively cheap for low values ​​(-128 to 127) due to interning - use Integer.valueOf(int) rather than the new Integer (int)
  • Do not use == or != With integer types
  • Consider using Integer when you need to represent a null value
  • Beware of unboxing Integer values ​​for int with null values
+60
May 16 '12 at 17:38
source share

If you can use int , do it. If the value can be null or used as an object, for example. Generics, use Integer

It usually doesn't matter which one you use, but often int does a little better.

+34
May 16 '12 at 17:36
source share

This approach is not very good in practice; use int whenever possible. Using Integer indicates that this particular variable may be null (or it was inside a collection of damned generics ...) - it is not.

Using Integer introduces extra overhead for boxing and unpacking.

+14
May 16 '12 at 17:37
source share

This is a potential disaster expected in large projects. The coder here forgot that every Integer is actually a new object, and to use it as an int, there must be boxing and unboxing all the time. This is not only ineffective, but will not work as expected. It is best recommended to always use int where possible, and use Integer only to place these values ​​in lists, containers, or to store the database. Remember that comparing objects with>, <, and == means something else than when you use the same operators to compare primitives.

+3
May 16 '12 at 17:38
source share

Do not use it only for a loop. Use a primitive int type (higher performance) that does not match the Integer class.

The Integer class wraps a primitive int value 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 int to String and String to int, as well as to other constants and methods useful when working with int.

So use Integer if you need to call Integer functions or you need to assign it a null value.

You also need to use Integer instead of int for generics like List

+1
May 16 '12 at 17:38
source share

When you need to use objects, you need to use Wrapper classes like Integer, Double, Float, etc.

eg:

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

Here we convert the string to an integer (primitive type), but the parseInt (String str) method only works on Wrapper classes (i.e. Object), so we used it ... you will find much more benefit from it in java.

+1
May 16 '12 at 17:40
source share

As a result of boxing and unpacking, there may be a penalty for execution, since you bear the overhead of conversion between a primitive and a reference type.

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

+1
May 16 '12 at 17:41
source share

I usually use Integer because of pure OO. Int's performance is definitely much better than its object copy, but it is only noticeable when you loop millions of times in a loop.

IMHO, If performance is paramount in your application and you want to compress at least one nano second, then use int without any brain. But if OO and readability are basic, then use Integer.

+1
May 17 '15 at 8:38
source share

Promote primitive types wherever possible.

primitive types cannot be used as GenericArgument or null.

0
May 16 '12 at 5:36 PM
source share



All Articles