Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Java / Java Basics

    Your First Program

    Java is a programming language released by Sun Microsystems in 1995 (now owned by Oracle). It's one of the most used languages

    Chapter 1 of 97 min read

    Start With the Problem#

    StudyBuddy needs to do something simple: for each student, take the number of quiz questions they answered correctly, give 10 points per correct answer, add a 20-point bonus if they finished on time, print a grade, and say who scored highest.

    A human does this with a pencil in two minutes. But the computer (How Computers Work Ch 5) is a cook who only follows tiny, exact steps: "put 18 in a box", "multiply the box by 10", "if the box is at least 200, write A". It will do this billions of times per second, perfectly — but only if every step is written exactly, with no ambiguity.

    A programming language is how you write those exact steps in a form a human can read and a machine can translate. By the end of this file you'll write that whole StudyBuddy report program yourself (Chapter 9) — and every piece of it will be something you understand.

    The Plain Picture#

    A recipe card in a cookbook has a fixed layout: the dish name at the top, then "Steps:", then numbered steps. The cook always starts reading at "Steps:". A Java program is the same: a fixed outer shape, and one special place where running begins.

    The Real Names#

    • Java is a programming language released by Sun Microsystems in 1995 (now owned by Oracle). It's one of the most used languages for large backend systems (banks, Amazon, Google, LinkedIn, Uber), Android apps, and — for you — DSA interviews.

    • Java code is compiled to bytecode and run by the JVM with a JIT compiler (How Computers Work Ch 8).

    • Three names people mix up:

    NameWhat it isContains
    JVM (Java Virtual Machine)the program that runs bytecodeinterpreter + JIT + garbage collector
    JRE (Java Runtime Environment)everything needed to run Java programsJVM + the standard library
    JDK (Java Development Kit)everything needed to write Java programsJRE + the compiler javac + tools

    You install the JDK. Modern Java distributions no longer ship a separate JRE.

    • Versions: a new Java version comes out every 6 months; every 2 years one is an LTS (Long-Term Support) release that companies standardise on: 8, 11, 17, 21, 25. This curriculum uses Java 21.

    Under the Hood#

    Install and check#

    Terminal
    java -version      # java version "21.0.8" 2025-07-15 LTS
    javac -version     # javac 21.0.8

    If they're missing: brew install openjdk@21 (then follow the message Homebrew prints about linking it), or download a JDK from adoptium.net.

    The first program#

    Make a file named exactly Hello.java:

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

    Run it — two ways:

    Terminal
    javac Hello.java     # 1. compile: creates Hello.class (bytecode)
    java Hello           # 2. run the class → Hello, StudyBuddy!
    
    java Hello.java      # shortcut (Java 11+): compile in memory and run a single file in one step

    Reading it line by line#

    Output
      public class Hello {                              ← a CLASS named Hello. Every Java program lives inside classes.
                                                          The file name must match: Hello.java
          public static void main(String[] args) {      ← the MAIN METHOD: where running starts. Memorise this line.
              System.out.println("Hello, StudyBuddy!"); ← one STATEMENT: print a line of text. Ends with ;
          }                                             ← end of main
      }                                                 ← end of class
    • Braces { } group lines into a block — "everything inside belongs together".

    • Semicolons ; end each statement, like a full stop ends a sentence.

    • Java is case-sensitive: Main ≠ main, String ≠ string.

    • Comments are notes for humans that the compiler ignores:

      Java
      // a comment until the end of the line
      /* a comment
         over several lines */

    What the words public, static, void, String[] args mean — each is taught properly later:

    For now: treat public static void main(String[] args) as the fixed "Steps:" heading of the recipe card.

    Compile errors vs runtime errors#

    • A compile error is caught by javac before the program runs — a missing ;, a misspelled name, a wrong type. The compiler tells you the file and line:

      Output
      Hello.java:3: error: ';' expected
    • A runtime error (an exception) happens while the program is running — e.g. reading past the end of an array (Ch 8). Java is a statically typed language (Ch 2), so it catches many mistakes at compile time that languages like Python only find while running.

    Gotchas

    • File name must match the public class name. public class Hello must be in Hello.java, or javac refuses.

    • Read the first compile error first. One missing brace can produce 20 errors; fixing the first often fixes the rest.

    Check yourself

    4 questions
    1

    What's the difference between the JDK, the JRE and the JVM?

    Show answer

    JVM runs bytecode; JRE = JVM + standard library (to run programs); JDK = JRE + compiler and tools (to write programs).

    2

    What does javac produce, and what runs it?

    Show answer

    A .class file of bytecode; the JVM runs it.

    3

    Where does a Java program start running?

    Show answer

    In the main method: public static void main(String[] args).

    4

    Compile error vs runtime error — give an example of each.

    Show answer

    Compile error: missing semicolon (caught by javac). Runtime error: accessing an array index that doesn't exist (happens while running).