Apart from using (byte[]) in streaming I don\'t really see byte and short used much. On the other hand I have seen long used where the actual value is |100| and byte would b
Stephen C's answer above is incorrect. (Sorry I don't have enough reputation points to comment, so I have to post an answer here)
He stated
"You don't achieve any space saving by using byte or short in simple variables instead of int, because most Java implementations align stack variables and object members on word boundaries"
It's not true. The following is run on Oracle JDK1.8.0 , with jol
public class CompareShorts {
public static void main(String[] args) {
System.out.println(VM.current().details());
System.out.println(ClassLayout.parseInstance(new PersonalDetailA()).toPrintable());
System.out.println(ClassLayout.parseInstance(new PersonalDetailB()).toPrintable());
}
}
class PersonalDetailA {
short height;
byte color;
byte gender;
}
class PersonalDetailB{
int height;
int color;
int gender;
}
The output:
# Running 64-bit HotSpot VM.
# Using compressed oop with 3-bit shift.
# Using compressed klass with 3-bit shift.
# Objects are 8 bytes aligned.
# Field sizes by type: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]
# Array element sizes: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]
com.hunterstudy.springstudy.PersonalDetailA object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1)
4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0)
8 4 (object header) 82 22 01 f8 (10000010 00100010 00000001 11111000) (-134143358)
12 2 short PersonalDetailA.height 0
14 1 byte PersonalDetailA.color 0
15 1 byte PersonalDetailA.gender 0
Instance size: 16 bytes
Space losses: 0 bytes internal + 0 bytes external = 0 bytes total
com.hunterstudy.springstudy.PersonalDetailB object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1)
4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0)
8 4 (object header) e1 24 01 f8 (11100001 00100100 00000001 11111000) (-134142751)
12 4 int PersonalDetailB.height 0
16 4 int PersonalDetailB.color 0
20 4 int PersonalDetailB.gender 0
Instance size: 24 bytes
Space losses: 0 bytes internal + 0 bytes external = 0 bytes total
As you can see, the class instance using shorts and bytes takes 16 bytes, and the class instance using ints takes 24 bytes. So it does save eight bytes of memory per class instance.
Most of the time, there's never a real good technical reason for a developer (Java, C#, BASIC, etc.) to decide for an int, short or byte - when the capacity is enough, of course. If the value will be under 2 billion then int it will be.
Are you sure we'll have people older than 255? Well, you never know!
Aren't 32,767 possible countries enough? Don't think too small!
In your example, you can be perfectly happy with your byte var containing 100, if you are absolutely sure than it will NEVER overflow. Why do guys use int the most? Because.... because.
This is one of those things that most of us just do because we saw it that way most of the time, and never asked differently.
Of course, I have nothing against "all things int". I just prefer to use the right type for each kind of value, no stress involved.
I think in most applications short has no domain meaning, so it makes more sense to use Integer.
The byte
datatype is frequently used when dealing with raw data from a file or network connection, though it is mostly used as byte[]
. The short
and short[]
types are often used in connection with GUIs and image processing (for pixel locations & image sizes), and in sound processing.
The primary reason for using byte
or short
is one of clarity. The program code states uncategorically that only 8 or 16 bits are to be used, and when you accidentally use a larger type (without an appropriate typecast) you get a compilation error. (Admittedly, this could also be viewed as a nuisance when writing the code ... but once again the presence of the typecasts flags the fact that there is truncation happening to the reader.)
You don't achieve any space saving by using byte
or short
in simple variables instead of int
, because most Java implementations align stack variables and object members on word boundaries. However, primitive array types are handled differently; i.e. elements of boolean
, byte
, char
and short
arrays are byte aligned. But unless the arrays are large in size or large in number, they doesn't make any significant contribution to the app's overall memory usage.
So I guess that the main reason that developers don't use byte
or short
as much as you (a C developer?) might expect is that it really doesn't make much (or often any) difference. Java developers tend not to obsess over memory usage like old-school C developers did :-).
I would most often use the short
and byte
types when working with binary formats and DataInput/DataOutput instances. If the spec says the next value is an 8bit or 16bit value and there's no value in promoting them to int
(perhaps they're bit flags), they are an obvious choice.