A simple understanding of memory in Java
1、 JAVA memory partitioning
It is divided into five parts. You can refer to this note for a brief understanding:
https://www.cnblogs.com/unleashed/p/13268027.html
2、 Start with the memory of the array
Memory map of an array
First, we have such a set of codes:
1 public class HelloWorld{
2 public static void main(String[] args){
3 int[] array = new int[2];
4 System.out.println(array);
5 System.out.println(array[0]);
6 System.out.println(array[1]);
7 array[0]= 1;
8 array[1]= 2;
9 System.out.println(array);
10 System.out.println(array[0]);
11 System.out.println(array[1]);
}
}
Look at this picture:
Memory diagram of two arrays
1. New array
If you add such a statement to the main method just now
int[] array2 = new int[10];
At this time, we need to remember that as long as new is created, it will open up a new space in the heap, which can also be said to be a new memory space
2. Delivery address
What if this statement is added?
int[] array2 = array;
At this time, there is still only one array in the heap. Only the address value of array is passed to array2, because their address values are equal. When array2 is assigned a value, the changed content is the content in the original array, that is, when two references point to the same array
3、 Look at the memory of the object
Memory map of an object
First, there must be a piece of code
public class student{
String name;
String ssex;
int age;
public void study(){
System.out.println("正在学习。。。。");
}
public void eat(){
System.out.println("正在吃饭。。。。");
}
}
Since it is an object, there must be a piece of code to use this student class
public class TestStudent{
public static void main(String[] args){
Student stu = new Student();
System.out.println(stu.name);
System.out.println(stu.ssex);
System.out.println(stu.age);
stu.name = "小杜";
stu.age = 20;
stu.ssex = "男";
System.out.println(stu.name);
System.out.println(stu.ssex);
System.out.println(stu.age);
stu.study();
stu.eat();
}
}
Then from the picture
Memory map when two objects use the same method
比如:
Student stu = new Student();
Student stu2 = new Student();
……
All refer to the same block address space of the same block of methods in the method area
Two references point to the memory map of the same object
Student stu = new Student();
Student stu2 = stu;
……
Find the address value of stu, and then call the method according to the address value of stu
Use the object type as an argument to the method
比如代码中有这么两三行
public static void method(Student stu){
System.out.println(stu.eat);
……
}
When an object is used as a parameter of a method, what is actually passed into the method is the address value of the object
Use the object type as the return value of the method
public static Student eat(){
Student stu =new Student();
stu.study();
stu.name = "小杜";
return stu;
}
When an object type is used as the return value of a method, the return value is actually the address value of the object
4、 String constant pool
String constant pool: the double quoted string written directly in the program is in the string constant pool
So:
For basic types, = = compares numeric values
For reference types, = = compares address values
Because the content is not changeable, it can be shared
Moreover, the effect of string is equivalent to char [] array, but the underlying principle is byte [] array, so it will be automatically converted into byte [] array in the stored process
Borrow an online map
By the way, static keyword
When accessing static member variables according to the class name, the whole process has nothing to do with the object, only with the class.
5、 Memory map in inheritance
Image source network:
That is, the parent class space takes precedence over the generation of child class objects. Each time a child class object is created, the parent class space is initialized first, and then the child class object itself is created.