Is Byte really implemented as an Int?

I had a java game in which, if possible, it is recommended to use all the data as Int, that this type works the fastest. He said that bytes, Char and Boolean are implemented as Int anyway, so you will not save the space and casting that you need to do in the code due to the fact that the byte data will slow it down. For example, for

throw is required.

a = (byte) (b + c);

since the result of the addition is Int, even if a, b, and c are declared as bytes.

I currently have a huge 2D array declared as Byte for my game to save space and bitwise operations. Does it really save space? I also saw bitwise operations performed on Ints in the examples, are bitwise operations performed as expected on Ints?

+4
source share
4 answers

This is usually not true. In fact, this is described in the JVM Specification §2.3 :

The primitive data types supported by the Java virtual machine are numeric types, type boolean( §2.3.4 ) and type returnAddress( §2.3.3 ).

Numeric types consist of integral types ( §2.3.1 ) and floating-point types ( §2.3.2 ).

Integral Types:

  • bytewhose values ​​are 8-bit signed integers with two additions and whose default value is zero

  • short, 16-

  • int, 32-

  • long, 64-

  • char, 16- , Unicode , UTF-16, ('\u0000')

, boolean . §2.3.4:

Java boolean, . Java Virtual Machine, boolean. Java, , Java Virtual Machine int.

- , byte[] int[], :

byte[] b = {42};
ICONST_1
NEWARRAY T_BYTE
DUP
ICONST_0
BIPUSH 42
BASTORE
ASTORE 1

int[] b = {42};
ICONST_1
NEWARRAY T_INT
DUP
ICONST_0
BIPUSH 42
IASTORE
ASTORE 1

?

, , , .

Ints?

, .

+6

, byte + byte = int, , byte 8 , int - 32 . byte , 4 .

, 10 10 byte s, 800, 10 × 10 int s 3200.

+1

, byte , byte[]. - , Java ( ). byte int 4 . - - baload - " ", , t25 int. , char boolean , int int.

2.6.1 JLS , , -Slot, char, float int . JVM , , , 3 , , .

: byte , , , byte int ( , byte , , ..).

+1

, , , , 8 + 8 , 8 .

.

|

255 |   1111 1111
121 |   0111 1001

376 | 1 0111 1000

, , , , . 376 "120", .

int , int. refer: http://www.tutorialspoint.com/java/java_bitwise_operators_examples.htm

0

All Articles