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, 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 1995; now stewarded by Oracle and the OpenJDK community).
  • Source files (.java) compile to bytecode (.class) that the Java Virtual Machine executes — the “write once, run anywhere” idea.
  • Used for backend services, Android (historically), enterprise apps, big data tools, and many coding interviews.

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.
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

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.
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 is a current LTS: records, text blocks, switch expressions, and sealed classes are fair game alongside classic Java 8 streams/lambdas.
L2

Advanced — Introduction to Java

Compilation, bytecode, and the launcher

  • javac is a compiler, not an interpreter. The JVM then interprets 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.
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 (Sun Microsystems). The project was originally called Oak, then renamed Java.

Q3) 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.

Q4) Is Java compiled or interpreted?

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

Q5) Why is Java called platform independent?

Answer Bytecode is portable. You compile once; any compatible JVM on Windows, Linux, or macOS can run the same .class files.

Q6) 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.

Q7) 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.

Q8) Do you still need a separate JRE in Java 17?

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.

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);
    }
}
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).
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 (11, 17, 21).
  6. Move Hello World into package demo and run it from the correct working directory.