Blog: bugstack.cn Github: github.com/fuzhengwei/…

Precipitation, share, grow, let yourself and others can gain something! 😄

One, foreword

There have been 22 interviews written so far. How many have you read?

😄 in fact, little Fu Ge is talking about Java core technology under the guise of face to face, exploring what is behind the interview of these core knowledge points.

Do you want to ask some interviewers, are you asking because everyone else is asking, or are you trying to get something out of this? In fact, many interviewers who are not familiar with these techniques will often be crushed by candidates’ answers. Hahahahahahaha. Examples include Mason rotation, open addressing, Fibonacci hashing, heuristic cleaning, Javassist proxy, perturbation function, hash consensus, and more.

Remember, let understand is really understand, than see hydrology, back the answer to cool much! Well, sometimes brains burn!

2. Interview questions

Thanks for the plane, note! , also don’t know how, total feeling some interview is not aggressive, but insulting extremely strong!

Interviewer: Has Xie Ever written Java?

Thank plane: that certainly had written, wrote 3 years many!

Interviewer: What is the relationship between the JDK, JRE, and JVM?

Xie Plane: well J J J, JDK has JRE, JVM seems to be in JRE! ?

Interviewer: What are Client mode and Server mode?

Xie Airplane: HMM! ? What?

Interviewer: Ok, a simple one. How does the JVM work? Have you memorized the answer?

Xie Plane: Goodbye, interviewer!

JDK, JRE, JVM

1. Java Platform Standard (JDK 8)

Oracle has two products that implement Java Platform Standard Edition (Java SE) 8: Java SE Development Kit (JDK) 8 and Java SE Runtime Environment (JRE) 8.

JDK 8 is a superset of JRE 8, and contains everything that is in JRE 8, plus tools such as the compilers and debuggers necessary for developing applets and applications. JRE 8 provides the libraries, the Java Virtual Machine (JVM), and other components to run applets and applications written in the Java programming language. Note that the JRE includes components not required by the Java SE specification, including both standard and non-standard Java components.

The following conceptual diagram illustrates the components of Oracle’s Java SE products:

Description of Java Conceptual Diagram

The relationship between the JDK, JRE, and JVM is clearly defined in the Java platform standards. That is the English introduction above.

  • Oracle has two standard Java platform products, the Java SE Development Kit (JDK) and the Java SE Runtime Environment (JRE).
  • Java Development Kit (JDK), the JDK is provided for the use of Java developers, which contains Java Development tools, including JRE. So if you install the JDK, you don’t need to install the JRE separately. The development tools include compilation tool (javac.exe) packaging tool (jar.exe) and so on.
  • The JRE(Java Runtime Environment) is a subset of the JDK, that is, it contains all the content of the JRE, as well as tools such as compilers and debuggers needed to develop applications. The JRE provides libraries, Java Virtual Machine (JVM), and other components for running the Java programming language, applets, and applications.
  • The JVM(Java Virtual Machine) can be understood as a Virtual computer with basic computing methods. It is mainly responsible for interpreting bytecode files generated by Java programs into Machine instructions on specific system platforms and running them on various platforms.

The JVM we’re running on is already installed in the JDK, although you may have been developing code for a long time and not even noticed. The biggest reason for not noticing is that you haven’t developed any jVM-specific component code

About the platform standard of each JDK version, you can compare and learn by yourself, as follows:

  • Java SE 6 Documentation:docs.oracle.com/javase/6/do…
  • The Java Platform Standard Edition 7 Documentation:docs.oracle.com/javase/7/do…
  • The Java Platform Standard Edition 8 Documentation:docs.oracle.com/javase/8/do…

2. JDK directory structure and functions

By default, there will be jdk1.8.0_45 and jre1.8.0_45 folders after installing JDK. In fact, there will be a JRE folder in the JDK file, the structure of the two JRE folders is the same.

  • Bin: a collection of EXE executable files, such as java. EXE, javac. EXE, javadoc. EXE, and key management tools.
  • Db: Built-in Derby database, small size, installation free.
  • Include: header files that Java and JVM interact with, such as our C++ projects written by JVMTI, which need to include this packagejvmti.h.For example, design non-intrusion monitoring based on JVMTI
  • Jre: A Java runtime environment that contains executable files required by the runtime, Java class libraries and dynamic link libraries that the runtime depends on.so .dll .dylib
  • Lib: Java class libraries, such as dt.jar and tools.jar

So which folder is the JVM in?

In case you hadn’t noticed, the JVM was here: C: Program Files\Java\jdk1.8.0_45\jre\bin\server

  • This part is the core of the entire Java implementation cross-platform. The.class files compiled by Java programs will be executed on the virtual machine.
  • In addition, the class library lib is called when the JVM interprets the class file. In the JRE directory there are two folders lib and bin, and lib is the class library that the JVM needs to execute.
  • The JVM. DLL does not work on its own. When the JVM. DLL is started, the explicit method is used to load the auxiliary dynamic linked library for execution.

3. What is the JDK?

In summary, the JDK is a superset of the JRE through the Java platform standards and the JDK directory structure. The JDK contains all the development, debugging, and monitoring tools of the JRE application. And the following important components:

  • Java – Run tools that run bytecodes for.class
  • Javac – compiler that compiles source code with the suffix. Java into bytecode with the suffix. Class
  • Javap – decompiler
  • Javadoc – Document generator that extracts documents from source code comments that conform to the specification
  • Jar – A packaging tool that packages related class files into a single file
  • JDB – Debugger
  • JPS – Displays the status of the process in which the Java program is currently running
  • Appletviewer – a tool to run and debug applets without using a browser
  • Javah – Generates C header files and C source files from Java classes. These files provide bonding so that Java and C code can interact.
  • Javaws – Run the JNLP program
  • Extcheck – a tool for detecting JAR package conflicts
  • Apt — Annotation processing tool
  • Jhat – Java heap analysis tool
  • Jstack – stack tracker
  • Jstat – JVM detection statistics tool
  • Jstatd – Jstat daemon
  • Jinfo – Gets configuration information about running or crashed Java programs
  • Jmap – Gets Java process memory mapping information
  • Idlj – IDL-to-Java compiler. Convert the IDL language to Java files
  • Policytool – a GUI policy file creation and management tool
  • Jrunscript – Command line scripts run
  • Appletviewer: Applets browser, a Java browser that executes Java applets on HTML files

4. What is JRE?

The JRE itself is also a program that runs on the CPU and explains the execution of Java code.

Typically, implementation work involves installing the JRE at the customer site, as this is the minimum required to run Java programs.

  • Bin: java.exe exists but javac.exe does not exist. That is, you can’t compile Java programs, but you can run Java programs. You can think of the bin directory as a JVM.
  • Lib: Java base & core libraries, containing libraries and rt.jar required by the JVM runtime. It also contains files for security management, such as security policies and security properties.

5. What is JVM?

The JVM is simply a virtual machine that runs Java bytecode. The JVM is a specification that can be implemented by any vendor. For example, Fuge himself has implemented a JVM virtual machine based on Java implementation in accordance with virtual machine specifications and handwritten books on the JVM.

Source code address: github.com/fuzhengwei/… Content introduction: This code mainly introduces how to use Java code to achieve the basic functions of the JVM (search and parse class files, bytecode commands, runtime data area, etc.), so that Java programmers through the most familiar Java programs, learning how the JVM is the Java program to run step by step.

Of course, the JVM we downloaded from Oracle is much more advanced than our own implementation. Their designs include continuously optimized memory models, GC collection strategies, adaptive optimizers, and more.

In addition, the JVM is called a virtual machine mainly because it is used to achieve “write-once-run-anywhere”. It provides an operating environment independent of the underlying operating system and machine hardware structure.

5.1 Client Mode and Server Mode

There are two different styles of startup mode in the JVM, Client mode and Server mode.

  • Client mode: The loading speed is fast. Can be used to run GUI interactive programs.
  • Server mode: Slow loading but fast running. Can be used to run the server daemon.

Modify the configuration mode file: C:\Program Files\Java\jre1.8.0_45\lib\amd64\jvm.cfg

# List of JVMs that can be used as an option to java, javac, etc.
# Order is important -- first in this list is the default JVM.
# NOTE that this both this file and its format are UNSUPPORTED and
# WILL GO AWAY in a future release.
#
# You may also select a JVM in an arbitrary location with the
# "-XXaltjvm=<jvm_dir>" option, but that too is unsupported
# and may not be available in a future release.
#
-server KNOWN
-client IGNORE
Copy the code
  • If necessary, you can set the client to KNOWN and place it in front of the Server.
  • The default JVM mode is Server mode, -xMS128M, -XMx1024M
  • The default JVM mode is -xMS1m and -XMx64m in Client mode

5.2 JVM architecture and actuators

This is the core knowledge of the JVM, but is not the focus of this article and will be covered in subsequent chapters. This chapter gives only a few introductions.

  • A Class Loader is a subsystem used to load Class files. Its main functions are loading, linking, and initialization.
  • JVM Memory Areas: method Areas, heap Areas, stack Areas, and program counters
  • Interpreter: The JVM Interpreter can translate each bytecode instruction into its corresponding local instruction by looking for a predefined JVM instruction-to-machine instruction map. It executes bytecode directly, without any optimizations.
  • JIT Compiler(just-in-time) : For efficiency, the JIT Compiler interacts with the JVM at run time and appropriately compiles bytecode sequences into native machine code. Typically, JIT Compiler executes one piece of code, not one statement at a time. Optimize this code and translate it into optimized machine code.JIT Compiler is enabled by default

Four,

  • This article is not complicated and involves less interview content. It is more of an introductory introduction to the following interview content related to JVM, paving the way for the following content.
  • If you haven’t looked at the structure of the JDK, JRE, and JVM and the corresponding component configuration and execution modes before, you can continue to learn from them. In addition, it is not easy to learn the JVM in depth. You need to learn both the JVM specification and the application practice, so it is recommended to write the JVM by hand and then validate it.
  • Okay, that’s it for this chapter. Even if I share these knowledge points with you, it is a process of learning, collecting, sorting and verifying by myself. Learn from each other and grow from each other. If there are mistakes, just leave a message to me and I will keep correcting them. Progress together!

Five, series recommendation

  • HashMap core knowledge, disturbance function, load factor, expansion linked list split
  • ArrayList has that much knowledge? A designated position to insert the plane will be dizzy!
  • In addition to JDK and CGLIB, there are three kinds of proxy methods. The interview is stuck again!
  • ReentrantLock fair lock explanation and implementation
  • Introduction and use of thread pool, and design of non-intrusion monitoring based on JVMTI