squeezing the use of memory

A

Asif Kazi

Hi,
Qick question about the use of int Vs. the use of short,char,byte
in 'for' loops. Will it affect the memory utilization/performance at all ?
If yes, to what extent, especially considering a program which uses
looping extensively and doesn't need the entire 'int' range.
Generally people just go ahead and use 'int' in loops...
Awaiting your thoughts,
Asif
 
V

VisionSet

Asif Kazi said:
Hi,
Qick question about the use of int Vs. the use of short,char,byte
in 'for' loops. Will it affect the memory utilization/performance at all ?
If yes, to what extent, especially considering a program which uses
looping extensively and doesn't need the entire 'int' range.
Generally people just go ahead and use 'int' in loops...
Awaiting your thoughts,

AFAIK
If there is a memory issue, ie large arrays etc, use the narrowest primative
type.
However consider that int is faster on most platforms since it is 32 bit and
so is the processor.

That may be over simplistic and I'd be interested in further comments also.
 
B

Brian S O'Neill

Whenever Java is running on a 32-bit platform, (which is usually the
case), then short, byte, and char are actully represented internally as
32-bit ints. You won't see any memory savings except in arrays.

As for performance in loops, int is prefered. Using a smaller type is
not native to the platform, and there may be a cost to keeping the value
in the right range and having to do any sign extension upon conversion.
 
J

Jon Skeet

Brian S O'Neill said:
Whenever Java is running on a 32-bit platform, (which is usually the
case), then short, byte, and char are actully represented internally as
32-bit ints. You won't see any memory savings except in arrays.

That's not true. It was in Sun's JVM before 1.4, but as of 1.4, you can
get a space saving. If you have an object with 1000 byte fields in it,
that will take up just over 1000 bytes. If you have an object with 1000
int fields in it, that will take up just over 4000 bytes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top