• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using byte/short for loop counter variables

 
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)?
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ... ...
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob, you're really fast today . . .

Jim ... ...
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm...yeah I get it. It hardly would make a difference! Thanks to all
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
reply
    Bookmark Topic Watch Topic
  • New Topic