Java fundamentals — the JVM, JDK, JRE, bytecode, and how a Java program actually runs.
Question 1: What is Java, and what makes it different from many other programming languages?
Ans
Java is a high-level, class-based, object-oriented programming language whose source code compiles into an intermediate format called bytecode instead of directly into machine code. That bytecode runs inside a Java Virtual Machine (JVM), so the same compiled program can run on any operating system that has a matching JVM, without being recompiled for each platform. Java is used heavily for backend services, enterprise applications, Android development, and large-scale systems where portability and a mature ecosystem matter.
Example
java
public class Main {
public static void main(String[] args) {
System.out.println("Hello, Java");
}
}
Important Point
Java is not directly compiled to native machine code by javac — it produces bytecode first, and the JVM executes or further compiles that bytecode at runtime.
Question 2: Why is Java called "platform independent"?
Ans
Java source code is compiled once into bytecode (.class files), and that same bytecode can run unchanged on any machine that has a JVM built for its operating system. The JVM acts as a translation layer between the bytecode and the actual hardware/OS, so the application itself never needs to know what platform it is running on. This idea is commonly summarized as "write once, run anywhere."
Important Point
Platform independence applies to the bytecode, not to native libraries a program depends on — code that calls OS-specific libraries can still lose portability.
Question 3: What is the JVM, and what does it actually do at runtime?
Ans
The JVM (Java Virtual Machine) is the runtime engine that loads compiled .class files, verifies the bytecode is safe to run, manages memory through garbage collection, and executes the program — either by interpreting bytecode or by compiling hot code paths into native instructions via the JIT compiler. It also enforces security checks and loads classes only when they are actually needed.
Important Point
JVM is a specification with multiple implementations (HotSpot being the most common); it is a runtime engine, not the same thing as the JDK.
Question 4: What is the JDK, and why does a developer need it?
Ans
The JDK (Java Development Kit) bundles everything needed to develop and run Java applications — the compiler (javac), the launcher (java), debugging and packaging tools like jar and javadoc, and a full Java runtime. Installing the JDK is normally the first step for any Java developer, since it provides both the tools to build a program and the runtime to execute it.
Example
bash
javac Main.java
java Main
Important Point
In modern Java distributions there is no separately shipped JRE — the JDK itself contains everything needed to run a program.
Question 5: What is the JRE, and how is it different from the JDK?
Ans
The JRE (Java Runtime Environment) traditionally referred to just the pieces needed to run an already-compiled Java program — the JVM plus the standard class libraries — without any development tools like a compiler. The JDK is a superset: development tools plus a runtime. In current Java releases the distinction is mostly historical, since Oracle no longer ships the JRE as a separate download.
Important Point
It is fine to explain the historical JDK/JRE/JVM split in an interview, but mention that modern distributions no longer separate JRE from JDK.
Question 6: What is bytecode, and why does it matter for Java's portability?
Ans
Bytecode is the intermediate, platform-neutral instruction format that the Java compiler produces from .java source files, stored in .class files. It is not native machine code for any specific CPU — instead, the JVM interprets or JIT-compiles it into whatever machine code the current CPU understands. This one extra layer is what lets the same compiled artifact run on different operating systems.
Bytecode is not native machine code — confusing the two is a common interview mistake.
Question 7: What's the relationship between JDK, JRE, and JVM?
Ans
The JVM executes bytecode. The JRE (conceptually) is the JVM plus the standard runtime libraries needed to run a program. The JDK adds development tooling — the compiler, debugger, and build tools — on top of that runtime. So the JDK is the broadest of the three, and the JVM is the innermost execution engine that both the JRE and JDK ultimately rely on.
Important Point
Do not say the JVM "contains" the JDK — the containment goes the other way: JDK includes a runtime, which relies on the JVM.
Question 8: What are the main features that make Java popular for large applications?
Ans
Java combines object-oriented design, platform independence through bytecode and the JVM, automatic memory management via garbage collection, strong static typing with compile-time checks, structured exception handling, built-in multithreading support, and a very large standard library and ecosystem. Together these make it practical to build and maintain large, long-lived codebases with many contributors.
Important Point
Java isn't purely object-oriented — it still has primitive types like int, boolean, and char that aren't objects.
Question 9: What is the main method in Java?
Ans
The main method is the conventional entry point used by the Java launcher to start an application: public static void main(String[] args).
public makes it accessible to the launcher, static means an object is not required, void means it returns no value, and String[] args receives command-line arguments.
Example
java
public static void main(String[] args) {
System.out.println("Start");
}
Output: Start
Important Point
The parameter can also be written as `String... args`.
Question 10: Why is main() static?
Ans
The main method is static so the JVM can invoke it without first creating an object of the class.
At application startup there may be no application object available yet, so a class-level entry point is convenient.
Example
java
class App {
static void start() {
System.out.println("Started");
}
}
Important Point
The JVM's startup contract expects a suitable static main method for the traditional launcher model.
Question 11: What is package?
Ans
A package groups related Java types and creates a namespace that helps organize code and control package-private access.
Packages reduce naming conflicts and help structure applications by feature or layer.
Example
java
package com.example.service;
Important Point
Package-private members are accessible to types in the same package.
Question 12: What is pass-by-value in Java?
Ans
Java is always pass-by-value. For an object variable, the value being passed is a copy of the reference, so a method can change the referenced object's state but cannot replace the caller's reference itself.
This distinction explains many confusing object-parameter examples.
Example
java
static void change(List<Integer> list) {
list.add(10); // changes the same object
}