java - Why in JVM Integer is stored as byte and short? -


here 1 snippet of code

public class classifier {     public static void main(string[] args)      {         integer x = -127;//this uses bipush         integer y = 127;//this use bipush         integer z= -129;//this use sipush         integer p=32767;//maximum range of short still sipush         integer = 128; // use sipush         integer b = 129786;// invokes virtual method integer class      }  } 

here partial byte code of

      stack=1, locals=7, args_size=1          0: bipush        -127          2: invokestatic  #16                 // method java/lang/integer.valueo f:(i)ljava/lang/integer;          5: astore_1          6: bipush        127          8: invokestatic  #16                 // method java/lang/integer.valueo f:(i)ljava/lang/integer;         11: astore_2         12: sipush        -129         15: invokestatic  #16                 // method java/lang/integer.valueo f:(i)ljava/lang/integer;         18: astore_3         19: sipush        32767         22: invokestatic  #16                 // method java/lang/integer.valueo f:(i)ljava/lang/integer;         25: astore        4         27: sipush        128         30: invokestatic  #16                 // method java/lang/integer.valueo f:(i)ljava/lang/integer;         33: astore        5         35: ldc           #22                 // int 129786         37: invokestatic  #16                 // method java/lang/integer.valueo f:(i)ljava/lang/integer;         40: astore        6         42: return 

as see integer range between -128 127 uses bipush push byte onto stack integer value. in range -32768 32767 uses short wrapper class sipush. next uses integer. jvm uses byte , short store integer value?

as far i've understood.

as can remaining byte code instruction doesn't store int byte or short first why bipush or short : bipush has 2 bytes 1 opcode , second value. i.e can range between -128 tp 127 (i.e 2 power 8) saves space , time of execution. can see remianing code compiler creates reference of variable integer type

2: invokestatic  #16                 // method java/lang/integer.valueo f:(i)ljava/lang/integer; 

and astore_1 store what's on top of stack i.e reference here local variable 1 similar sipush can store value range (-32768 32767) beacuse it's 3 byte instruction set, 1 byte opcode , rest 2 byte value (i.e can hold 2 power 16)

now why not ldc jvm has per-type constant pool. byte code requires data, of time data large store directly in byte codes. it's stored in constant pool , byte code contains reference constant pool. ldc push constant #index constant pool (string, int or float) onto stack consumes time , cycles here rough comparision between ldc operation , bipush operation

enter image description here enter image description here

jvm bytecode ref here says

where possible, more efficient use 1 of bipush, sipush, or 1 of const instructions instead of ldc.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -