The understanding of the stack in Java, the memory is divided into two----1. Heap memory, 2. Stack memory * * * stack space is automatically allocated and released by the system; * * Heap space is manually applied and released. Advantage: Stacks are faster than heap access, data can be shared note: Stacks are to hold object references, and the heap is to store the Java object data in the * * stack variable to the variable in the heap-----can be interpreted as Java pointers----When we declare basic data types and object reference variables, it is allocated by stack memory. When a variable is defined in a section of code, Java allocates memory space for it in the stack, and if the variable exits the scope, Java automatically frees the variable to occupy the memory and use it for another. * * For example, when we define a variable Object obj = null, the memory space is allocated by the stack memory. ----When we create a new object in the novel, we allocate the memory space in the heap memory, which holds the content of the object. Variable data that mainly holds some basic types in the stack (int short long byte double float double boolean char)
Pay special attention to ... : Constants are stored in a constant pool of string data are constants (the String class is final, cannot be inherited), and the base type data that is modified by static final is constant. Eg:int a =1----This 1 is not a constant here, it is assigned to the stack and placed on an instruction pointer; When you define an int b=1 again, if you have the same value of 1, then this B will point directly to the 1 pointer so that they share the data. But any change between them will not affect the other values. Do not confuse the values stored in the stack with the values stored in the constant pool. A string constant object is a constant that is stored in a constant pool, or a variable that is modified by the static final.
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