Question:
Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct?
Answer:
Java is an object-oriented language with two prominent features. First, it offers the ability to "write once, run anywhere," making it easy to achieve cross-platform capabilities. Second, Java incorporates garbage collection (GC) where a garbage collector automatically reclaims allocated memory, relieving programmers from the burden of manual memory management in most cases.
In our daily work, we come across JRE (Java Runtime Environment) or JDK (Java Development Kit). JRE includes the JVM (Java Virtual Machine), Java class libraries, and various modules. JDK can be considered as a superset of JRE, providing additional tools such as compilers and diagnostic utilities.
Regarding the statement "Java is interpreted," it is not entirely accurate. The Java source code we develop is first compiled into bytecode using Javac. During runtime, the bytecode is then interpreted by the interpreter embedded within the Java Virtual Machine (JVM) to produce the final machine code. However, popular JVMs like Hotspot JVM provided by Oracle JDK include a Just-In-Time (JIT) compiler. The JIT compiler dynamically compiles hot code into machine code at runtime. In this case, the execution becomes partially compiled rather than interpreted.
Analysis:
In fact, this question is quite general. The question itself is very open-ended and often tests multiple aspects, such as whether the understanding of basic knowledge is clear and whether the interviewee has a grasp of the main modules and operating principles of the Java platform, etc. Many interviewees may struggle with this kind of question and, feeling a bit nervous, may not know where to start and give a very brief answer.
For such general questions, you should try to demonstrate your deep and systematic thinking, as well as a comprehensive understanding of Java knowledge. You must avoid giving the impression to the interviewer that you know the facts but not the underlying reasons. After all, understanding the basic components and mechanisms is the foundation for many tasks in daily work, such as problem diagnosis and performance tuning. I believe no hiring party would dislike an interviewee who "loves learning and thinking."
Even if you feel that your answer is not very comprehensive, there is no need to worry. Personally, I think it is normal to give somewhat one-sided answers to these kinds of general questions. Most experienced interviewers will not easily draw conclusions about an interviewee based on one question alone. They usually try to guide the interviewee to showcase their true level. This type of question is just a warm-up, and interviewers often expand on related questions based on your answer.
Expansion:
When it comes to understanding the Java platform, we can briefly discuss it from various aspects. For example, Java language features, including generics, lambdas, and other language features. We can also talk about the core class libraries, such as collections, IO/NIO, networking, concurrency, security, and other foundational libraries. It would be helpful to systematically summarize the commonly used libraries in our daily work before the interview, as it aids in on-the-spot performance.
Alternatively, we can discuss some basic concepts and mechanisms of the JVM, such as Java's class loading mechanism. We can delve into commonly used class loaders embedded in JDK versions (e.g., JDK 8), such as Bootstrap, Application, and Extension ClassLoaders. We can explore the general process of class loading, including loading, verification, linking, and initialization. Additionally, topics like custom class loaders can be discussed. Another area to explore is the basic principles of garbage collection and the most common garbage collectors, such as SerialGC, Parallel GC, CMS, G1, and understanding which workloads they are best suited for. These are expandable areas that I will cover in more detail in future columns.
Furthermore, we can touch upon the tools included in the JDK or other tools within the Java domain. These may include compilers, runtime environments, security tools, diagnostic and monitoring tools, and more. These essential tools ensure our daily work efficiency and can also be helpful when working on other language platforms, as many concepts can be applied analogously.
Some interviewers like to "dig deep" into specific questions because it is an effective way to further understand the extent of the candidate's knowledge. Let's delve a little deeper into this topic.
As we know, Java is typically divided into the compilation phase and the runtime phase. When we talk about Java compilation, it differs from C/C++. In the case of Java, the compilation by Javac produces bytecode in the ".class" file, which is not directly executable machine code. Java achieves its "write once, run anywhere" capability by using bytecode and the Java Virtual Machine (JVM) as a platform abstraction layer, shielding the details of the operating system and hardware.
During runtime, the JVM loads the bytecode using class loaders and interprets or compiles it for execution. As mentioned earlier, in mainstream Java versions such as JDK 8, there is a hybrid mode called "mixed mode" (-Xmixed), which combines interpretation and compilation. JVMs running in server mode, for example, perform thousands of invocations before gathering enough information for efficient compilation, while in client mode, the threshold is 1500 invocations. Oracle Hotspot JVM includes two different JIT compilers: C1, which corresponds to the client mode and is suitable for applications sensitive to startup speed, such as regular Java desktop applications, and C2, which corresponds to the server mode and is optimized for long-running server applications. The default is to use a technique called "Tiered Compilation" that employs both compilers. I won't delve into more JIT details at this point; it's not necessary to dive right in. I will cover the topic of Tiered Compilation in more detail later on.
Moreover, Oracle JDK supports the collaboration of tiered compilation and AOT. They are not mutually exclusive options. AOT is not limited to just one approach; there have been third-party tools in the industry (such as GCJ and Excelsior JET) that provide similar functionality.
Furthermore, as a powerful platform, the JVM is not limited to running only Java programs. Essentially, any bytecode that is compliant can run on the JVM. Java itself provides convenience for this, and we can see a wide range of JVM languages such as Clojure, Scala, Groovy, JRuby, Jython, and more, actively used in different scenarios.