public class CodeBlock02
{
{
System.out.println("第一代码块");
}
public CodeBlock02()
{
System.out.println("构造方法");
}
{
System.out.println("第二构造块");
}
public static void main(String[] args)
{
new CodeBlock02();
new CodeBlock02();
new CodeBlock02();
}
}
在这里, new CodeBlock02()
; 或者换成 CodeBlock02 code = new CodeBlock02();
他们是一样的吗!
First, let’s clarify a few concepts. Java code runs in jvm, and the memory area of jvm is divided into several modules:
Program Counter (Program Counter Register): The program counter is a relatively small memory area used to indicate which line of bytecode executed by the current thread has been executed. It can be understood as the line number indicator of the current thread. . When the bytecode interpreter is working, it will fetch a statement instruction by changing the value of this counter.
Virtual Machine Stack (JVM Stack):When each method of a thread is executed, a stack frame (Statck Frame) will be created. The stack frame stores local variable tables, operation stations, dynamic links, methods Exit, etc., when the method is called, the stack frame is pushed into the JVM stack, and when the method execution is completed, the stack frame is popped out of the stack.
Native Method Stack: The native method stack is the same as the virtual machine stack in terms of function, operating mechanism, exception types, etc. The only difference is that the virtual machine stack executes Java methods, while native methods The stack is used to execute native methods. In many virtual machines (such as Sun's JDK default HotSpot virtual machine), the native method stack and the virtual machine stack are used together.
Heap: The heap area is the most important area to understand the Java GC mechanism, bar none. The heap area is the largest piece of memory managed by the JVM. The heap area is also the main memory area managed by the Java GC mechanism. The heap area is shared by all threads and is created when the virtual machine starts. The heap area exists to store object instances. In principle, all objects are allocated memory on the heap area (but in modern technology, this is not so absolute, and some objects are allocated directly on the stack).
Method Area: (also known as the permanent generation), the method area is an area shared by each thread, used to store class information that has been loaded by the virtual machine (that is, the information that needs to be loaded when loading a class, Including version, field, method, interface and other information), final constants, static variables, code compiled by the compiler on-the-fly, etc.
Direct Memory: Direct memory is not memory managed by JVM. It can be understood that direct memory is machine memory other than JVM. For example, if you have 4G of memory and JVM occupies 1G, then the rest 3G is direct memory. There is a memory allocation method based on channel and buffer in JDK. The native function library implemented in C language is allocated in direct memory and referenced by DirectByteBuffer stored in the JVM heap. Since direct memory is limited by the memory of this machine, an OutOfMemoryError exception may also occur.
After understanding these basic concepts, let’s look at the areas where the questioner has doubts. In fact, what the questioner is wondering about is what object references are in Java and what is their relationship with the instantiation process of objects.
Don’t worry, let’s first analyze how a reference is implemented in Java:
A Java reference access involves three memory areas: JVM stack, heap, and method area.
Taking the simplest local variable reference: Object obj = new Object() as an example:
Object obj represents a local reference, which is stored in the local variable table of the JVM stack and represents a reference type data;
new Object() is stored in the heap as instance object data;
The address of the type information of the Object class (interface, method, field, object type, etc.) is also recorded in the heap, and the data executed by these addresses is stored in the method area;
There are many specific implementation methods, handle is one of them, and the relationship is as shown in the figure.
You should understand it when you see this. The information of the class itself, class instance data, and reference information pointing to the object are placed in the method area, stack area, and heap area of java respectively.
In the question’s example:
code is a reference stored in the local variable table, which points to the object instance data in the heap. This object instance data is obtained through
new CodeBlock02()
.Be more specific:
To sum up, code is the "remote control" you use to receive the instance produced by new. It points to the specific location of this object in the heap area.
You need to understand java’s quotes
CodeBlock02 code = new CodeBlock02();
The one on the left is called a variable of type CodeBlock02.
The one on the right is called an object of type CodeBlock02.
You can also let this variable point to two different objects of the same type in turn.
You can even make variables of this type point to objects of subclasses of this type:
You can also call methods directly on the object from new like this:
Both are declaration objects. What the poster is asking about should be assignment
If you don’t continue to operate on this value later, it will be the same whether you assign it or not
new CodeBlock02() // If no value is assigned after declaration, there is no way to continue operating on this object
CodeBlock02 code = new CodeBlock02(); // Assign the declared object to a variable, and subsequent operations can be performed
The one on the left is the reference variable of the object, and the one on the right is the object actually allocated in the memory.