In any programming language, whether it is a basic type or a reference type, regardless of its scope, it must be allocated a certain amount of memory space, and the Java language is no exception
Java's data types can be divided into two types: the base type (the value of the variable holding the data itself) and the reference type (which is a reference to an object, not the object itself);
The basic types are Boolean, float, double, int, long, short, Byte, and Char, and in the Java programming language all but the base type are reference types such as: class type, array type, and so on.
In computer memory mainly from four places: Heap segment (heap area), stack segment (stack), Codesegment (code area), data segment Different places store different data: The heap area is mainly stored in the Java program to create all the reference types are placed in it, the main storage of Java program to run the local variables required, the parameters of the method, the object reference and the results of intermediate operations and other data; Code area main Java code The data area mainly stores static variables and global variables, and the following examples are given to discuss their specific mechanisms.
As an example:
public class Student {private string name;private int age;public Student (String name,int age) {this.name = Name;this.age = Age;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;}}
public class Test {static int i = 10;public static void Main (string[] args) {Student S1 = new Student ("Feng", 21);}}
When the program is running, its computer memory is distributed roughly as follows:
Java Fundamentals: Memory allocation mechanism