• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

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

 
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.


 
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.

 
Sheriff
Posts: 24366
55
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.
 
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.
 
Bartender
Posts: 10759
68
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
 
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!
 
Bartender
Posts: 3323
86
  • 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.
 
Marshal
Posts: 64103
215
  • 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!