Problem introduction
Here we first define a very Nonstandard
Of “ book ” class , In order to facilitate the demonstration of the class .
class Book{
String name; // Title
double price; // Price
public void getInfo(){
System.out.println("name:"+name+";price:"+price);
}
}
Two properties and a method are defined in this class , Of course, it is also possible to define multiple and classes and multiple methods .
Classes are now defined , But to use it, a class must instantiate the object , There are two formats for defining objects :
// Declare and instantiate objects : Class name Object name = new Class name ()
Book book = new Book();
// Complete declaration and instance operations step by step :
// |- Declare objects : Class name Object name = null;
Book book = null;
// |- Instantiate objects : Object name = new Class name ();
book = new Book();
Object is of reference data type , The biggest difference from the basic data type is that the reference data type needs to be Memory allocation , And keywords new
The main function is to open up memory space , In other words, as long as the reference data type is used, the keyword must be used new
To open up space . Sometimes we need to operate on object properties , So how is the stack memory allocated ? Now let’s analyze the process .
Heap memory and stack memory
If you want to analyze the process of object operation in memory , First of all, we should understand the concept of two memory spaces :
- Heap memory : Save the attribute content of each object , Heap memory needs keywords new In order to open up .
- Stack memory : What is saved is the address of a piece of heap memory .
Heap memory is easy to understand , One might wonder why there is stack memory , for instance , For example, there are many classrooms in the school , Each classroom has a house number , There are many desks and chairs in the classroom , This number is like an address , The teacher asked Xiao Ming to go to a classroom to get things , The teacher must tell Xiao Ming the room number to get it , That’s why the address has to be stored in one place , And this place in the computer is stack memory .
Object null attribute
Let’s first instantiate an object , And set no value for its properties
public class Test{
public static void main(String args[]){
Book book = new Book();
book.getInfo();
}
}
The operation results are as follows :
name:null;price:0.0
The memory change diagram is as follows :
Use keywords new Just open up a space in the stack memory to store book object , And point to a space in heap memory , It is not assigned a value at this time , So always point to the default heap memory space .
Operands properties
We first declare and instantiate Book class , And give examples of book Objects manipulate their property content .
public class Test{
public static void main(String args[]){
Book book = new Book();
book.name = " In depth understanding of JVM";
book.price = 99.8;
book.getInfo();
}
}
The results of the compilation are as follows :
name: In depth understanding of JVM;price:99.8
The memory change diagram is as follows :
Instantiate objects step by step
The sample code is as follows :
public class Test{
public static void main(String args[]){
Book book = null; // Declare objects
book = new Book(); // Instantiate objects
book.name = " In depth understanding of JVM";
book.price = 99.8;
book.getInfo();
}
}
Obviously, the result must be the same as before
name: In depth understanding of JVM;price:99.8
There’s no difference on the surface , But the memory allocation process is different , Let’s analyze
In any case, just use new We must open up new heap memory space , Once the heap memory space is opened up , It must contain all the properties defined in the class , In this case, all attribute contents are the default values of their corresponding data types .
Intuitively, the stack memory must first point to a null, Then wait to open up a new stack memory space before pointing to its property content .
NullPointerException Appearance
So if you use an object that is not instantiated , There is one of the most common and painful anomalies NullPointerException
, Like the code below
public class Test{
public static void main(String args[]){
Book book = null;
// book = new Book(); // This step of instantiation is annotated
book.name = " In depth understanding of JVM";
book.price = 99.8;
book.getInfo();
}
}
There is no error in the compilation process , Because only syntax errors break at compile time , This kind of logical error can be compiled successfully , But it will throw out when it is executed NullPointerException abnormal .
Running results :
Exception in thread "main" java.lang.NullPointerException at language.Test.main(Test.java:19)
Null pointer exception is one of the most frequently encountered exceptions , As long as it is a reference data type, it can appear . The occurrence of this anomaly is also easy to understand , It’s like you said you were chased by a dinosaur today , Dinosaurs were extinct centuries ago , There can’t be… In real life , Of course, people will think that what you said is a lie . It’s the same in the program , Objects that are not instantiated directly call their properties or methods , I’m sure there will be an error .
Citation data analysis
Reference is the whole thing java The core essence of , A quotation is similar to C++ The concept of pointer in , But it’s simpler than the concept of a pointer .
A simple example , For example, Li Hua’s nickname is Xiaohua , Li Hua asked his teacher for a day off , The teacher asked who asked for leave today , Li Hua asked for leave and Xiao Hua asked for leave , Xiao Hua is another name of Li Hua , They’re both individuals .
If two objects are declared in the code , And use keywords new
The instantiation operation of the two objects is carried out respectively , Then they must occupy their own heap memory space , And it doesn’t affect each other .
for example : Declare two objects
public class Test{
public static void main(String args[]){
Book bookA = new Book();
Book bookB = new Book();
bookA.name = " In depth understanding of JVM";
bookA.price = 99.8;
bookA.getInfo();
bookB.name = "Java Multithreading ";
bookB.price = 69.8;
bookB.getInfo();
}
}
The operation results are as follows :
name: In depth understanding of JVM;price:99.8
name:Java Multithreading ;price:69.8
Let’s analyze the memory changes
Next, let’s look at object reference passing
for example : Object reference passing
public class Test{
public static void main(String args[]){
Book bookA = new Book(); // Declare and instantiate objects
Book bookB = null; // Declare objects
bookA.name = " In depth understanding of JVM";
bookA.price = 99.8;
bookB = bookA; // reference
bookB.price = 69.8;
bookA.getInfo();
}
}
The operation results are as follows :
name: In depth understanding of JVM;price:69.8
Strictly speaking bookA and bookB It contains the address information of the object , So the above reference process belongs to will bookA The address of is assigned to bookB, At this point, the two objects point to the same heap memory space , So any object that changes the heap memory will affect other objects .
A heap memory can be pointed to by multiple stack memory at the same time , But the other way around , A stack memory can only hold the address of one heap memory space .
The generation of garbage
First look at the following code :
public class Test{
public static void main(String args[]){
Book bookA = new Book(); // Declare and instantiate objects
Book bookB = new Book(); // Declare and instantiate objects
bookA.name = " In depth understanding of JVM";
bookA.price = 99.8;
bookB.name = "Java Multithreading ";
bookB.price = 69.8;
bookB = bookA; // Reference relationship
bookB.price = 120.8;
bookA.getInfo();
}
}
The operation results are as follows :
name: In depth understanding of JVM;price:120.8
What happens to the memory in the whole process ? Let’s take a look
In the process, it turns out bookB The heap memory pointed to has no stack, and the memory points to , A piece of heap memory without any stack memory points to becomes garbage , Waiting to be java The recycling mechanism in recycling , After recycling, the space it occupies will be released .
Although in java Automatic garbage collection is supported in , But in the process of code writing, we should try to reduce the generation of garbage space .
Welcome to my blog website ~blog.beifengtz.com