Java Quick Revision Notes - Part 1 to 6
■ 1. What is Java?
■ Concept
- Java is a high-level, class-based, object-oriented programming language.
- Developed by James Gosling at Sun Microsystems in 1995.
- Known for platform independence → "Write Once, Run Anywhere (WORA)".
- Runs using JVM (Java Virtual Machine) which executes bytecode.
■ Important Rules / Exceptions
1. Java is case-sensitive (main ≠ Main).
2. The file name must match the public class name.
3. Only one public class allowed per .java file.
4. main method signature must be:
public static void main(String[] args)
- args can have any name, but String[] type is mandatory.
5. Without main, Java program won’t run (except JShell or static block before Java 7).
■ Interview Questions
- Why is Java called platform independent?
- Difference between JDK, JRE, JVM?
- Can we run a Java program without main() method?
- Why is main() method public static void?
- Is Java 100% Object-Oriented?
■■ Example
class HelloJava {
public static void main(String[] args) {
System.out.println("Hello Java!");
}
}
■ Tips to Remember
- Java = OOP + Platform Independent + Secure + Robust.
- Keywords like goto & const are reserved in Java but not used.
- Always think of Java as a language + platform (JVM).
■ 2. History of Java
■ Concept
- 1991 → Project started by James Gosling & team at Sun Microsystems (called Oak).
- 1995 → Officially released as Java (since Oak was trademarked).
- Designed for embedded systems, interactive TV, later adapted for internet applications.
- 1996 → First JDK (Java Development Kit 1.0).
- 2009 → Oracle acquired Sun Microsystems → Java under Oracle.
- Today → widely used in Enterprise, Android, Microservices, Big Data, Cloud.
■ Important Rules / Exceptions
- Java versioning follows major releases (Java SE 8, 11, 17, 21 are LTS).
- Backward compatibility → Old Java code usually works in newer versions.
- Applets & Java Web Start deprecated & removed in Java 11+.
■ Interview Questions
- Who is the father of Java?
- Why was Java initially named Oak?
- What was Java’s original purpose?
- Which company owns Java now?
- Which Java versions are LTS?
■■ Example (Timeline)
1995 → Java 1.0
2004 → Java 5 (Generics, Annotations)
2011 → Java 7 (try-with-resources, switch on String)
2014 → Java 8 (Lambdas, Streams, Default Methods)
2017 → Java 9 (Modules)
2018 → Java 11 (LTS, Removed Applets, JavaFX out)
2021 → Java 17 (LTS, Sealed classes, Pattern Matching)
2023 → Java 21 (LTS, Virtual Threads)
■ Tips to Remember
- Java’s slogan: "Write Once, Run Anywhere."
- Always know Java 8, 11, 17, 21 → most used in industry.
- Interviewers often ask what changed in latest LTS version.
■ 3. Features of Java
■ Concept
Java provides a rich set of features that made it popular and widely used.
Main Features:
1. Simple – Syntax similar to C/C++, no pointers, no operator overloading.
2. Object-Oriented – Everything is in the form of classes & objects.
3. Platform-Independent – Code compiles to bytecode, runs on any JVM.
4. Secure – No direct memory access (like pointers), runs in JVM sandbox.
5. Robust – Strong memory management + exception handling.
6. Multithreaded – Supports multiple threads of execution.
7. High Performance – Uses JIT (Just-In-Time) compiler.
8. Distributed – Supports networking & RMI.
9. Portable – Same bytecode runs on different machines.
10. Dynamic – Supports runtime polymorphism, reflection, and dynamic class loading.
■ Important Rules / Exceptions
- Java does not support multiple inheritance with classes, but supports it with interfaces.
- final keyword prevents inheritance, overriding, or reassignment.
- Garbage Collection is automatic, but System.gc() is only a request, not a guarantee.
- Pointers & operator overloading are intentionally removed for simplicity & security.
■ Interview Questions
- Why is Java platform-independent?
- Difference between robustness and security in Java?
- How does Java achieve multithreading?
- Why doesn’t Java support multiple inheritance with classes?
- How is Java different from C++?
■■ Example
class FeatureDemo {
public static void main(String[] args) {
// Object-Oriented
Person p = new Person("Sahil");
p.display();
// Robust (exception handling)
try {
int x = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Exception handled: " + e);
}
// Multithreaded
Thread t = new Thread(() -> System.out.println("Thread running..."));
t.start();
}
}
class Person {
String name;
Person(String name) { this.name = name; }
void display() { System.out.println("Hello " + name); }
}
■ Tips to Remember
- Always mention WORA (Write Once, Run Anywhere).
- OOPS principles (Encapsulation, Inheritance, Polymorphism, Abstraction) are hot interview topics.
- Java is not 100% OOP (because of primitives like int, char).
- Security & Robustness are always tested in interviews with tricky exception handling questions.
■ 4. JDK, JRE, JVM
■ Concept
1. JVM (Java Virtual Machine)
- Executes Java bytecode line by line.
- Provides platform independence.
- Handles memory management & garbage collection.
2. JRE (Java Runtime Environment)
- Contains JVM + Libraries + Runtime Classes.
- Used to run Java programs.
- Does not have development tools (compiler, debugger).
3. JDK (Java Development Kit)
- Contains JRE + Development Tools (javac, javadoc, debugger).
- Needed for both development & execution.
- Multiple versions available (Standard Edition, Enterprise Edition, etc.).
■ Important Rules / Exceptions
- JDK = JRE + Development Tools.
- JRE = JVM + Libraries.
- JVM is platform-dependent (different implementation per OS), but bytecode is
platform-independent.
- Java 11 onwards: JRE is no longer provided separately, only JDK includes everything.
- HotSpot JVM is the most widely used implementation.
■ Interview Questions
- Difference between JDK, JRE, and JVM?
- Is JVM platform-independent?
- Why is Java platform-independent but JVM is not?
- What is the role of JIT compiler in JVM?
- Can we run Java without JDK installed?
■■ Example (Flow of Compilation & Execution)
Source Code (.java)
↓ [javac compiler]
Bytecode (.class)
↓ [JVM → Class Loader → JIT Compiler → Execution Engine]
Machine Code (OS specific)
class Demo {
public static void main(String[] args) {
System.out.println("Hello from JVM!");
}
}
Compile: javac Demo.java
Run: java Demo
■ Tips to Remember
- Compilation vs Execution: javac compiles → JVM executes.
- Java 11+: only JDK is shipped, no separate JRE.
- JIT Compiler improves performance by compiling bytecode to native machine code at runtime.
- Formula:
- JDK = JRE + Development Tools
- JRE = JVM + Libraries
■ 5. Java Versions
■ Concept
- Java evolves with major releases introducing new features.
- LTS (Long Term Support) versions are most important for industry use.
- Current trend: new release every 6 months, LTS every 3 years.
■ Major Versions & Features
- JDK 1.0 (1996) → First official release.
- JDK 1.2 (1998) → Collections Framework.
- JDK 1.5 / Java 5 (2004) → Generics, Annotations, Enum, Autoboxing.
- Java 6 (2006) → Scripting, JDBC 4.0, better Web Services support.
- Java 7 (2011) → Try-with-resources, Diamond Operator, switch on String.
- Java 8 (2014, LTS) → Lambda Expressions, Streams, Default Methods, Functional Interfaces,
Date/Time API.
- Java 9 (2017) → Module System (Project Jigsaw), JShell.
- Java 10 (2018) → var keyword for local variables.
- Java 11 (2018, LTS) → Removed JavaFX/Applets, new HttpClient API, var in lambda, String
methods (isBlank, lines).
- Java 12–15 → Switch expressions, text blocks ("""), records (preview).
- Java 17 (2021, LTS) → Sealed Classes, Pattern Matching, Strong Encapsulation, Records
finalized.
- Java 21 (2023, LTS) → Virtual Threads (Project Loom), Pattern Matching for switch, Sequenced
Collections.
■ Important Rules / Exceptions
- Backward compatibility → Older code runs in newer versions (mostly).
- Features marked preview need --enable-preview flag.
- Oracle provides paid support for LTS (8, 11, 17, 21).
- Non-LTS versions get support only until the next release.
■ Interview Questions
- Which versions of Java are LTS?
- What is the difference between Java 8 and Java 11?
- What are the key features of Java 17?
- Why was the module system introduced in Java 9?
- What are virtual threads in Java 21?
■■ Example
// Java 8 Example: Lambda + Stream
import java.util.*;
class VersionDemo {
public static void main(String[] args) {
List names = Arrays.asList("Sahil", "Amit", "Raj");
names.stream()
.filter(n -> n.startsWith("S"))
.forEach(System.out::println);
}
}
■ Tips to Remember
- Always highlight Java 8, 11, 17, 21 in interviews → most used in projects.
- Know at least 2–3 new features per LTS version.
- Java 8 = Lambdas & Streams, Java 11 = HttpClient & removed JRE,
Java 17 = Sealed classes, Records, Java 21 = Virtual Threads.
- Interviewers often ask: “Which Java version are you working with?” → be ready to explain its
features.
■ 6. Java Path and JAVA_HOME
■ Concept
- JAVA_HOME: Environment variable pointing to the Java installation directory (e.g., C:\Program
Files\Java\jdk-17).
- PATH: Tells OS where to find executables (javac, java) without typing full path.
■ Steps to Set JAVA_HOME and PATH
Windows
1. Install JDK (e.g., JDK 17).
2. Set JAVA_HOME = C:\Program Files\Java\jdk-17.
3. Add %JAVA_HOME%\bin to PATH.
4. Verify:
java -version
javac -version
Linux / macOS
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk
export PATH=$JAVA_HOME/bin:$PATH
source ~/.bashrc
■ Rules / Exceptions
- Without JAVA_HOME: Some tools (Maven, Gradle, Tomcat) may fail to detect Java.
- PATH must include bin: Otherwise, java/javac won’t run globally.
- If multiple Java versions installed → Use update-alternatives (Linux) or tools like SDKMAN.
■ Interview Questions
- What is the difference between PATH and JAVA_HOME?
- Why do we need to set JAVA_HOME even if PATH works?
- How do you configure multiple Java versions in one system?
- How to check which java your system is using?
■■ Example
# Example: Check Java version and installation path
java -version
which java # Linux/macOS
where java # Windows
Output (Java 17 example)
java version "17.0.9" 2023-10-17 LTS
Java(TM) SE Runtime Environment (build 17.0.9+11-LTS)
Java HotSpot(TM) 64-Bit Server VM
■ Tips to Remember
- JAVA_HOME is must when using Maven, Gradle, Jenkins.
- Always check java -version after setup.
- If interview asks “Your Java command is not recognized, what will you do?” → Answer: Check
PATH variable.
- For troubleshooting → echo %JAVA_HOME% (Windows), echo $JAVA_HOME (Linux).