JDK FAQ
Basic Questions
What is JDK
JDK (Java Development Kit) is the software development kit for the Java programming language. It is the software package required for developing Java applications. JDK includes the Java compiler (javac), Java Virtual Machine (JVM), Java core class libraries, and various development tools. Without JDK, you cannot develop or run Java programs.
The difference between JDK and JRE is: JRE (Java Runtime Environment) only contains the environment needed to run Java programs, not development tools. If you only need to run Java programs, install JRE; if you need to develop Java programs, you must install JDK.
What is JVM
JVM (Java Virtual Machine) is the core of Java program execution. JVM is responsible for interpreting or compiling Java bytecode (.class files) for native code execution. The existence of JVM enables Java programs to achieve "Write Once, Run Anywhere" cross-platform capability.
JVM not only runs Java programs but also handles memory management (garbage collection), thread management, security management, etc. Understanding how JVM works is essential for writing high-performance Java applications.
What is the Difference Between JDK and JRE
JDK (Java Development Kit) is the complete development kit, containing JRE and development tools. JRE (Java Runtime Environment) is the Java runtime environment, containing only components needed to run Java programs.
Simply put: if you just want to run Java programs developed by others, installing JRE is enough; if you want to develop Java programs yourself, you must install JDK. JDK contains complete JRE, so you don't need to install JRE separately after installing JDK.
Which JDK Version is Best
There is no absolute "best" version; choice depends on specific needs. For new projects, JDK 21 (the latest LTS version) or JDK 25 (the newest version) is recommended. For enterprise projects requiring long-term maintenance, LTS versions like JDK 21 or JDK 17 are recommended.
JDK 21 is the latest Long-Term Support version, receiving at least 8 years of support. JDK 17 is still widely used and free to use. JDK 8, although old, is still used in many legacy systems.
Installation Questions
How to Check if JDK is Installed Successfully
Run java -version in the command line. If version information is displayed, JDK is installed correctly. Run javac -version to check if the compiler is available. Run echo %JAVA_HOME% (Windows) or echo $JAVA_HOME (Linux/macOS) to check if environment variables are set correctly.
If these commands show errors in the command line, first confirm whether JDK is installed correctly, then check whether environment variables are configured correctly.
What is JAVA_HOME
JAVA_HOME is an environment variable pointing to the JDK installation directory. Many Java tools and build scripts (like Maven, Gradle) rely on JAVA_HOME to locate JDK. Correctly setting JAVA_HOME ensures these tools can work properly.
On Windows, JAVA_HOME is typically set to paths like C:\Program Files\Java\jdk-21. On Linux/macOS, it's typically set to paths like /usr/lib/jvm/java-21-openjdk.
How to Install Multiple JDK Versions on Windows
Multiple JDK versions can be installed on the same machine. After installing multiple versions, switch the default version by modifying the order of JDK bin directories in the PATH environment variable, or by modifying the JAVA_HOME environment variable.
You can also use third-party tools to manage multiple JDK versions, like SDKMAN! for Windows or JetBrains Toolbox. These tools make switching between versions more convenient.
What is the Difference Between Oracle JDK and OpenJDK
Oracle JDK and OpenJDK are almost completely identical at the code level, with main differences being license agreements and technical support. Oracle JDK provides commercial support and additional enterprise-level features, but some versions may require payment for commercial use. OpenJDK is open-source and free, maintained by the community.
For most use cases, OpenJDK is perfectly adequate. Many organizations choose OpenJDK to avoid license issues while getting almost identical performance and functionality compared to Oracle JDK.
Usage Questions
What is Classpath
Classpath is the path used by JVM to find class files. When you run java MyClass, JVM will search for MyClass.class in directories and JAR files specified by classpath.
When compiling and running Java programs, classpath can be specified using -classpath or -cp options. If not specified, JVM defaults to using the current directory as classpath.
What is Bytecode
Bytecode is a form of intermediate code compiled from source code by the Java compiler. Bytecode is not native code and cannot run directly on CPU; it can only be interpreted by JVM or JIT-compiled for execution.
The existence of bytecode gives Java programs cross-platform capability. The same bytecode can run on any system with JVM installed, whether Windows, Linux, or macOS.
What is a JAR File
JAR (Java Archive) is a format for packaging multiple Java class files into a single file. JAR files are actually ZIP-formatted compressed packages containing compiled .class files and some metadata.
The jar tool can be used to create, view, and extract JAR files. Executable JAR files contain a MANIFEST.MF file specifying the main class and can be run directly with java -jar.
What is the Module System
The Java module system (Project Jigsaw) was introduced in JDK 9, dividing JDK and applications into modules. Modules provide better encapsulation and clearly declare dependencies, helping build more robust and maintainable applications.
Modules are declared through module-info.java files, defining module name, dependencies on other modules, exported packages, etc. The module system helps resolve classpath conflicts and provides better version management.
Performance Questions
How to Optimize JVM Performance
JVM performance optimization mainly involves: reasonably setting heap memory size, avoiding settings that are too large or too small; choosing an appropriate garbage collector, G1 is a good choice for most situations; for applications pursuing low latency, consider using ZGC or Shenandoah; enabling class data sharing (-Xshare:classes) to speed up startup.
Use performance monitoring tools (like VisualVM, JConsole) to analyze application performance, find bottlenecks, and perform targeted optimization.
What is Garbage Collection
Garbage Collection (GC) is the mechanism by which JVM automatically manages memory. During program execution, objects are created and consume memory. When objects are no longer referenced, the garbage collector automatically identifies and reclaims the memory occupied by these objects.
Java's garbage collection greatly simplifies memory management work; programmers don't need to explicitly free memory. However, understanding garbage collection principles helps write more efficient code and avoid memory leaks and frequent GC.
What is Memory Leak
Memory leak refers to memory that cannot be reclaimed during program execution, causing available memory to gradually decrease. In Java, memory leaks are usually caused by collections (like static Maps, Lists) that continuously add objects without timely cleanup.
To investigate memory leaks, you can use jmap to generate heap dump files, then use tools like MAT (Memory Analyzer Tool) to analyze and find leaking objects.
What is JIT Compiler
JIT (Just-In-Time) compiler is part of JVM, responsible for compiling hot bytecode into native code to improve execution performance. During runtime, JVM analyzes code execution; for frequently executed code (hot spots), the JIT compiler compiles them into native machine code, significantly improving performance.
JIT compilation happens at runtime without additional compilation steps. C1 compiler compiles fast but with less optimization, C2 compiler compiles slowly but with more optimization. Tiered compilation combines the advantages of both.
Version Questions
Which Java Version Should I Use
For new projects, the latest LTS version JDK 21 is recommended. It provides good performance, rich features, and long-term support. For projects needing Java 8/11 compatibility, those versions can be used.
Using versions that have stopped being supported, like JDK 8 (unless with paid subscription), is not recommended. Upgrading to new JDK versions timely provides security updates and performance improvements.
What Does LTS Mean in Java Version Numbers
LTS (Long-Term Support) is the abbreviation for Long-Term Support versions. LTS versions receive at least 8 years of technical support and patch updates from Oracle. Current LTS versions include JDK 8, JDK 11, JDK 17, and JDK 21.
Non-LTS versions (like JDK 25) have shorter support cycles, usually only 6 months. These versions are suitable for developers who want the latest features, but LTS versions are recommended for production environments.
Can I Still Use Java 17
Java 17 is a mature LTS version and can still be used normally. Oracle changed the JDK 17 license in October 2024. However, according to Oracle's statement, JDK 17 can still be used free of charge for development, personal use, and running Oracle-licensed products.
Many enterprises are still using JDK 17 because it's a very mature and stable version with abundant community support resources. If a project is already using JDK 17, it can continue to be used; if starting a new project, JDK 21 is more recommended.
Development Questions
How to Compile Java Files
Use the javac command to compile Java files. Basic usage: javac MyClass.java. After compilation, a MyClass.class bytecode file is generated.
For projects, build tools like Maven or Gradle are typically used to manage the compilation process. These tools automatically handle dependencies and compilation options.
How to Run Java Programs
Use the java command to run compiled Java programs. Basic usage: java MyClass. Note that the .class extension is not needed.
If the program is packaged in a JAR file, use java -jar MyApp.jar to run.
How to Set Classpath
Specify classpath at runtime using -classpath or -cp: java -cp lib/*:out MyClass. On Windows, the separator is semicolon (;); on Linux/macOS, it's colon (:).
You can also set CLASSPATH through the environment variable, but command-line specification is more flexible.
What are Maven and Gradle
Maven and Gradle are build tools for Java projects. They can automatically download dependencies, compile code, run tests, and package for release, greatly simplifying the project build process.
Maven uses XML (pom.xml) to define project configuration, while Gradle uses Groovy or Kotlin scripts (build.gradle) to define build logic. Both are widely used build tools in Java projects.
