Using int vs Integer
Asked Answered
I

10

70

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) 
Isogamete answered 16/5, 2012 at 17:34 Comment(3)
Possible duplicate: int or IntegerVolin
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.Martella
possible duplicate of int or IntegerVelarize
M
103

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
  1. 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
Martella answered 16/5, 2012 at 17:38 Comment(4)
You answer is very good. Please note the performance benefit in mobile/resource-restricted applications of using primitive values.Georginageorgine
"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; }Baier
Why not use == or != with Integer types ? Won't it auto unbox to int ?Barbate
@MuhammadShahab using == or != compares the references instead of the integer values. This https://mcmap.net/q/281137/-why-aren-39-t-integers-cached-in-java is a great answer explaining the equality of Integers.Spume
T
43

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.

Tibetoburman answered 16/5, 2012 at 17:36 Comment(3)
Also it's trivial to change back & forth using Integer#intValue(Integer) and Integer#valueOf(int)Anisaanise
I think you mean Integer.intValue() ;) With auto-boxing and unboxing introduced in Java 5.0, you don't need to do this.Tibetoburman
In many cases int performs not just slightly, but a whole lot better, especially in tight loops :)Ruthven
V
18

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.

Velarize answered 16/5, 2012 at 17:37 Comment(0)
M
5

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.

Meuse answered 16/5, 2012 at 17:38 Comment(0)
E
1

Promote primitive types always where it is possible.

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

Emboss answered 16/5, 2012 at 17:36 Comment(0)
S
1

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

Semblance answered 16/5, 2012 at 17:38 Comment(0)
A
1

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.

Artistic answered 16/5, 2012 at 17:40 Comment(1)
Noob question: Isn't this equivalent to: int n = Integer.parseInt("10").intValue; ?Mowery
S
1

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.

Sheepwalk answered 16/5, 2012 at 17:41 Comment(0)
E
1

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)

Exstipulate answered 7/4, 2022 at 18:30 Comment(0)
M
0

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.

Mandola answered 17/5, 2015 at 8:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.