• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using byte/short for loop counter variables  RSS feed

 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read that it is always better to use an int as a loop counter variable, because we cannot be sure that internally java wont promote short/byte to int if used.
Why cant we use a short if we are sure the counter wont go behind or above -2^15 or 2^15 - 1? Why is java doing this - when we think we can save some memory?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your machine has (probably) several gigabytes of memory, and you're worried about saving one or two bytes?

My honest answer would be "don't worry about it".

A slightly longer answer would be "sometimes, it's more efficient for the JVM to do things like this"
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
then why do we need short at all? instead of having 4 categories of integers - byte,short,int,long we can have just int and long,right?
does java provide us something(short) not to be used - even if it is used it is actually not being used at all(promoted to int)?
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will get promotion to an int as soon as you try comparisons, for example with < myArray.length. So using bytes and shorts is pointless.
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The primitive type 'short' uses two bytes while type 'int' uses 4-bytes. There are
situations where using 1/2 as much memory can be important, such as managing
huge arrays or images, for example. Loop counters is just not one of these situations.

Jim ... ...
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
short is two bytes; if you need only one byte use the appropriately named type "byte"

Ah, you've already fixed it.
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob, you're really fast today . . .

Jim ... ...
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinoth Kumar Kannan wrote:then why do we need short at all? instead of having 4 categories of integers - byte,short,int,long we can have just int and long,right?
does java provide us something(short) not to be used - even if it is used it is actually not being used at all(promoted to int)?

Because sometimes there are situations where it does make a difference. Suppose that you have some data structure that contains millions of numbers, and you know that each number fits in the range of a short. It can be a noticeable difference if you have to store, for example, 10 million numbers - you'll save 20 MB if you store them as shorts.

But a loop counter is only one variable; making it a short instead of an int saves 2 bytes of memory, which will absolutely not make any noticeable difference in speed or memory usage at all.
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm...yeah I get it. It hardly would make a difference! Thanks to all
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!