Byte vs Int

I’ve been coding a network project for my high school AP programming class, and have been debating whether to use byte/short as the datatype for variables in the code before sending it across a network, or using int then typecasting it before sending across a network. I could use a little advice in the matter :slight_smile: :

byte/short:
I know that according to the Java VM specification, byte, short, char, and int all occupy 4 bytes in memory, but the shorter data-types are type-casted to int before arithmethetic operations are performed. I assume this takes some time to perform, however I do not know if it is small enough to worry about, or not.

int:
My concern here is that when typecasting to byte/short to pass to ByteBuffer to convert to network endianness, that the variable’s “contents” may be different when read on the other side of the connection.

Speed is important to me for this project, so I would enjoy any and all suggestions. Thanks in advance.

[quote=“crazyc94,post:1,topic:24670”]
Really? I thought those sorts of details were left to the VM implementation.

I spent a several hours reading through the Java VM Specification over a long weekend and noted that they’re all required to be 4 bytes in size (for stack and array purposes).

How are you planing on read/writing the data. Unless your using serialization it do not mather how it is stored in memory.

Then perhaps you should reread it. On most current JVMs an array of bytes uses just one byte for each element. A JVM isn’t required to slavishly follow the ‘slot’ scheme in the specification; it can do what it likes provided the behaviour of a Java program is unchanged.

I just created a test which subtracts Runtime.getRuntime().freeMemory() before and after the declaration of an int[10000] and byte[10000], respectively. The int[] has four times the impact of the byte[]. For the details, the difference was 40016 for int[] declaration and 10016 for byte[], so on a side note it would seem reasonable that array objects have a 16 byte overhead.

In order of posting:

  • I was planning on reading/writing as raw integer values

  • It has been a while since I read the specification, so I may have mis-remembered something.

  • I think I understand the various data types pretty well, but my main concern is whether there would be any performance gains from store data in the types used to transmit data across a network, or store them as ints and typecast.

Network-transfer is so slow that casting between primitives has no performance-penalty whatsoever. If you use the smallest primitives possible, you’ll have the best performance, as bandwidth is your bottleneck.

Thanks. That answers my question. :smiley: