JVM byte code integer-to-stack directive (Iconst, Bipush, Sipush, LDC)

Source: Internet
Author: User

Official website: http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html

Original address: http://www.linmuxi.com/2016/02/25/jvm-int-pushstack-01/

This article mainly share under the JVM in the type of int value of the instruction into the stack, according to the range of int values JVM into the stack bytecode instructions are divided into 4 categories, the following are described below the four classes of instructions.

Preface

When the int value -1~5 takes the iconst instruction, takes the value -128~127 to use the bipush instruction, the value -32768~32767 uses the sipush instruction, takes the value - 2147483648~2147483647 adopts the LDC Directive.

Iconst

When int is -1~5 , the JVM uses the iconst directive to push the constants into the stack.
Defining Test.java files

1  Public Static void Main (string[] args) {2     int i = 5; 3     int j =-1; 4 }

View class files

1  Public Static void Main (java.lang.string[]); 2 Code: 3 0: iconst_54 1: Istore_15 2: iconst_m16 3 : istore_2 7 return 8 }

Analysis class file, int value 0~5 when the JVM uses Iconst_0, iconst_1, Iconst_2, Iconst_3, Iconst_4, iconst_5 instructions to press the constant into the stack, the value of 1 when the use of Iconst_ The M1 directive pushes constants into the stack.

Bipush

When int is -128~127 , the JVM uses the bipush directive to push the constants into the stack.
Defining Test.java files

1  Public Static void Main (string[] args) {2     int i = 127; 3 }

View class files

1  Public Static void Main (java.lang.string[]); 2 Code: 3 0:bipush 1274 2: Istore_15return6 }

You can see that the third line of the above code is using the bipush instruction to press the constant 127 into the stack.

Sipush

When int is -32768~32767 , the JVM uses the sipush directive to push the constants into the stack.
Defining Test.java files

1  Public Static void Main (string[] args) {2     int i = 32767; 3 }

View class files

1  Public Static void Main (java.lang.string[]); 2 Code: 3 0:sipush 327674 3: Istore_15return6 }

You can see that the third line of the above code is using the sipush instruction to press the constant 32767 into the stack.

LDC

When int is -2147483648~2147483647 , the JVM uses the LDC directive to push the constants into the stack.
Defining Test.java files

1  Public Static void Main (string[] args) {2     int i = integer.max_value; 3 }

View class files

1  Public Static void Main (java.lang.string[]); 2 Code: 3 // int 2147483647 4 2: Istore_15return6 }

As you can see in the third line of the above code, the 2147483647 constants are pressed into the stack with the LDC directive, and it is important to note that the LDC Directive obtains the value from the constant pool, that is, in this range (-2147483648~2147483647 the int value inside is stored in the constant pool.

If the understanding is wrong also hope sensible wise to point out.

JVM byte code integer-to-stack directive (Iconst, Bipush, Sipush, LDC)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.