Program memory analysis and program memory Analysis
It has been several years since I learned the programming language. However, the call relationship between the syntax in the programming language in the computer memory has been unclear until recently, I learned something about Java. So here is a summary.
First, let's look at a piece of code:
Class Point {private double x; private double y; Point (double x1, double y1) {x = x1; y = y1;} public double getX () {return x ;} public double getY () {return y;} public void setX (double I) {x = I;} public void setY (double j) {y = j ;}} class Circle {private Point o; private double radius; Circle (Point p, double r) {o = p; radius = r;} Circle (double r) {o = new Point (0.0, 0.0); radius = r;} boolean contains (Point p) {double x = p. getX ()-o. getX (); double y = p. getY ()-o. getY (); if (x * x + y * y> radius + radius) return false; else return true;} public void setO (double x, double y) {o. setX (x); o. setY (y);} public Point getO () {return o;} public double getRadius () {return radius;} public void setRadius (double r) {radius = r ;} public double area () {return 3.14 * radius;} public class TestCircle {public static void main (String arg []) {Circle c1 = new Circle (new Point (1.0, 2.0), 2.0); Circle c2 = new Circle (5.0 ); // slightly ***********************
}}
When the program executes the phrase "Circle c1 = new Circle (new Point (1.0, 2.0), 2.0);", the program resolves the statement one by one from inside to outside.
The memory usage is as follows:
Medium, the large box on the left indicates the stack in the memory, and the right indicates the heap in the memory.
When the program creates an object, it always calls a constructor of the object to assign values to the object parameters.
In the figure, the large box on the left represents the stack in the memory, and the right represents the heap in the memory.