views:

205

answers:

3

i read that JVM stores internally short, integer and long as 4 bytes. this was an article from year 2000 so i don't know how true it is now.

for the newer JVMs is there any performance gain in using short over integer/long? and did that part of the implementation change since 2000?

thanks

+1  A: 
long  64 –9,223,372,036,854,775,808 to 9 ,223,372,036,854,775,807 
int  32 –2,147,483,648 to 2,147,483,647 
short  16 – 32,768 to 32,767 
byte  8 – 128 to 127 

Use what you need, I would think shorts are rarely used due to the small range and it is in big-endian format.

Any performance gain would be minimal, but like I said if your application requires a range more then that of a short go with int. The long type may be too extremly large for you; but again it all depends on your application.

You should only use short if you have a concern over space (memory) otherwise use int (in most cases). If you are creating arrays and such try it out by declaring arrays of type int and short. Short will use 1/2 of the space as opposed to the int. But if you run the tests based on speed / performance you will see little to no difference (if you are dealing with Arrays), in addition, the only thing you save is space.

Also being that a commentor mentioned long because a long is 64 bits. You will not be able to store the size of a long in 4 bytes (notice the range of long).

JonH
Long is not "generally" considered 8 bytes, it's defined as 64 bit. It will always be 64 bits.
Steve Kuo
+1  A: 

Integer types are stored in many bytes, depending on the exact type :

  • byte on 8 bits
  • short on 16 bits, signed
  • int on 32 bits, signed
  • long on 64 bits, signed

See the spec here.

As for performance, it depends on what you're doing with them. For example, if you're assigning a literal value to a byte or short, they will be upscaled to int because literal values are considered as ints by default.

byte b = 10;  // upscaled to int, because "10" is an int

That's why you can't do :

byte b = 10;
b = b + 1;  // Error, right member converted to int, cannot be reassigned to byte without a cast.

So, if you plan to use bytes or shorts to perform some looping, you won't gain anything.

for (byte b=0; b<10; b++) 
{ ... } 

On the other hand, if you're using arrays of bytes or shorts to store some data, you will obviously benefit from their reduced size.

byte[] bytes = new byte[1000];
int[] ints = new int[1000];  // 4X the size

So, my answer is : it depends :)

Olivier Croisier
+1  A: 

It's an implementation detail, but it's still true that for performance reasons, most JVMs will use a full word (or more) for each variable, since CPUs access memory in word units. If the JVM stored the variables in sub-word units and locations, it would actually be slower.

This means that a 32bit JVM will use 4 bytes for short (and even boolean) while a 64bit JVM will use 8 bytes. However, the same is not true for array elements.

Michael Borgwardt
I assume you are essentially saying that you will save space but there will be no gain for perforamnce reasons (especially with arrays).
JonH
With regular variables, short saves neither space nor peformance over int (on a 32 or 64bit JVM). With arrays, it's hard to say since the extra CPU commands needed to convert the shorts to words and back can easily be offset by better cache behaviour due to smaller data size.
Michael Borgwardt
I agree with that, short vs int as regular variables will not matter, again it comes down to what are the needs in the application (the range of the type). But for arrays, the short will be a better space saver then an int / long.
JonH