The memory allocation method of the Java primitive Type array is analyzed by byte code.

Source: Internet
Author: User

We know that the objects created in new mode in Java are created in the heap, and the values corresponding to the local variables are stored on the stack. So where is the int [] arr={1,2,3} in Java, where is the int []arr = new int[3] stored,

Let's look at the resulting bytecode file to see how the JVM handles both cases by writing two small examples.

1.int[] arr = new Int[3] Example

 Public class arraytest {    publicstaticvoid  main (string[] args) {        int  newint[3];    }}

The resulting bytecode file is as follows: only the primary information is shown here. Java bytecode instruction information please refer to the Java byte Code instruction

Compiled from "Arraytest.java" Public classArraytest { Publicarraytest (); Code:0: aload_0//load reference to stack from local variable 0, pressing the current object reference into the stack. 1:invokespecial #8//Call An instance of the object method, that is, initialize the method init, here is the corresponding method in the method pool reference, 4:return   Public Static voidMain (java.lang.string[]); Code:0: Iconst_3//press 3 into the stack. 1:newarrayint//The instruction first POPs data 3 from the stack and creates a shape array of size 3 and presses the object reference (address) of the array into the stack. 3: Astore_1//The object reference in the pop-up stack is stored in the local variable 1. 4:return}

2.int arr[] = {A-sample} example:

 Public class arraytest {    publicstaticvoid  main (string[] args) {        //  int[] arr = new int[3];        int [] arr = {A-I};}    }

The resulting bytecode is as follows:

Compiled from ' Arraytest.java ' public class Arraytest {public  arraytest ();    Code:       0:aload_0       1:invokespecial #8                  //Method java/lang/object. " <init> ":() V       4:return public  static void Main (java.lang.string[]);    Code:       0:iconst_3       1:newarray       int//Same as       3:dup     //Copy a stack top value and press it on stack,       4:iconst_0       5: Iconst_1       6:iastore//store 1 of the top of the stack in the position of the array's 0 index       7:dup       8:iconst_1       9:iconst_2      10:iastore  // Place the top 2 of the stack at the 1 index of the array      11:dup      12:iconst_2       13:iconst_3      14:iastore//Place the top 3 of the stack at the position of the array 2 index.      15:astore_1      16:return}

By generating the bytecode we can see that both arrays are created by the Newarr keyword, and the JVM opens up space in the heap and stores references to the array in the stack.

An array in Java is an indication of the space allocated in the heap. But in c,c++ the array is allocated space in the stack.

The memory allocation method of the Java primitive Type array is analyzed by byte code.

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.