I am looking through the code someone wrote a while back and wondering whether I am missing something here
Assuming
List<Integer> runUids = new ArrayList<Integer>();
and later on as part of a loop
int runUID = runUidsAL.get(i).intValue();
Is there any reason why intValue()
needs to be called here?. Don't think it's needed here. Do you?
intValue. Returns the value of this Integer as an int . Returns: the numeric value represented by this object after conversion to type int .
intValue. Returns the value of the specified number as an int , which may involve rounding or truncation. Returns: the numeric value represented by this object after conversion to type int .
Integer objects are immutable, so you cannot modify the value once they have been created.
As already mentioned int is a primitive data type and takes 32 bits(4 bytes) to store. On other hand Integer is an object which takes 128 bits (16 bytes) to store its int value.
You didn't say so, but I assume that runUID
is an int
.
It's not necessary to call intValue()
explicitly on the Integer
object returned by runUidsAL.get(i)
; Java will do this automatically by auto-unboxing.
A real situation that I have lived a few minutes ago:
I have a list of Integer objects. Each Integer is the index of an other list whose positions I want to remove.
Then... I was doing that:
for (Integer index : positionsToRemoveList) {
historyRecord.remove(index);
}
Java was calling to the next method
public boolean remove(Object object);
Instead of:
public E remove(int location);
My "historyRecord" List wasn't being updated beacuse Java was trying to remove by an Object instead of by position.
The right way is:
for (Integer index : positionsToRemoveList) {
historyRecord.remove(index.intValue());
}
And this is my real story : )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With