• Post Reply Bookmark Topic Watch Topic
  • New Topic

Integer or int for loops. Which one should use?  RSS feed

 
Robson Martinz
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys!

I would like to know which one is more appropriate to use in "for" loops or "while" loops. Thank you!
I don't know exactly when to use Integer or int.


 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Likes 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first one. Only use Integers when you have to. ints are numbers, while Integers point to an object that contains a number. For example, a List can only contain objects. So I'd need to use Integers to make a List of numbers.

 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joel, you've been providing good answers like this one for quite a while now. And I noticed that somehow you don't have any cows yet. So I'm giving you your first cow.
 
Pankaja Shinde
Ranch Hand
Posts: 87
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Robson Martinz

Both the for loop works.

But its good practice to use first one.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robson Martinz wrote:I don't know exactly when to use Integer or int.

I think Joel's covered most of it, but the main reason not to use Integers is that they are immutable.
Unfortunately, all the boxing and unboxing features in Java sometimes hide that fact from you. For example, if you expanded all of those 'boxing/unboxing' bits in your second loop, you'd get:

for (Integer i = Integer.valueOf(0); i.intValue() < 10;
   i = Integer.valueOf(i.intValue() + 1)) {
...

Quite a mouthful, eh? But that's what's actually happening (or something very similar).

HIH

Winston
 
James Harte
Greenhorn
Posts: 17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will want to use the int in loops so that:
1) int is faster to increment
2) Using Integer will use more memory for each increment as it is immutable and will force a new object to be created each time.
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Joel, you've been providing good answers like this one for quite a while now. And I noticed that somehow you don't have any cows yet. So I'm giving you your first cow.

Thank you!
 
Tony Docherty
Bartender
Posts: 3271
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Harte wrote:2) Using Integer will use more memory for each increment as it is immutable and will force a new object to be created each time.

That's not necessarily true as the Integer class caches a number Integer objects (I don't think it's specified in the JLS but for I believe it generally caches the values -128 to 128) so as long as the for loops stay in this range no new objects will be created. Also even if new objects were created it wouldn't really effect memory usage much as each object would be available for garbage collection once it was no longer referenced - there would of course be a performance penalty.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It says so in the Integer class; the range −128…+127 is a minimum and larger values can be cached.

Slightly different subject:
It says in the JLS that certain values are cached when boxing, and there is more discussion here.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!