directory

  • knowledge
  • Written Java code, how does it work
  • How does it become a JVM process once it’s running
  • How does the JVM process run our code
  • conclusion

knowledge

Today to share a knowledge point, that is usually we write good Java code, how it runs, and it runs after how to become a JVM process? How does the JVM process then run our code? These questions must be a lot of Java writing for a long time brothers may not be clear, today we will have a good talk about these questions.

Written Java code, how does it work?

First of all, we usually develop a Java system, in fact, in their own local Intellij IDEA to write a lot of Java code, these Java code is actually some files with a. Java suffix, we write Java code is stored in these files with a. Java suffix. It’s no different than writing a.doc document in Word, as shown below.

Then, suppose you write better code, want to put the code to run now, at this time you should need to do a thing, that is compiled, the compiler, it means that, you write good Java code, our computer is look not to understand, must have to write good code compiler into machine language that the computer can understand, is the bytecode, If you use Intellij IDEA, it will automatically compile the code for you, and the compiled bytecode file is the.class suffix, as shown in the following figure.

Now, the key point is, if you want to run the code yourself, you can issue Java commands to the.class bytecode file from the command line, and if you’re using Intellij IDEA, if you click the Run code button, it will automatically compile the code for you. It then runs the.class bytecode file itself directly with Java commands, as shown below.

How does it become a JVM process once it’s running?

When a Java command is issued, it directly starts a JVM process, so that’s how the JVM process is created, and it’s important to note that any Java class system you run is dependent on the JVM process, so when you run your code with a Java command, you must start a JVM process, and the next thing you know, This is handed over to the JVM process, as shown below.

Then the next step, the JVM process must be to do a thing, it is that class loading, which means it must have to write our good class to a each a loaded into memory, then load including our * *. The class bytecode instruction * *, and then to run the code we wrote, this time the first class is a main method to load the main class, This class is the first to be loaded into memory, and the bytecode instructions in this class are the first to be executed, as shown below.

So this time the JVM, there are two things can lead to, is a method of area, or now Java area, the new version can also be called metadata is the area is a class where we loaded into memory, the other is the program counter, is this thing we are stored to run under a bytecode instruction, one might say, What is this bytecode instruction? You can probably think of bytecode instructions as code written in our methods, which are converted to bytecode instructions that run our code logic, as shown below.

To give you a slightly more graphic example, let’s look at the following code for a class that contains the main method:

public class Application {     
    public static void main(String[] args) {  
        // A lot of code}}Copy the code

How does the JVM process run our code?

So when our JVM starts, does the Application class have to be loaded and thrown into its own method area? And then does the JVM process have to execute line after line of the main method? How do I execute the code? When the code is compiled into bytecode instructions, we need to reference bytecode instructions one by one through the program counter, and then run each instruction through the CPU, which is equivalent to running each line of our code, as shown in the following figure.

In the bytecode instruction process, you may find that your method will have some local variables, and also create some objects, local variables will refer to the object you created, the code may look like this:

public class Application {     
    public static void main(String[] args) { 
    User user = newUser(); }}Copy the code

What happens at this point? It’s also very simple, for that User User, that’s a local variable in a method, and that’s the kind of variable that other code instructions, at runtime, throw into a place called the stack memory, and that’s where the local variable in your method code runs, and then what about the object that you create with new User()? Such objects are placed in the heap, which is dedicated to objects, and variables in the stack refer to objects in the heap, as shown below.

Finally a concept is the thread, the JVM process will actually default open is a main thread to run the code in the main method, which is the main method in the bytecode instruction, you also can open up the code to other threads to run concurrently other code, so it should also introduce the concept of a thread to execute code, the following figure.

conclusion

At this point, we can summarize how our Java code is run step by step, and how it is run continuously as a JVM process. First, our Java code has to be compiled into bytecode, and second, we have to start a JVM process with Java commands. The JVM process will then load our class into the method area and start a default main thread that points to each instruction in our main method through the program counter and runs the instructions through the CPU.

Then our method during the running of code instructions, throw stack for local variables will be memory, for creating objects will be thrown in the heap memory, as a way to run out, then the method of variable from the stack memory, then the method created during the period of operation object in the heap memory, no reference, After some time, a garbage collection thread of the JVM will also collect the unused objects. This is how the JVM operates.

END

Scan code for free 600+ pages of Huisei teachers original fine articles summary PDF

Summary of original technical articles