One 、JAVA Six storage addresses for
- register register
Inside the processor , It's the fastest memory , But the number is extremely limited . The compiler allocates according to the requirements , It can't be controlled by code , For developers, it's No sense Of . - Stack stack
be located RAM in , The stack pointer moves down to allocate new memory , Move up to free memory . When you create a program , The compiler must know the exact size and lifetime of all the data stored in the stack . some JAVA The data is stored in the stack —— Especially object references , however JAVA Object does not store in it . - Pile up heap
be located RAM in , The advantage over the stack is that you don't need to know how much storage space is allocated from the heap and how long to live .JAVA Objects are stored here . - Static storage static
there “ static state ” Refer to “ In a fixed position ”. Static storage is the storage of data that is always present while the program is running . You can use keywords static To identify that a particular element of an object is static - Constant storage constant
Constant values are usually stored directly within the program code , It's safe to do this , Because they will never be changed . Sometimes , In embedded systems , The constant itself will be separated from the rest , So in this case , You can choose to put it in ROM in . - Not RAM Storage
If the data exists completely outside the program , So it can't be controlled by any program , It can exist when the program is not running .
Two 、 Stack 、 Pile up 、 The contents stored in the method area
Heap area :
- All that's stored is objects , Each object contains a corresponding class Information about .(class The purpose is to get operational instructions )
- jvm There's only one heap area (heap) Shared by all threads , Basic types and object references are not stored in the heap , Just store the object itself .
The stack area :
- Each thread contains a stack area , Only values and objects of the underlying data type and references to the underlying data are stored in the stack
- The data in each stack ( Underlying data types and object references ) It's all private , Other stacks cannot be accessed .
- The stack is divided into 3 Parts of : Basic type variable area 、 Execution context 、 Operation instruction area ( Store operation instructions ).
Method area :
- It's also called static area , It's like a pile , Shared by all threads . The method area contains all the class and static Variable .
- The method area contains elements that are always unique throughout the program , Such as class,static Variable .
3、 ... and 、JAVA Storage of basic data types
There are basic types in common 8 Kind of , namely int, short, long, byte, float, double, boolean, char. This type is defined by int a = 3 To define in the form of , be called Automatic variable . It is worth noting that Automatic variables store literal values , Not an instance of a class .
for example int a = 3, there a It's a point int Type references , Point to 3 This is the literal value , There is no class in the whole process . The data size of the literal value is known , The survival time is known ( Defined in a block , After the block exits , The field value disappears ), In stack .
Stack has a very important particularity , Data stored in the stack can be shared
Suppose we define it at the same time int a = 3; int b = 3;
The compiler processes int a = 3; First, it creates a variable in the stack as a References to , Then find out if there is a literal value of 3 The address of , Did not find , Just open up a store 3 The address of this face value , And then a Point to 3 The address of . And then deal with int b = 3; After creating b After referencing variables for , Because there are already 3 This is the literal value , It will be b Direct to 3 The address of . such , And that's what happened a And b At the same time, they all point to 3 The situation of .
Special note , The reference to this literal is different from the reference to a class object . Suppose two class object references point to one object at the same time , If an object reference variable modifies the internal state of the object , Then another object reference variable immediately reflects the change . contrary , Modify the value of a literal by reference to it , Does not cause the value of another reference to this literal to change .
As in the above example , We're done defining a And b After the value of , Re order a=4; that ,b It's not equal to 4, Or is it equal to 3. Inside the compiler , encounter a=4; when , It will re search the stack for 4 The face value of , without , Reopen address storage 4 Value ; If there are already , Will directly a Point to this address . therefore a Changes in values do not affect b Value .