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)
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)
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
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.
Integer#intValue(Integer)
and Integer#valueOf(int)
–
Anisaanise Integer.intValue()
;) With auto-boxing and unboxing introduced in Java 5.0, you don't need to do this. –
Tibetoburman int
performs not just slightly, but a whole lot better, especially in tight loops :) –
Ruthven 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.
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.
Promote primitive types always where it is possible.
primitive types can not be used as GenericArgument or a null.
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
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.
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.
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)
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.
© 2022 - 2024 — McMap. All rights reserved.