Focus on “Java Backend Technology Full Stack”

Reply “000” to get a large number of e-books

Know the JVM

What is the JVM

The FULL name of JVM is Java Virtual Machine, also known as Java Virtual Machine. It recognizes files with the.class suffix and can parse its instructions, eventually calling functions on the operating system to do what we want.

Some partners may have learned C++, C++ developed programs, compiled into binary files, can be directly executed, the operating system is able to recognize.

But we open Java program is not the same, using javac command compiled. Class file, the operating system is not recognized, the corresponding JVM to do a conversion, the operating system can recognize.

Why can’t we just run the compiled binaries directly on the operating system, like C++? Rather than make a virtual machine in the middle of the program and operating system?

This is where the JVM excels. As we all know, Java is a highly abstract language, offering a range of features such as automatic memory management. These features are unlikely to be implemented directly on the operating system, so a series of transformations are required by the JVM.

When you start learning Java, you know that there is a Write Once, Run Everywhere. So we write a Java file that we compile into a.class file that we can run on a variety of systems.

In fact, there is a premise, we need to install the corresponding JVM on the corresponding operating system, and then our.class file will be ready to run.

For example, JDK installation versions are available for Windows and Linux.

! [](https://static001.geekbang.org/infoq/ff/ffe7120e65d4aaec957e012ee232b28b.jpeg?x-oss-process=image/resize,p_80/auto-or ient,1)

Know the JDK

The Java Development Kit (JDK) is a software Development Kit for Java developers from Sun corporation (acquired by Oracle). Since the introduction of Java, the JDK has become the most widely used Java SDK (Software Development Kit).

According to unofficial surveys, JDK8 is currently the most used version.

JDK14 will receive security updates in April and July, then be replaced by the non-LTS version of JDK 15 that expires in September. JDK14 includes 16 new features such as JDK Flight Recorder event flow, pattern matching and switch expressions.

Since JDK9, Oracle has adopted a new release cycle: release every 6 months and LTS release every 3 years. JDK14 is the fourth non-LTS release after JDK9. The latest LTS release is JDK11.

Here are the JDK versions

! [](https://static001.geekbang.org/infoq/37/371aaf2e5f64d8338955c569bf757a19.jpeg?x-oss-process=image/resize,p_80/auto-or ient,1)

Keep an eye out for JDK updates and new features.

Official website: www.oracle.com/java/

The JDK installation is omitted here.

JDK, JRE, and JVM

We’ve talked about JDK and JVM concepts,

The Java Runtime Environment (JRE) is an indispensable Runtime Environment for running programs based on the Java language. It also allows Java developers to distribute their applications to users for use.

What is the relationship between the three?

See the official website for more information on the relationship between the three

Docs.oracle.com/javase/8/do…

! [](https://static001.geekbang.org/infoq/03/0318d30ae3b07d2f12b83bad0e7d09c8.jpeg?x-oss-process=image/resize,p_80/auto-or ient,1)

The JDK includes the JRE, as well as the JDK, and the JRE includes the JDK. Scope relationships: JDK>JRE>JVM

“.java” files to “.class” files

javacThe command

Write a helloWorld.java file

! [](https://static001.geekbang.org/infoq/14/144e9e8150223a5cf5a3d6905ac71d3a.jpeg?x-oss-process=image/resize,p_80/auto-or ient,1)

The content is a Java primer

public class HelloWorld { public static void main(String[] args) { System.out.println("Hello world"); }}Copy the code

Open CMD, go to the current directory, and run the command

javac HelloWorld.java
Copy the code

Helloworld.class is compiled

! [](https://static001.geekbang.org/infoq/00/004bd89b61073dc1c0fe99c324ca519b.jpeg?x-oss-process=image/resize,p_80/auto-or ient,1)
! [](https://static001.geekbang.org/infoq/a3/a31c60b03b8499278d4e7df5294413df.jpeg?x-oss-process=image/resize,p_80/auto-or ient,1)

The build process

What does this Javac command procedure actually do?

Javac does this roughly behind the scenes

! [](https://static001.geekbang.org/infoq/f9/f94f2141d01d396dfbf682f8d1731009.jpeg?x-oss-process=image/resize,p_80/auto-or ient,1)

This process

! [](https://static001.geekbang.org/infoq/f7/f7be485c6a77a6dd576fc38551a60b85.jpeg?x-oss-process=image/resize,p_80/auto-or ient,1)

1. Lexical analysis

Read the source code, read byte by byte, find the keywords we defined (such as if, else, for, while in Java, identify which if is a valid keyword, which is not), this is the process of lexical analysis by the lexical analyzer. The result is to find a normalized Token stream from the source code.

2. Grammatical analysis

The Token stream after lexical analysis is parsed through the parser. This step checks whether the keyword combinations again conform to the Java language specification (such as if followed by a Boolean judgment expression). The result of lexical analysis is an abstract syntax tree that conforms to the Java language specification.

3. Semantic analysis

Semantic analysis is performed by semantic analyzer. Phonetic analysis is mainly to transform some difficult and complex grammar into simpler grammar, resulting in the simplest grammar (such as foreach into a for loop, so that there are annotations, etc.), and finally form a annotated abstract grammar tree, which is closer to the grammar rules of the target language.

4. Generate bytecode

Bytecode generation through a bytecode producer, bytecode generation from an annotated syntactic abstraction tree, is the transformation of one data structure into another. Finally, generate the.class file we want.

View the contents of the class file in hexadecimal

I’m only using Notepad++, select text → plug-in →Converter→ASCII->HEX

! [](https://static001.geekbang.org/infoq/92/927c6e1a846f3c015f3b0adfd918f3ea.jpeg?x-oss-process=image/resize,p_80/auto-or ient,1)

The class file starts with

CAFEBABE

To learn the meaning of the hexadecimal bytecode here refer to

Docs.oracle.com/javase/spec…

Javap View the contents of the class file

Javap is a Java Class file shredder that can decompile (that is, decompile files compiled by JavAC) or view bytecodes generated by the Java compiler.

Create a new user.java source file and generate user.classs after javac compilation.

package com.tian.demo.test;

public class User {
    private int age = 22;
    private String name = "tian";

    public int addAge() {
        return age = age + 1;
    }

    public static void main(String[] args) {

    }
}
Copy the code

Use the javap command

javap -v User.class >log.txt
Copy the code

Open the log. TXT

Classfile /D:/workspace/new/demo/src/main/java/com/tian/demo/test/User.class Last modified 2020-11-5; size 441 bytes MD5 checksum 2fa72d3f53bd9f138e0bfae82aba67e3 Compiled from "User.java" public class com.tian.demo.test.User minor version: 0 major version: 52 flags: ACC_PUBLIC, ACC_SUPER Constant pool: #1 = Methodref #6.#21 // java/lang/Object."<init>":()V #2 = Fieldref #5.#22 // com/tian/demo/test/User.age:I #3 = String  #23 // tian #4 = Fieldref #5.#24 // com/tian/demo/test/User.name:Ljava/lang/String; #5 = Class #25 // com/tian/demo/test/User #6 = Class #26 // java/lang/Object #7 = Utf8 age #8 = Utf8 I #9 = Utf8 name #10 = Utf8 Ljava/lang/String; #11 = Utf8 <init> #12 = Utf8 ()V #13 = Utf8 Code #14 = Utf8 LineNumberTable #15 = Utf8 addAge #16 = Utf8 ()I #17 = Utf8 main #18 = Utf8 ([Ljava/lang/String; )V #19 = Utf8 SourceFile #20 = Utf8 User.java #21 = NameAndType #11:#12 // "<init>":()V #22 = NameAndType #7:#8 // age:I  #23 = Utf8 tian #24 = NameAndType #9:#10 // name:Ljava/lang/String;  #25 = Utf8 com/tian/demo/test/User #26 = Utf8 java/lang/Object { public com.tian.demo.test.User(); descriptor: ()V flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial #1 // Method java/lang/Object."<init>":()V 4: aload_0 5: bipush 22 7: putfield #2 // Field age:I 10: aload_0 11: ldc #3 // String tian 13: putfield #4 // Field name:Ljava/lang/String; 16: return LineNumberTable: line 3: 0 line 4: 4 line 5: 10 public int addAge(); descriptor: ()I flags: ACC_PUBLIC Code: stack=3, locals=1, args_size=1 0: aload_0 1: aload_0 2: getfield #2 // Field age:I 5: iconst_1 6: iadd 7: dup_x1 8: putfield #2 // Field age:I 11: ireturn LineNumberTable: line 8: 0 public static void main(java.lang.String[]); descriptor: ([Ljava/lang/String;)V flags: ACC_PUBLIC, ACC_STATIC Code: stack=0, locals=1, args_size=1 0: return LineNumberTable: line 13: 0 } SourceFile: "User.java"Copy the code

Magic and class file version Constant pool access flag class index, parent class index, interface index Field table collection method table collection property table collection

The JVM can then read the user.class file for parsing and other operations.

So that’s our Java file to class file.

Stay tuned for a series of JVM articles to follow

Recommended reading:

Java Web Enterprise Project in Action. PDF

MySQL Developer’s GUIDE to SQL. PDF

Follow public account “Java Backend Technology full Stack”

Free access to 500G of the latest learning materials