Memory Division & amp; Memory workflow during program code running, memory Division

Source: Internet
Author: User

Memory Division & program code runtime memory workflow, memory Division

Memory Division:
1. Register.
2. Local method area.
3. Method area.
4. stack memory.
    Local variables are stored.
And once the scope of the variable ends, the variable is automatically released.

5. heap memory.
    Storage is an array and an object.(In fact, arrays are objects.) All new objects are in the heap.
Features:
1) each object has a first address value.
2) each variable in the heap memory has a default initialization value, which varies depending on the type.The integer is 0, the decimal number is 0.0 or 0.0f, The boolean type is false, the char type is '\ u0000', and the referenced data type is NULL.
3) garbage collection mechanism.

Differences between global variables and local variables:

A local variable is a variable defined in a method and is released at the end of its scope;

Global variables are variables defined outside the method.

Demo of memory workflow during program code running:

DEMO code:

 1 class  Demo2 2 { 3     public static void main(String[] args)  4     {     5         int[] arr = new int[3]; 6         arr[2] = 5; 7         System.out.print(arr[2]); 8          9     }10 }

 

Demo illustration:

Text description:

1) Open up a memory space for the main function in the stack memory;

2) The main memory space opens up a memory space for the local variable arr;

3) open a memory space in the heap memory to store the object array, and the array is automatically initialized;

4) Pass the first address of the array opened in the heap memory to the local variable arr in the stack memory;

5) modify the value of arr [2;

6) print the value of arr [2] on the console.

 Tips:

1) determine the length of an array;

2) entities in the heap memory will be automatically initialized, but not in the stack memory, and uninitialized in the stack will report an error during compilation;

3) The array name variable opened in the heap memory for the first address of the array space to be passed to the stack memory. The storage value of the array name variable is [I @ hash value; ([indicates that the reference points to the array, I indicates that the reference points to an array of the int type, and @ is followed by a hash value. The first address referenced in the heap memory is obtained by calculating the hash value)

  Common array code exceptions:

1) ArrayIndexOutOfBoundsException: (this exception is passed during compilation and an error is reported during running)

This exception occurs when you access a badge that does not exist in the array.

2) NullPointerException: (this exception is passed during compilation and an error is reported during running)

If the referenced variable does not point to any entity and you are still using it to operate the entity, this exception occurs.

  

 

Related Article

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.