Java FAQ _07JVM Architecture (006) _java Heap and stack what's the difference

Source: Internet
Author: User

Click to enter _ more _java thousand ask

1. What is the difference between Java heap and stack?

Understanding JVM Memory Management Look here: How the JVM manages memory
In the article "How the JVM manages memory", the PC Register (counter PC registers), method area, local methods stack (native method stacks), stack (stacks), Heap memory area, where the stack (stacks), Heap is a very important two part of Java memory management, the specific differences are as follows:

2. Is the basic data type stored in the stack?

The base data type is stored directly in the stack because it is fixed in length and requires less space (string is a special type, its value is stored in the heap, but the pool reaches the same access speed as the stack, see here: How the string is stored in memory). The object is relatively large, so only a 4btye reference address (logical address) is stored in the stack.

Instance:

int3 ;int3 ;

The analysis is as follows:

  1. The compiler processes int a = 3 First, it creates a reference to a variable in the stack, and then finds out if there is a value of 3 in the stack, and if it does not, it stores the 3 in and then points a to 3.

  2. then the int b = 3 is processed, and after the reference variable of B is created, because there are already 3 values in the stack, B points directly to 3. In this case, A and B both point to 3.

  3. At this point, if a=4 again, then the compiler will re-search the stack for a value of 4, if not, then store 4 in, and a point to 4, if already, then direct a to it. Therefore the change of a value does not affect the value of B.

Java FAQ _07JVM Architecture (006) _java Heap and stack what's the difference

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.