Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand heap, stack and constant pool in Java

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to understand the heap, stack and constant pool in Java". The content in the article is simple and clear, and it is easy to learn and understand. now please follow the editor's train of thought to study and learn how to understand the heap, stack and constant pool in Java.

1. Register

The fastest storage area, which is allocated by the compiler according to demand, is beyond our control in the program.

two。 Stack

Basic types of variable data and references to objects are stored, but the objects themselves are not stored in the stack, but in the heap (objects coming out of new) or in constant pools (string constant objects are stored in constant pools. )

3. Heap

Store all the objects that come out of new.

4. Static domain

Store static members (defined by static)

5. Constant pool

Holds string constants and primitive type constants (public static final).

6. Non-RAM storage

Storage space such as hard disk

Here we are mainly concerned with stack, heap and constant pool, which can be shared for objects in stack and constant pool, but not for objects in heap. The size and life cycle of the data in the stack can be determined, and when there is no reference to the data, the data disappears. The garbage collector is responsible for the collection of objects in the heap, so the size and life cycle do not need to be determined, and have a lot of flexibility.

For strings: references to their objects are stored on the stack, in the constant pool if they have been created at compile time (defined directly in double quotes), and in the heap if they can only be determined at run time (new). For equals equivalent strings, there is always only one copy in the constant pool and multiple copies in the heap.

Such as the following code:

String S1 = "china"; String S2 = "china"; String S2 = "china"; String ss1 = new String ("china"); String ss2 = new String ("china"); String ss3 = new String ("china")

For basic types of variables and constants: variables and references are stored in the stack, and constants are stored in the constant pool.

Such as the following code:

Int i1 = 9; int i2 = 9; int i3 = 9; public static final int INT1 = 9; public static final int INT2 = 9; public static final int INT3 = 9

For member variables and local variables: member variables are variables defined outside the method and internally within the class; local variables are variables defined inside the method or statement block. Local variables must be initialized. Formal parameters are local variables, and the data of local variables exist in stack memory. The local variables in the stack memory disappear as the method disappears.

Member variables are stored in objects in the heap and are collected by the garbage collector.

Such as the following code:

Class BirthDate {private int day; private int month; private int year; public BirthDate (int d, int m, int y) {day = d; month = m; year = y;} omit the get,set method. } public class Test {public static void main (String args []) {int date = 9; Test test = new Test (); test.change (date); BirthDate D1 = new BirthDate;} public void change1 (int I) {I = 1234;}

For the above code, date is a local variable, iQuery ddirection mdirection y is a local variable, and day,month,year is a member variable. Let's take a look at the changes in code execution:

1. The main method starts to execute:

Int date = 9

Date local variables, base types, references, and values are all stored in the stack.

2. Test is an object reference and is stored in the stack, and the object (new Test ()) is stored in the heap.

Test test = new Test ()

3.

Test.change (date)

I is a local variable, and references and values are stored in the stack. When the method change is executed, I disappears from the stack.

4.

BirthDate D1 = new BirthDate (7, 7, 7, 1970)

D1 is an object reference, which is stored in the stack, and the objects (new BirthDate ()) are stored in the heap, where ddimline y is a local variable stored in the stack, and their type is the base type, so their data is also stored in the stack. Day,month,year are member variables that are stored in the heap (new BirthDate ()). When the BirthDate constructor is executed, the dmemmrey disappears from the stack.

After the 5.main method is executed, the date variable, the test,d1 reference will disappear from the stack, and new Test (), new BirthDate () will wait for garbage collection.

Thank you for your reading, the above is the content of "how to understand the heap, stack and constant pool in Java". After the study of this article, I believe you have a deeper understanding of how to understand the heap, stack and constant pool in Java. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report