All these days I've been looking for a chance to tidy up the knowledge of Java memory, after all, any knowledge involves this aspect. Java memory analysis is a major focus of Java learning.
Here we go to the formal topic discussion:
We know that Java memory is roughly divided into three blocks:
Let's take a general look at the data storage for each of the Java partitions:
Stack area: Mainly for the method service, there are a lot of methods stack frame, in the method stack frame to open up a local variable space, the basic type of basic data type data size to open up space, and reference type open four bytes size.
Heap Area: The object data that is created is primarily stored.
Method Area: The main storage load class, static variables, static initialization blocks, constants, and program code to run.
Let's analyze a program that will help us learn more about Java memory:
First the JVM enters from the main method to load the Mydog class into the method area and then loads the animal class into the method area and creates space for the static variable MyAnimal in the method area and initializes it by default.
The dog type object is then created in the heap area: 1 first opens up space for the parent class property and initializes it by default, and then calls the parent class construction method to initialize.
2 re-creating new properties for subclasses re-calling sub-class construction method
3 The new methods and subclasses that inherit the parent class of the subclass are further open space. The dog type object is created.
The MyAnimal reference is then directed to the newly created dog type object (also called upward transformation).
The MyAnimal reference then calls the subclass dog to inherit the parent class's Method Setweight ().
Finally call the subclass dog to inherit the parent class's Method Getweight () and print it out.
Ps: It is worth noting that the upward transformation is myanimal=new Dog ();
1. The MyAnimal reference does not access the parent class's private properties (and methods).
2. The MyAnimal reference does not access the subclass new properties (and methods), and you can access the inherited properties and methods.
A discussion on the analysis of Java memory