Analysis of Java interview classic questions - talk about your understanding of the Java platform?

image.png
From your exposure to Java development until now, what is your most intuitive impression of Java? Is it the "Compile once, run anywhere" that it advertises, or is it an overly formal syntax? How much do you know about the Java platform? Please stop and think for a moment.

The question I want to ask you today is, talk about your understanding of the Java platform? "Java is interpreted execution", is this sentence correct?

Typical answer

Java itself is an object-oriented language. The most notable features have two aspects. One is the so-called "compile once, run anywhere" (Compile once, run anywhere), which can easily obtain cross-platform capabilities; the other is garbage Collection (GC, Garbage Collection), Java reclaims and allocates memory through the garbage collector (Garbage Collector). In most cases, programmers do not need to worry about memory allocation and recycling.

We will come into contact with JRE (Java Runtime Environment) or JDK (Java Development Kit) on a daily basis. JRE, which is the Java Runtime Environment, contains JVM and Java class libraries, as well as some modules. The JDK can be seen as a superset of the JRE, providing more tools, such as compilers, various diagnostic tools, etc.

This statement is less accurate for the statement "Java is interpreted execution". The source code of Java we develop is first compiled into bytecode by Javac, and then, at runtime, the bytecode is converted into final machine code by the interpreter embedded in the Java Virtual Machine (JVM). However, common JVMs, such as the Hospot JVM provided by Oracle JDK that we use in most cases, provide a JIT (Just-In-Time) compiler, also known as a dynamic compiler. JIT can convert hot spots at runtime. The code is compiled into machine code. In this case, some hot codes are compiled and executed, not interpreted and executed.

Test point analysis

Actually, this question is a bit general. The topic itself is very open, and often examines multiple aspects, such as whether the basic knowledge is clear; whether the main modules and operating principles of the Java platform are mastered. Many interviewers will suffer from this kind of question. They are a little nervous and don't know where to start, so they give a very brief answer.

For such general questions, you need to try your best to show that your thinking is in-depth and systematic, and your Java knowledge is more comprehensive. Be sure to avoid making the interviewer think that you are a person who "knows what it is and doesn't know why". After all, understanding the basic composition and mechanism is the basis for many things such as problem diagnosis or performance tuning in daily work. I believe that no recruiter will dislike interviewers who "love learning and thinking".

Don't worry if your answer isn't perfect. I personally feel that it is normal to answer this kind of general question a little bit one-sidedly. Usually try to guide the interviewer and show his true level. This kind of question is to be an opening warm-up, and the interviewer will often expand related questions based on your answers.

knowledge expansion

Returning to the topic, we can briefly talk about the understanding of the Java platform from many aspects, such as: Java language features, including language features such as generics and Lambda; basic class libraries, including collections, IO/NIO, networking, concurrency, security and other basic class libraries. For the class libraries that are widely used in our daily work, we can systematically summarize them before the interview, which is helpful to play on the spot.

Or talk about some basic concepts and mechanisms of JVM, such as Java's class loading mechanism, the Class-Loader embedded in common versions of JDK (such as JDK 8), such as Bootstrap, Application and Extension Class-loader; the general process of class loading: loading, Verification, linking, initialization (here refers to Zhou Zhiming's "In-depth Understanding of Java Virtual Machine", a great JVM starter book); custom Class-Loader, etc. There are also the basic principles of garbage collection. The most common garbage collectors, such as SerialGC, Parallel GC, CMS, G1, etc., have a good idea of ​​what kind of workload they are suitable for. These are all areas that can be expanded, and I will introduce them more systematically in a later column.

Of course, what tools are included in the JDK or other tools in the Java field, such as compilers, runtime environments, security tools, diagnostic and monitoring tools, etc. These basic tools are the guarantee of daily work efficiency. They are also helpful for us to work on other language platforms. Many of them are analogous.

The picture below is a relatively broad blueprint I have summarized for your reference.

image.png

Without further expansion, back to the previously asked questions about interpreting execution and compiling execution. Some interviewers like to "go to the bottom of things" on specific questions because it's an effective way to learn more about an interviewee's knowledge, and I'll go a little deeper.

As we all know, we usually divide Java into compile time and runtime. The Java compilation and C/C++ mentioned here have different meanings. The Javac compilation, compiling the Java source code to generate the ".class" file, is actually bytecode, not directly executable machine code. Java shields the details of the operating system and hardware through the cross-platform abstraction of bytecode and the Java Virtual Machine (JVM), which is also the basis for realizing "compile once, execute everywhere". Here I recommend an architecture learning exchange circle to everyone. Communication study guide pseudo-xin: 1253431195 (there are a lot of interview questions and answers), which will share some video recordings recorded by senior architects: there are Spring, MyBatis, Netty source code analysis, high concurrency, high performance, distributed, micro-service architecture The principle of JVM performance optimization, distributed architecture, etc. have become the necessary knowledge system for architects. You can also receive free learning resources, which are currently benefiting a lot

At runtime, the JVM loads the bytecode through the Class-Loader, interprets or compiles it for execution. As I mentioned earlier, mainstream Java versions, such as JDK 8, are actually a mode of mixed interpretation and compilation, the so-called mixed mode (-Xmixed). Usually, the JVM running in server mode will make tens of thousands of calls to collect enough information for efficient compilation. The threshold of client mode is 1500 times. Oracle Hotspot JVM has built-in two different JIT compilers. C1 corresponds to the client mode mentioned above, which is suitable for applications that are sensitive to startup speed, such as ordinary Java desktop applications; C2 corresponds to server mode, which is optimized for long-running servers. end application design. The default is to use the so-called tiered compilation (TieredCompilation). I won't go into more details of JIT here, there is no need to dive into it all at once, I will introduce the content of layered compilation later.

When the Java virtual machine starts, you can specify different parameters to select the running mode. For example, specifying "-Xint" tells the JVM to only interpret and execute the code without compiling the code. This mode abandons the performance advantages that JIT may bring. After all, the interpreter reads in item by item and interprets and executes item by item. Correspondingly, there is also a "-Xcomp" parameter, which tells the JVM to close the interpreter and not perform interpretation execution, or the maximum optimization level. Then you may ask whether this mode is the most efficient? Simply put, not really. "-Xcomp" will cause the JVM to start very slowly. At the same time, some JIT compiler optimization methods, such as branch prediction, often cannot be effectively optimized if profiling is not performed.

In addition to the most common Java usage patterns in our daily life, there is actually a new compilation method, the so-called AOT (Ahead-of-Time Compilation), which directly compiles bytecode into machine code, thus avoiding JIT warm-up, etc. Various overheads, such as the introduction of experimental AOT features in Oracle JDK 9, and the addition of new jaotc tools. Use the following command to compile a class or a module into an AOT library.

jaotc --output libHelloWorld.so HelloWorld.class
jaotc --output libjava.base.so --module java.base

Then, you can specify it directly at startup.

java -XX:AOTLibrary=./libHelloWorld.so,./libjava.base.so HelloWorld

Moreover, Oracle JDK supports layered compilation and AOT collaborative use, which is not a choice between the two. AOT is not the only way, the industry has long had third-party tools (such as GCJ, Excelsior JET) to provide related functions.

In addition, as a powerful platform, the JVM is not only the Java language that can run on the JVM, but also essentially compliant bytecodes. The Java language itself also provides convenience for this. We can see similarities like Clojure and Scala. , Groovy, JRuby, Jython and many other JVM languages ​​are active in different scenarios.

Today, I briefly introduced some content related to the Java platform, with the purpose of building a general impression, including Java language features, core class libraries and commonly used third-party class libraries, basic principles of Java virtual machine and related tools. you are helpful.

Guess you like

Origin blog.csdn.net/m0_54828003/article/details/127240601