Android: does short take really 2 bytes?
android, short
Solution
Technically, in the Java language, a `short` is 2 bytes. Inside the JVM, though, `short` is a storage type, not a full-fledged primitive data type like `int`, `float`, or `double`. JVM registers always hold 4 bytes at a time; there are no half-word or byte registers. Whether the JVM ever actually stores a `short` in two bytes inside an object, or whether it is always stored as 4 bytes, is really up to the implementation.
This all holds for a "real" JVM. Does Dalvik do things differently? Dunno.
Problem
I am trying to make decision how to design my app. I have about 300 instances of class just like this: ``` public class ParamValue { protected String sValue = null; protected short shValue = 0; protected short mode = PARAM_VALUE_MODE_UNKNOWN; /* * ... */ } ``` I have an array of these instances. I can't find out does these shorts really take 2 bytes or they take anyway 4 bytes? And i need to pass the list of these objects via AIDL as a `List<Parcelable>`. Parcel can't `readShort()` and `writeShort()`, it can only work with `int`. So, to use short here too i have to manually pack two my shorts into one int, parcel it, and then unpack back. Looks too obtrusive. Could you please tell me how many bytes shorts take, and does it make sense to use short instead of int here? UPDATE: I updated my question for future readers. So, I wrote a test app and I figured out that in my case there's absolutely no reason to use `short`, because it takes the same space as `int`. But if I define array of shorts like that: ``` protected short[] myValues[2]; ``` then it takes less space than array of ints: ``` protected int[] myValues[2]; ```