I had a java game book recommend implementing all data as Int when possible, that that type runs the fastest. It said the Byte, Char, and Boolean are implemented as Int anyway, so you don't save space and the casting you end up having to do in the code because of the Byte data will slow it down. For instance, a cast is needed for
a = (byte)(b+c);
since the addition result is an Int, even when a,b, and c are all declared as Bytes.
I currently have a huge 2D array declared as Byte for my game, to save space and for bitwise operations. Is it actually saving space? I've also seen bitwise operations done on Ints in examples, do bitwise operations work as expected on Ints?
This is generally incorrect. In fact, this is outlined in the JVM Specification §2.3:
The primitive data types supported by the Java Virtual Machine are the numeric types, the
boolean
type (§2.3.4), and thereturnAddress
type (§2.3.3).The numeric types consist of the integral types (§2.3.1) and the floating-point types (§2.3.2).
The integral types are:
byte
, whose values are 8-bit signed two's-complement integers, and whose default value is zero
short
, whose values are 16-bit signed two's-complement integers, and whose default value is zero
int
, whose values are 32-bit signed two's-complement integers, and whose default value is zero
long
, whose values are 64-bit signed two's-complement integers, and whose default value is zero
char
, whose values are 16-bit unsigned integers representing Unicode code points in the Basic Multilingual Plane, encoded with UTF-16, and whose default value is the null code point ('\u0000'
)
Now, for boolean
it's slightly a different story. From §2.3.4:
Although the Java Virtual Machine defines a
boolean
type, it only provides very limited support for it. There are no Java Virtual Machine instructions solely dedicated to operations onboolean
values. Instead, expressions in the Java programming language that operate on boolean values are compiled to use values of the Java Virtual Machineint
data type.
You can see differences in the bytecode depending on whether you use a byte[]
or an int[]
, so they're not identical:
byte[] b = {42};
ICONST_1 NEWARRAY T_BYTE DUP ICONST_0 BIPUSH 42 BASTORE ASTORE 1
vs
int[] b = {42};
ICONST_1 NEWARRAY T_INT DUP ICONST_0 BIPUSH 42 IASTORE ASTORE 1
Is it actually saving space?
Yes, it likely is, especially if the array is very large.
do bitwise operations work as expected on Ints?
Yes, they do.
It is true that byte + byte = int
, requiring a cast, but byte
s are implemented with 8 bits of data in memory, while int
s are 32 bits. Therefore, using byte
s will decrease the amount of memory that an array takes up by 4 times.
For example, if you had a 10 by 10 array of byte
s, its size would be 800, but a 10 by 10 array of int
s' size would be 3200.
More information on this
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With