Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Byte implemented as Int anyway?

Tags:

java

int

byte

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?

like image 704
Androidcoder Avatar asked Feb 15 '23 03:02

Androidcoder


2 Answers

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 the returnAddress 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 on boolean values. Instead, expressions in the Java programming language that operate on boolean values are compiled to use values of the Java Virtual Machine int 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.

like image 134
arshajii Avatar answered Feb 17 '23 21:02

arshajii


It is true that byte + byte = int, requiring a cast, but bytes are implemented with 8 bits of data in memory, while ints are 32 bits. Therefore, using bytes 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 bytes, its size would be 800, but a 10 by 10 array of ints' size would be 3200.

More information on this

like image 24
tckmn Avatar answered Feb 17 '23 19:02

tckmn