Java-Preliminary Understanding-fourth chapter-Memory plot

Source: Internet
Author: User

Stack memory and heap memory are memory partitions that always exist. They are used in conjunction with each other.

In the partitioning of data types, the array is divided into reference data types, which are derived from the way that local variables get data from the array entities. Data types are divided into basic data types and reference data types. The reference data type focuses on "references," and "basic" and "reference" differ in mechanism. Classes and functions are also referred to as reference data types, indicating that they are also independent and can be called, specifically, to see the previous functional function call.

The code in is not necessarily in the main function, but the variable must have its own function.

Let's analyze the process: The five-part memory that the CPU divides is always there, first in the stack memory, loaded into the function and the local variables that belong to the function. To make it clear that the basic data assignment is different from the reference data assignment, we distinguish between the two when we define the local variables, one is int x and the other is int[] X. One is the int type and the other is the [] data type, but in order to represent what type of data is stored in the array entity, the type int that stores the data is prepended to the type of []. At the same time, the heap memory to prepare the corresponding data, first according to the keyword new, in the heap memory to open an array type of space, the size of the space is 3, at the same time give 3 small space given a space address (first given address, then a given value) and the default assignment, and finally the corresponding spatial address to local variables, The data is searched by the local variables in the array itself. This process, which can be understood as a local variable, is not itself, and references the data from the array.

For the second statement int[] Y=new int[3], Y and X refer to never the same array, which is another redefined data.

Then the x[]=9, y[]=34, are assigned to elements in the array and have no relation to the local variables in the stack.

Finally, write the x=y statement, X and y are local variables, the assignment operation between them is in the stack memory operation, the memory of y of the space address to X, and then x go to the following array to search for the corresponding location of the data, the data becomes garbage. Then System.out.println (X[0]), the output is 34, because the above array has become garbage.

As long as there are local variables pointing, then the array is not garbage. Multiple reference variables can point to an entity at the same time.

Java-Preliminary Understanding-fourth chapter-Memory plot

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.