Why does jvm extend bytes & short to int before pushing the stack?

java code :,

byte a_b = 12; short c_d = 14 

replaced by byte codes with

 bipush 12 // expands byte1 (a byte type) to an int and pushes it onto the stack sipush 14 // expands byte1, byte2 (a short type) to an int and pushes it onto the stack 

Why does jvm make this extension and not use bytes and short?

Also, when I open the bytecode of my file

EDIT: short var = 14 is replaced with bipush 14, not sipush 14

I understand that I do not understand or is there a mistake?

enter image description here

I am using the next version

 java version "1.6.0_26" Java(TM) SE Runtime Environment (build 1.6.0_26-b03) Java HotSpot(TM) Client VM (build 20.1-b02, mixed mode, sharing) 
+4
source share
1 answer

Because (conceptually) the smallest unit of data in the JVM stack is 32 bits. Thus, there is no way to increase the size of the stack by only 8 bits.

http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-2.html#jvms-2.6.2

At any moment in time, the operand stack has a corresponding depth, where a value of type long or double brings two units into the depth and a value of any other type brings one unit.

+5
source

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


All Articles