Android: does short occupation really 2 bytes?

I am trying to make a decision on how to create my application.

I have about 300 class instances:

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 if these shorts really take 2 bytes or do they take 4 bytes?

And I need to pass the list of these objects through AIDL as List<Parcelable> . The premise cannot readShort() and writeShort() ; it can only work with int . So, to use short here too, I need to manually pack my two shorts into one int, send it, and then unpack it back. It looks too intrusive.

Could you tell me how many bytes it takes, 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 application, and I realized that in my case there is no reason to use short , because it takes the same place as int . But if I define an array of shorts like this:

  protected short[] myValues[2]; 

then it takes up less space than an array from ints:

  protected int[] myValues[2]; 
+5
android short
source share
2 answers

Technically, in Java, short is 2 bytes. However, inside the JVM, short is a storage type, not a full-fledged primitive data type, such as int , float or double . JVM registers always contain 4 bytes at a time; no half-register or byte registers. Regardless of whether the JVM really stores short in two bytes inside the object or is always stored as 4 bytes, it really depends on the implementation.

All this is done for the "real" JVM. Does Dalvik do anything different? Dunno.

+5
source share

According to the Java Virtual Machine Specification, chap. 2.4.1 , a short always exactly two bytes.

The Java Native Interface provides direct access from native code to arrays of primitives stored in a virtual machine. A similar situation could happen in Android JNI . This pretty much ensures that Java short[] will be an array of double-byte values ​​in a JVM compatible environment or in a Dalvik virtual machine.

+1
source share

All Articles