Skip to main content

Java Basics

Introduction to Java

Complete lesson for Introduction to Java using LEARNCODEGURU’s 6-level learning model — from basics to advanced practice, with Java OOP and Collections where relevant.

L1

Basics — Introduction to Java

Section: Java Basics. Start Java with history, versions, features, JDK vs JRE vs JVM, compilation, and your first Hello World.

What is Java?

  • Java is a general-purpose, class-based, object-oriented language created by James Gosling at Sun Microsystems. First public release was 1995; the language is now stewarded by Oracle and the OpenJDK community.
  • You write .java source, javac compiles it to bytecode (.class), and the Java Virtual Machine executes that bytecode — the “write once, run anywhere” (WORA) idea.
  • Java is strongly typed, garbage-collected, and multithreaded. You do not manage raw pointers the way you do in C or C++.
  • It is used for backend services, Android (historically and still in many codebases), enterprise apps, big-data tools (Hadoop, Spark APIs), desktop tools, and a large share of campus coding interviews.

History of Java

  • 1991: Sun’s Green Project starts. Gosling’s language is first called Oak (named after an oak tree outside his office).
  • 1995: Oak is renamed Java. The public slogan is Write Once, Run Anywhere — compile to bytecode, run on any compatible JVM.
  • 1996: JDK 1.0 ships. Developers compile .java files with javac and run them with java.
  • 1998: Java 2 (J2SE 1.2) brings Swing and the Collections Framework that interviews still use (List, Map, Set).
  • 2004: Java 5 is a language leap — generics, enums, annotations, autoboxing, varargs, and the enhanced for loop.
  • 2014: Java 8 becomes the industry default for years — lambdas, Streams, Optional, and java.time.
  • 2017–2018: Java 9 adds the module system; a six-month release train starts. Java 11 is the first modern Long-Term Support (LTS) line.
  • 2021 onward: Java 17, then 21 (2023) and 25 (2025) are the LTS versions you should know for jobs and new projects.

History of Java — visual timeline

Java did not start as “Java”. James Gosling’s Green Project language was first called Oak (1991), renamed Java for the 1995 public release, then grew through JDK 1.0, Java 2, the Java 5 language leap, Java 8 lambdas, and today’s LTS line (11, 17, 21, 25). The bullets above have the exact years; the poster is the picture version.

History of Java language timeline: 1991 Green Project and Oak by James Gosling at Sun Microsystems, 1995 renamed Java with Write Once Run Anywhere, 1996 JDK 1.0, 1998 Java 2 with Swing and Collections, 2004 Java 5 generics and enums, 2014 Java 8 lambdas and streams, 2018 Java 11 LTS, and today Java 17, 21, and 25 LTS.
History of Java — from Oak and JDK 1.0 to modern LTS versions
  • 1991–1995: Oak at Sun; public Java release and WORA.
  • 1996–2004: JDK 1.0 → Java 2 collections → Java 5 language features.
  • 2014: Java 8 lambdas, Streams, and java.time.
  • Today: LTS versions 11, 17, 21, and 25 on OpenJDK.

Features of Java

  • Object-oriented: almost everything lives in classes; you model data with fields and behaviour with methods.
  • Platform independent: bytecode is portable; the JVM adapts it to Windows, Linux, or macOS.
  • Simple (relative to C++): no pointer arithmetic, no multiple class inheritance, automatic memory management via GC.
  • Secure and robust: bytecode verification, a security manager model (historically), strong typing, and exception handling.
  • Multithreaded: Thread, Runnable, and (from Java 21) virtual threads are part of the platform.
  • Rich standard library: I/O, collections, concurrency, networking, dates, and more in java.* / javax.* / jdk.*.
  • High performance in practice: the HotSpot JVM interprets first, then JIT-compiles hot methods to native code.

JDK vs JRE vs JVM

  • These three names are the most common Java-platform interview question. Do not mix them up.
  • Since Java 11, Oracle no longer ships a separate consumer JRE the old way — you typically install a JDK. The *concepts* still matter.
  • A shortcut to remember: JVM runs bytecode, JRE is “runtime only”, JDK is “developer kit” (compiler + runtime).
ComponentContainsUsed for
JVMRuntime engine: class loader, bytecode verifier, interpreter / JIT, GCExecute .class bytecode on a host OS/CPU
JREJVM + core class libraries (java.lang, java.util, …)Run already-compiled Java programs
JDKJRE + development tools (javac, jar, javadoc, debugger)Write, compile, package, and run Java code

Java editions — SE, EE, ME

  • Java SE (Standard Edition) is what this tutorial teaches: language + core libraries + JDK tools. Interviews mean SE unless they say otherwise.
  • Jakarta EE (formerly Java EE) adds server APIs: servlets, JPA, REST, messaging — used in enterprise backends.
  • Java ME targeted constrained devices. You will rarely meet it in campus interviews today.

First program — Hello World

  • The public class name must match the file name: save this as HelloWorld.java.
  • Compile with javac HelloWorld.java (creates HelloWorld.class), then run java HelloWorld (no .class suffix).
  • main must be public static void main(String[] args) so the launcher can start the program without an instance.
  • On Java 11+ you can also run a single-file program with java HelloWorld.java (the launcher compiles it in memory).
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, Java!");
    }
}

Why Java is still taught first for interviews

  • Strong static typing, a huge standard library, and a predictable memory model (GC, no raw pointers).
  • Checked exceptions, collections, and OOP vocabulary show up constantly in campus and service-company interviews.
  • Java 17 and Java 21 are current LTS lines: records, text blocks, switch expressions, sealed classes, and virtual threads sit beside classic Java 8 streams and lambdas.
  • Knowing history + versions (Oak → Java 5 → Java 8 → LTS 11/17/21/25) is a common warm-up question before coding rounds.
L2

Advanced — Introduction to Java

Compilation, bytecode, and the launcher

  • javac is a compiler, not an interpreter. The JVM then interprets bytecode and/or JIT-compiles hot methods to native code.
  • java HelloWorld uses the application class loader; a public class in the default package is found on the current directory classpath (.).
  • If you use a package (package com.demo;), the file must live in a matching folder and you run java com.demo.HelloWorld from the package root.
  • One public top-level class per .java file. Additional package-private classes in the same file are allowed but uncommon in tutorials.
  • main can also be public static void main(String... args) (varargs). A non-static main will not start the program.
  • Typical failure modes: compile error (syntax), “cannot find symbol” (wrong classpath / missing import), “could not find or load main class” (wrong directory or package).

Java versions and LTS

  • Java used to ship a big release every few years (1.0 → 1.1 → 1.2/Java 2 → 1.3 → 1.4 → 5 → 6 → 7 → 8). From Java 9 (2017) it moved to a six-month feature cadence.
  • LTS (Long-Term Support) versions are the ones companies keep for years: Java 8 (legacy), 11, 17, 21, and 25.
  • Non-LTS versions (12, 13, 14, …) are fine for trying new syntax, but production and most interviews assume an LTS.
  • Set JAVA_HOME to one JDK and put %JAVA_HOME%\bin (Windows) or $JAVA_HOME/bin (macOS/Linux) first on PATH. Then java -version and javac -version must match.
VersionYearWhy interviews mention it
JDK 1.0 / 1.11996–1997Birth of the JDK; inner classes, JDBC
Java 2 (1.2)1998Swing + Collections Framework
Java 52004Generics, enums, annotations, autoboxing, enhanced for
Java 82014Lambdas, Streams, Optional, java.time — still everywhere
Java 11 LTS2018First modern LTS; var; HTTP Client; no separate consumer JRE
Java 17 LTS2021Records, sealed classes, text blocks, pattern matching
Java 21 LTS2023Virtual threads, sequenced collections, switch patterns
Java 25 LTS2025Current LTS line for new projects

Java versions at a glance

Interviewers rarely want every release number. They want the language leaps (Java 5, Java 8) and the LTS line companies actually run: 11, 17, 21, and 25. The poster below is the cheat-sheet; the table above has the interview one-liners.

Java versions infographic from JDK 1.0 in 1996 through Java 1.1, Java 2, Java 5, Java 8, then LTS releases Java 11, Java 17, Java 21, and Java 25, highlighting lambdas, streams, records, sealed classes, and virtual threads.
Java versions — JDK 1.0 to Java 25 LTS; companies standardise on LTS releases
  • Language leaps: Java 5 (generics) and Java 8 (lambdas / Streams).
  • LTS to remember: 8 (legacy), 11, 17, 21, 25.
  • Cadence: from Java 9, a new feature release about every six months.
  • Learning tip: install JDK 17 or 21 so your syntax matches most interviews.

How “write once, run anywhere” actually works

  • The compiler does not emit Windows or Linux machine code. It emits JVM bytecode — a stable instruction set the JVM understands.
  • Each OS has its own JVM (HotSpot, Eclipse OpenJ9, …). That JVM loads classes, verifies bytecode, and either interprets or JIT-compiles it.
  • WORA is not magic: JNI native libraries, file-path separators, and line endings can still be platform-specific. Core Java SE code is what stays portable.
  • System.getProperty("java.version") and "os.name" are the usual way to inspect the runtime you actually launched.

Packages, classpath, and editions in real projects

  • Default package (no package statement) is only for tiny demos. Real code lives in reverse-DNS packages such as com.learncodeguru.demo.
  • Classpath is a list of directories and JARs the class loader searches. The current directory . is on the classpath by default for simple java launches.
  • From Java 9, the module path (--module-path) sits beside the classpath. Beginners can ignore modules until they meet module-info.java.
  • Build tools (Maven, Gradle) hide most of this; interviews still expect you to compile a single file by hand at least once.
L3

Tricky Interview Points

Q1) What is Java?

Answer Java is a class-based, object-oriented language that compiles to bytecode executed by the JVM, which gives platform independence.

Q2) Who created Java, and what was its original name?

Answer James Gosling at Sun Microsystems. The Green Project language was first called Oak, then renamed Java in 1995.

Q3) What does “write once, run anywhere” mean?

Answer javac produces bytecode (.class), not OS-specific machine code. Any compatible JVM on Windows, Linux, or macOS can run the same class files.

Q4) What is the difference between JDK, JRE, and JVM?

Answer JVM executes bytecode. JRE = JVM + standard libraries (needed to run programs). JDK = JRE + tools like javac (needed to develop). You install a JDK to compile.

Q5) Is Java compiled or interpreted?

Answer Both. javac compiles source to bytecode; the JVM interprets bytecode and JIT-compiles hot code to native instructions.

Q6) Name the major Java versions interviewers expect.

Answer Remember Java 5 (generics), Java 8 (lambdas/streams), then LTS lines 11, 17, 21, and 25. Java 8 is still common in legacy code.

Q7) What is an LTS version, and which Java versions are LTS?

Answer Long-Term Support: vendors patch it for years. Modern LTS versions are 11, 17, 21, and 25. Java 8 is the older LTS still seen in industry.

Q8) Why was Java 8 such a big deal?

Answer It added lambdas, the Streams API, Optional, and java.time. A huge amount of production code and interview problems still assume Java 8.

Q9) What changed from Java 9 onward?

Answer The module system (JPMS) arrived in Java 9, and Oracle moved to a six-month release train. Non-LTS versions ship features; LTS versions are what companies standardise on.

Q10) Do you still need a separate JRE in Java 17 or 21?

Answer Conceptually JRE still means “runtime only”, but modern distributions ship JDKs. You can create a custom runtime with jlink. Interviews still expect JDK > JRE > JVM.

Q11) What is the signature of the entry-point method?

Answer public static void main(String[] args). It must be public and static so the launcher can invoke it without constructing an object. String... args is also valid.

Q12) What happens if the public class name does not match the file name?

Answer javac fails. The public top-level class name must match the .java file name.

Q13) What is the difference between Java SE, EE, and ME?

Answer SE is the language plus core JDK (this course). EE / Jakarta EE adds server APIs (servlets, JPA). ME targeted small devices and is rarely asked now.

Q14) Why is Java called platform independent but the JVM is not?

Answer Bytecode is platform independent. The JVM itself is a native program written for each OS/CPU. You need a JVM build for that platform to run the same .class files.

Q15) How do you check which Java version is installed?

Answer Run java -version and javac -version. They should match. You can also print System.getProperty("java.version") from a program.

L4

Simple Programs

Hello World

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, Java!");
    }
}

Hello with command-line args

public class HelloArgs {
    public static void main(String[] args) {
        String name = args.length > 0 ? args[0] : "learner";
        System.out.println("Welcome to Java, " + name);
        System.out.println("Argument count = " + args.length);
    }
}

Print Java version and OS

public class JavaRuntimeInfo {
    public static void main(String[] args) {
        System.out.println("Java version : " + System.getProperty("java.version"));
        System.out.println("Java vendor  : " + System.getProperty("java.vendor"));
        System.out.println("OS name      : " + System.getProperty("os.name"));
        System.out.println("Home of JDK  : " + System.getProperty("java.home"));
    }
}

Tiny history quiz in code

public class JavaHistoryFacts {
    public static void main(String[] args) {
        System.out.println("Creator     : James Gosling");
        System.out.println("First name  : Oak");
        System.out.println("Public year : 1995");
        System.out.println("First JDK   : 1.0 (1996)");
        System.out.println("Famous LTS  : 8, 11, 17, 21, 25");
    }
}

Compile-style welcome (package-ready later)

public class JavaWelcome {
    public static void main(String[] args) {
        String topic = "Introduction to Java";
        System.out.println("LEARNCODEGURU");
        System.out.println("Now learning: " + topic);
        System.out.println("Next: javac this file, then java JavaWelcome");
    }
}
L5

Problem Solving Tricks

  • File name = public class name. HelloWorld.java → public class HelloWorld.
  • Run java HelloWorld, not java HelloWorld.class.
  • If compilation succeeds but java says “could not find or load main class”, you are in the wrong directory or used a package without matching folders.
  • System.out is a PrintStream; println adds a platform line separator.
  • Print the Java version you are using: java -version and javac -version — they must match if you have several JDKs installed.
  • For Java 11+ you can run a single-file program with java HelloWorld.java (no separate javac step).
  • Memorise the version story in three beats: Java 5 (generics), Java 8 (lambdas), then LTS 11 → 17 → 21 → 25.
  • Prefer an LTS JDK for learning (17 or 21). Live features from a non-LTS release may not be on your interviewer's machine.
  • Set JAVA_HOME and put its bin folder first on PATH so IDEs, Maven, and the terminal all use the same JDK.
  • Bytecode is portable; JNI .dll / .so files are not. If a program needs native libraries, WORA no longer fully applies.
L6

Practice Programs List

  1. Write HelloWorld.java, compile with javac, and run with java.
  2. Change the class name without renaming the file and read the compiler error.
  3. Print your name and the value of args.length when you pass 0, 1, and 3 arguments.
  4. Explain JDK, JRE, and JVM in four sentences from memory, then check the table.
  5. Run java -version and write down whether you have an LTS (8, 11, 17, 21, or 25).
  6. Write a program that prints java.version, java.vendor, and os.name.
  7. From memory, list: original name (Oak), creator, public year (1995), and first JDK year (1996).
  8. Make a two-column notes sheet: Java 5 features vs Java 8 features vs Java 11/17 LTS headlines.
  9. Move Hello World into package demo and run it from the correct working directory.
  10. On Java 11+, run the same file with java HelloWorld.java and compare that with the javac + java two-step flow.