Java Core Technology Interview Essentials (Lecture 1) | Tell me about your understanding of the Java platform?

From your exposure to Java development to the present, what is your most intuitive impression of Java? Is it the "Write once, run anywhere" it promotes, or is it a grammar that is too formalistic? What exactly do you know about the Java platform? Please stop and think about it first.

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


Typical answer

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

We are exposed to JRE (Java Runtime Environment) or JDK (Java Development Kit) every day. JRE, which is the Java runtime environment, contains JVM and Java class libraries, as well as some modules. The JDK can be regarded as a superset of JRE, providing more tools, such as compilers, various diagnostic tools, and so on.

For the phrase "Java is interpretation and execution", this statement is not very accurate. The Java source code we developed is first compiled into bytecode by Javac, and then, at runtime, the bytecode is converted into the final machine code by the interpreter embedded in the Java Virtual Machine (JVM). But common JVMs, such as the Hotspot JVM provided by Oracle JDK that we use in most cases, provide a JIT (Just-In-Time) compiler, which is commonly referred to as a dynamic compiler. JIT can convert hotspots at runtime. The code is compiled into machine code. In this case, part of the hot code is compiled and executed, rather than interpreted and executed.

Knowledge expansion

Back to the topic, you can briefly talk about the understanding of the Java platform from many aspects, such as: Java language features, including generics, Lambda and other language features; basic class libraries, including collections, IO/NIO, network, concurrency, and security And other basic libraries. For the class libraries that we use more in our daily work, we can systematically summarize before the interview, which will help us to play on the spot.

Or talk about some basic concepts and mechanisms of JVM, such as Java's class loading mechanism, and the Class-Loader embedded in commonly used 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 (refer to Zhou Zhiming's "In-depth Understanding of the Java Virtual Machine", a great JVM getting started 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 workloads are best. These are all areas that can be expanded, and I will give a more systematic introduction to them in a later column.

Of course, there are also tools 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 summarized for your reference.

No more expansion, back to the interpretation, execution and compilation and execution questions asked earlier. Some interviewers like to "see the bottom line" on specific questions, because this is an effective way to learn more about the interviewer's knowledge. I will discuss it a little bit more deeply.

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 compilation of Javac, compiling the Java source code to generate the ".class" file actually contains bytecode, not machine code that can be directly executed. Through the cross-platform abstraction of bytecode and Java Virtual Machine (JVM), Java shields the details of the operating system and hardware, which is also the basis for achieving "compile once, execute everywhere".

At runtime, the JVM will load the bytecode through the Class-Loader, interpret or compile and execute it. As I mentioned earlier, in mainstream Java versions, such as JDK 8 is actually a mixed mode of interpretation and compilation, the so-called mixed mode (-Xmixed). Generally, a JVM running in server mode will make tens of thousands of calls to collect enough information for efficient compilation. The threshold for client mode is 1500. Oracle Hotspot JVM has built-in two different JIT compilers. C1 corresponds to the client mode mentioned earlier, which is suitable for applications that are sensitive to startup speed, such as ordinary Java desktop applications; C2 corresponds to the server mode, which is optimized for long-running servers Designed for end applications. The default is to use the so-called Tiered Compilation (TieredCompilation). I won't expand on more details of JIT here, there is no need to get in 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 operating mode. For example, specifying "-Xint" tells the JVM to only perform interpretation and execution without compiling the code. This mode abandons the performance advantages that JIT may bring. After all, the interpreter reads in one by one and interprets and runs one by one. Corresponding to it, there is also a "-Xcomp" parameter, which tells the JVM to close the interpreter and not to perform interpretation, or it is called the maximum optimization level. Then you may ask whether this model is the most efficient? Simply put, it's not necessarily true. "-Xcomp" will cause the JVM to start much slower. At the same time, some JIT compiler optimization methods, such as branch prediction, often cannot be effectively optimized without profiling.

In addition to our most common daily use of Java, 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 aspects of overhead, such as Oracle JDK 9 introduced experimental AOT features, and added a new jaotc tool. Use the following command to compile a certain class or a certain module into an AOT library.


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

 Then, just specify it directly at startup.


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

Moreover, Oracle JDK supports hierarchical compilation and AOT collaborative use, the two are not an alternative relationship. If you are interested, you can refer to related documents: http://openjdk.java.net/jeps/295. AOT is not only this 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 essentially all compliant bytecodes can be run. The Java language itself also provides convenience for this. We can see things like Clojure and Scala. A large number of JVM languages ​​such as, Groovy, JRuby, Jython, etc. are active in different scenarios.

Today, I briefly introduced some content related to the Java platform. The purpose is to outline and build an overall impression, including Java language features, core class libraries and commonly used third-party class libraries, basic principles of the Java virtual machine, and related tools. You are helpful.


Other classic answers

The following answer comes from the netizen Woj:

"Compile once, run everywhere" refers to the cross-platform features of the Java language. The cross-platform features of Java are inseparable from the existence of the Java virtual machine, which can run in different environments. For example, Windows platform and Linux platform have corresponding JDK, after installing JDK, there will be a running environment of Java language. In fact, the Java language itself is not very different from other programming languages. It is not that the Java language can be cross-platform, but that there are environments where the Java language can run on different platforms. That’s why Java has a one-time compilation, everywhere. Run such an effect.

Strictly speaking, there is more than just Java as a cross-platform language, but Java is a more mature one. The effect of "compile once, run everywhere" is related to the compiler. The processing of the programming language requires a compiler and an interpreter. The Java virtual machine is similar to DOS, and is equivalent to a platform for programs to run.

The program has three stages from source code to running: coding-compiling-running-debugging. Java embodies the characteristics of cross-platform in the compilation stage. The compilation process is roughly like this: The first is to convert the Java source code into the bytecode of the .CLASS file. This is the first compilation. A .class file is a file that can be run everywhere. Then the Java bytecode will be converted into the target machine code, which is executed by the JVM, which is the second compilation of Java.

 The key and premise of "running everywhere" is the JVM. Because the JVM plays a key role in the second compilation. A JVM operating system is included wherever the Java virtual machine can be run. As a result, JAVA provides various virtual mechanisms on different platforms, thus achieving the effect of "running everywhere". It should be emphasized that Java is not a compilation mechanism, but an interpretation mechanism. The design of Java bytecode fully considers JIT, a just-in-time compilation method, which can directly transform bytecode into high-performance native machine code, which is also a component of the virtual machine.

The following answers from the three armies of netizens:

Java features:

Object-oriented (encapsulation, inheritance, polymorphism), platform independence (JVM running .class file), language (generic, Lambda), class library (collection, concurrency, network, IO/NIO), JRE (Java runtime environment, JVM, class library), JDK (Java development tools, including JRE, javac, diagnostic tools)

Does Java parse and run?

Incorrect!

1. Java source code is compiled into .class file by Javac, 2. .class file is parsed or compiled and run by JVM (1) parsed: .class file is parsed and executed by the JVM built-in parser. (2) Compilation: There is a JIT compiler (Just In Time Compile) that uses frequently running code as "hot code" to compile machine code related to the local platform and perform various levels of optimization. (3) AOT compiler: Java 9 directly compiles all codes into machine code for execution.

The following is the answer from the netizen Jerry Yinyin:

After paying attention for a long time, I finally looked forward to the first lecture. When I saw this topic, I didn't immediately click in to read the original text, but gave myself some time to think.

First of all, I personally feel that this question is very abstract and general. There is no standard answer to this question, but there is a "good" answer, and the answer depends entirely on the interviewer's own technical literacy and systematic understanding of Java. My understanding is as follows:

Macroscopic view: The biggest difference from c/c++ is that c/c++ programming is operating system-oriented and requires developers to pay great attention to the differences between different operating systems; while the Java platform shields the bottom layer of the operating system through a virtual machine Details, so that developers do not need to care too much about the differences between different operating systems. "Decoupling" by adding an indirect middle layer is a very common "artistic technique" in the computer field. This is true for virtual machines, operating systems, and HTTP. The Java platform has formed an ecosystem in which there are many research fields and application fields:

1. Research on virtual machine and compilation technology (for example: GC optimization, JIT, AOT, etc.): The pursuit of efficiency is another human nature

2. Optimization of Java language itself

3. Big data processing

4. Java Concurrent Programming

5. Client development (for example: Android platform)

6. ......

Microscopic view: There are two cores in the Java platform: 1. The Java language itself, the core class libraries and related tools provided in the JDK 2. The Java virtual machine and other included GCs

1. The Java language itself, the core class libraries and related tools provided in the JDK

To be engaged in the development of the Java platform, it is necessary to master the Java language, core class libraries and related tools. I think this is the foundation of the foundation.

>> To understand the language itself, developers need to be very familiar with the grammatical structure of the language; Java is an object-oriented language, which in turn requires developers to have a deep understanding of object-oriented design concepts;

>> The Java core class library includes collection classes, thread-related classes, IO, NIO, and JUC concurrent packages, etc.;

>> The tools provided by JDK include: basic compilation tools, virtual machine performance testing related tools, etc.

2. Java Virtual Machine

The Java language has the characteristics of cross-platform, it is precisely because of the existence of the virtual machine. Java source files are compiled into bytecode, loaded by the virtual machine and executed. There are two levels of implicit meaning here:

1) In most cases, programmers only need to care about the Java language itself, without having to care about the underlying details. Including the allocation and recovery of memory, it is also fully handed over to the GC.

2) For the virtual machine, as long as it is bytecode that meets the specification, they can be loaded and executed. Of course, it is not enough for a program that can run normally to meet this point. The program itself needs to ensure that there is no abnormality during operation. Therefore, languages ​​such as Scala, Kotlin, and Jython can also be run on the virtual machine.

Focusing on the efficiency of virtual machines, some optimization techniques will be involved, such as JIT and AOT. Because if the virtual machine loads the bytecode, it is completely interpreted and executed, which will inevitably affect the execution efficiency. Therefore, for this operation link, the virtual machine performs some optimization processing, such as JIT technology, which compiles certain frequently-running codes into machine code. The AOT technology directly converts bytecode into machine code through tools before running.

The following answer comes from netizen Ouyang Tian:

1. JVM memory model, heap, stack, method area; cross-platform bytecode; strong references, weak references, soft references, and phantom references in the JVM. Can the finalise method be used to save it? ; Parents are delegated for class loading, what is a parent? Parents are multiple parents. I load a document, and then you load it. Is this document the same in the JVM? ; Polymorphism is the core concept that Java needs, and it is also the best interpretation of object-oriented behavior; understand the execution flow of method overloading and rewriting in memory, and how to locate this specific method.

2. Development process, JDK5 (rewrite bug), JDK6 (the most stable commercial version), JDK7 (switch string support), JDK8 (functional programming), has been evolving.

3. Understand the ancestor class Object and how its behavior is connected with real life.

4. Understand 23 design patterns, because it is a combination of Tao and Shu.

The following answer comes from the netizen Stone Lion:

1. Compile once, run everywhere. The jvm layer encapsulates the system API and provides consistent calling behavior for different systems. It reduces the workload for adapting to different operating systems and different architectures.

2. Garbage collection reduces the difficulty of memory collection during development. Reduce the probability of memory leaks. Although it also brings some additional costs, it is enough to make up for the benefits. Reasonable generation strategy improves memory usage.

3. Compared with other compiled languages, jit reduces compilation time, because most of the code is compiled at runtime, which avoids the problem of cold code participating in compilation at compile time.

 Improved the efficiency of code execution. Lua was used for related development in previous projects. Because lua is an interpreted language, and it is used in conjunction with lua-jit. During the development process, if the lua code written is not supported by jit, the code performance will be very low compared with the compilable code.

 

 

Guess you like

Origin blog.csdn.net/qq_39331713/article/details/114009721
Recommended