Why does each slot in the local array of variables in the stack frame have 4 bytes rather than 1 byte in the JVM?

Each slot of a local array of variables has 4 bytes. Therefore, one slot is used to store characters, short or byte variables. Means that all smaller data types are internally converted to an int data type.

I doubt:

1). Doesn't that make smaller data types useless if they have 4 bytes inside ?, If yes, why not remove these data types from the language? 2). If each slot has 1 byte, then there will be no memory loss. Why does each slot not have 1 byte?

+2
source share
4 answers

1). , 4 ?

, , - .

, , theres , ( ) .

, ?

-, , theres . -, theres I/O. s char s , , . , , . , StringBuilder.append(char) StringBuilder.append(byte), int.

2). 1 , . 1-?

, - . ( ) . CPU . , long double , , , Java .

, , , long double. . , JVM . , - , CPU, .

+3

, 4 ?,

32- 64- . , , 1 , 2 4 .

1- , . 1-?

256 . , ; 1 ? , . .

+3

4 (byte, char, short, int, boolean, float, ) 8 (long, double).

. .

.

4- , 32 64 , 4 .

+2

bytes bacause . byte int.

And there is no reason to make the variable slot 1-byte, because it will not save a lot of memory (the stack is usually much less than a heap), and the memory works faster if the infinity is supplemented.

0
source

Source: https://habr.com/ru/post/1657860/


All Articles