SyntaxPilot
Courses/Java/Lesson 1
Video coming soonThis lesson’s video is being made

Start with the notes below; the video appears here as soon as it is on YouTube.

Java · Lesson 1 of 51

JDK, JRE, JVM & first program

scheduled

These are the notes for Java episode 1. You will learn what Java is and where it is used, how a .java file becomes output (javac, bytecode and the JVM), the classic JDK vs JRE vs JVM interview question, your first program line by line, print vs println, escape sequences, the + concatenation trap, and how to read compile errors. Every example below runs as-is in the SyntaxPilot compiler.

The problem: one program, many computers

You write a program on your Windows laptop. Your friend has a Mac, the college lab runs Linux, and the company server is something else again. In many languages you would have to rebuild the program separately for each of them.

Java's answer is its famous promise: Write Once, Run Anywhere. You compile your code once, and the same compiled file runs on Windows, Mac and Linux with the same output.

Think of a movie file:

Movie Java
You make the movie file once You write Main.java and compile it to Main.class
You copy the same file to a pen drive The same bytecode file goes to every machine; it never changes
Every TV or laptop has its own video player Every operating system has its own JVM
The movie plays the same everywhere Same output on every OS

What is Java?

  • A programming language launched by Sun Microsystems in 1995, maintained today by Oracle.
  • Thirty years later it is still one of the most widely used languages: Android apps, banking systems and the software at big companies.
  • On the backend it is used heavily with frameworks like Spring Boot.
  • It is one of the most common languages in interviews and placements.
  • Its superpower: Write Once, Run Anywhere (interviewers love this line).

How Java code runs: javac, bytecode, JVM

  1. You write source code in a file called Main.java.
  2. javac, the Java compiler, checks every line. Any mistake gives a compile error right here, before the program runs. That is a good thing: bugs are caught early.
  3. If everything is fine, javac creates Main.class. Inside it is bytecode, and this bytecode is the same for every operating system.
  4. That one Main.class file can go to any computer: Windows, Mac or Linux.
  5. Every operating system has its own JVM (Java Virtual Machine), which translates the bytecode for that machine and runs it. Same output everywhere.
Main.java  --javac-->  Main.class (bytecode)  --java (JVM)-->  output

In a terminal these are two commands:

javac Main.java
java Main

On SyntaxPilot, one Run button (or Ctrl + Enter) does both.

Three points for your notebook: (1) javac checks all the code first: a mistake means a compile error. (2) Bytecode (.class) is the same for every OS. (3) Each OS has its own JVM, so the same bytecode runs anywhere.

JDK vs JRE vs JVM

Think of boxes inside boxes:

Part Full form What it contains What it is for
JVM Java Virtual Machine the engine that runs bytecode running .class files
JRE Java Runtime Environment JVM + ready-made libraries (java.lang, java.util, java.io …) running Java programs
JDK Java Development Kit JRE + developer tools (javac compiler, debugger, jar, javadoc) building Java programs

So JDK ⊃ JRE ⊃ JVM. To only run programs the JRE is enough; to build programs you need the JDK, which is why developers always install the JDK.

The kitchen version:

Kitchen Java
The stove actually cooks the food the JVM actually runs the bytecode
A kitchen with a stove and ready ingredients the JRE: JVM + libraries
The chef's full kit: kitchen + knives and tools the JDK: JRE + javac, debugger, jar

You are the developer, you are the chef, so you need the JDK.

Interview tip: since Java 11, Oracle no longer ships a separate JRE download; you install a JDK, which includes everything needed to run programs too. The concept (JVM inside JRE inside JDK) is still what interviewers ask about.

The SyntaxPilot compiler

  • Left: the code editor. The file is called Main.java.
  • Right: the terminal, where output appears. The command at the top shows javac Main.java then java Main.
  • Java is selected in the language dropdown; press Run or Ctrl + Enter.
  • Nothing to install: open the browser and start writing Java.

Your first Java program

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

Output:

Hello, World!

Some languages do this in one line and Java takes five, but every line has a clear job.

What each line means

Line Meaning
public class Main { All Java code lives inside a class. If the class is public, its name must match the file name: class Main lives in Main.java. On SyntaxPilot, always name the class Main.
public static void main(String[] args) { The entry point: execution always starts here. public, static, void and args are explained in the OOP episodes; for now, write this line exactly like this.
System.out.println("Hello, World!"); Prints the text and moves to the next line. System has a capital S. The semicolon ends the statement.
} } Close main first, then the class. Every { you open must be closed.

The building analogy:

A building Your program
The building: everything is inside it class Main: all code lives in a class
The main door: everyone enters here main(): the JVM enters here
Walls of a room: where it starts and ends { }: where a block starts and ends
Full stop: ends a sentence ;: ends a statement

How the JVM runs it

  1. The JVM loads class Main.
  2. It looks for the main method, the entry point.
  3. System.out.println sends Hello, World! to the screen and the cursor moves to the next line.
  4. When the JVM reaches the last closing bracket of main, the program ends.
  • print writes the text and the cursor stays on the same line.
  • println ("print line") writes the text, then moves to the next line.
  • An empty println() just moves to the next line.
public class Main {
    public static void main(String[] args) {
        System.out.print("Hello ");
        System.out.print("Java");
        System.out.println();
        System.out.println("Line two");
    }
}

Output:

Hello Java
Line two

Keyboard version: typing words = print; typing then pressing Enter = println("..."); pressing only Enter = println().

Note the space inside "Hello ": print adds nothing on its own, not even a space.

The + trap: Java reads left to right

When one side of + is text (a String), + does not add; it glues (this is called concatenation). Java works left to right:

public class Main {
    public static void main(String[] args) {
        System.out.println("5 + 3 = " + 5 + 3);
        System.out.println("5 + 3 = " + (5 + 3));
    }
}

Output:

5 + 3 = 53
5 + 3 = 8
  • Line 1: "5 + 3 = " + 5 gives the text "5 + 3 = 5", then + 3 glues on 3: 53.
  • Line 2: the brackets are calculated first, 5 + 3 = 8, then joined to the text.

It is like a hotel label printer: given "Room ", 5, 3, it just sticks each piece on the label and prints Room 53, not Room 8. Phone numbers and PIN codes are joined like this too.

Rule: when printing, always put the maths in brackets.

The order matters. Numbers before any text are still added:

System.out.println("Java" + 1 + 2);     // Java12
System.out.println("Java" + (1 + 2));   // Java3
System.out.println(1 + 2 + "Java");     // 3Java  (1 + 2 happens before any text appears)

Escape sequences

Special characters inside a string start with a backslash \:

Escape Meaning
\n new line
\t tab (useful for lining up columns)
\" a double quote inside a string
\\ a backslash itself
public class Main {
    public static void main(String[] args) {
        System.out.println("Line 1\nLine 2");
        System.out.println("Name:\tAarav");
        System.out.println("He said \"Hi\"");
        System.out.println("C:\\Users\\Java");
    }
}

Output:

Line 1
Line 2
Name:   Aarav
He said "Hi"
C:\Users\Java

Without the backslash, "He said "Hi"" would end the string after said, and the code would not compile.

Reading and fixing errors

Getting an error is not bad: every programmer sees errors every day. Reading and fixing them is a skill. A compile error is like a teacher checking homework: javac checks it first, marks the mistake with the line number, you fix it, and then it is accepted.

Missing semicolon

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello, World!")
    }
}
Main.java:3: error: ';' expected
        System.out.println("Hello, World!")
                                           ^
1 error

The message gives the file, the line (3) and the problem, and the caret ^ points exactly where the semicolon should go.

Lowercase system

system.out.println("Hello, World!");
Main.java:3: error: package system does not exist

Java is case sensitive. It thinks system is a package name, and no such package exists. Fix: System with a capital S.

Class name does not match the file name

public class Hello {   // saved in Main.java
    ...
}
Main.java:1: error: class Hello is public, should be declared in a file named Hello.java

Fix: rename the class to Main (on SyntaxPilot the file is always Main.java).

Common mistakes (and the real error messages)

Mistake Code Error
Missing semicolon System.out.println("Hi") ';' expected
Lowercase system system.out.println("Hi"); package system does not exist
Class name differs from file name public class Hello in Main.java class Hello is public, should be declared in a file named Hello.java
Capital P in println System.out.Println("Hi"); cannot find symbol (symbol: method Println(String))
Missing closing quote System.out.println("Hi); unclosed string literal
Missing closing } class never closed reached end of file while parsing
main misspelt as Main public static void Main(String[] args) compiles, but running gives Error: Main method not found in class Main, please define the main method as:
Maths without brackets "Total: " + 5 + 3 no error, prints Total: 53

Comments: notes for humans

The compiler ignores comments. Java has three kinds:

/**
 * My first Java program.
 */
public class Main {
    public static void main(String[] args) {
        // single-line comment
        System.out.println("Hi");
        /* multi-line
           comment */
    }
}

Output:

Hi
Kind Syntax Use
Single-line // ... everything after // on that line is ignored
Multi-line /* ... */ any number of lines between /* and */
Javadoc /** ... */ documentation is generated from it; seen above classes and methods in professional code

Good comments explain why, not just what. Putting // in front of a line is also a quick way to switch it off while testing.

Mini project: a profile card

public class Main {
    public static void main(String[] args) {
        // Mini project: profile card
        System.out.println("=".repeat(30));
        System.out.println("Name:\tAarav");
        System.out.println("City:\tLucknow");
        System.out.println("Goal:\tJava Developer");
        System.out.println("=".repeat(30));
        System.out.println("Episode " + 1 + " done with " + "SyntaxPilot");
    }
}

Output:

==============================
Name:   Aarav
City:   Lucknow
Goal:   Java Developer
==============================
Episode 1 done with SyntaxPilot
  • "=".repeat(30) repeats the = sign 30 times to make a border (repeat needs Java 11 or newer).
  • \t lines up the values.
  • The last line uses concatenation: here there is text before the 1, so it is glued as text, which is exactly what we want.

Put in your own details and build your own card.

Practice

  1. Print 1 2 3 4 5 on one line using only print (and one final println).
  2. Print a small marks table (subject and marks) for three subjects using \t.
  3. Without running it, predict the output of System.out.println(10 + 20 + "Java" + 10 + 20);. Then run it.
  4. Print this exact line, including the quotes and the backslash: File saved to "D:\notes".
  5. Explain JDK vs JRE vs JVM in your own words, in two lines.
Solutions and hints
  1. Five calls in a row: System.out.print("1 "); System.out.print("2 "); System.out.print("3 "); System.out.print("4 "); System.out.println("5"); prints 1 2 3 4 5.
  2. One println per row with \t between the columns: System.out.println("Subject\tMarks");, then System.out.println("Maths\t90");, System.out.println("Science\t85"); and so on.
  3. 30Java1020. Left to right: 10 + 20 is maths (30) because no text has appeared yet; after "Java" everything is glued.
  4. System.out.println("File saved to \"D:\\notes\"");
  5. JVM runs bytecode. JRE = JVM + libraries, enough to run programs. JDK = JRE + tools like javac, needed to build programs.

Quick quiz

What will this code print?

System.out.println("Java" + 1 + 2);
  • A. Java3
  • B. Java12
  • C. Error
  • D. 3Java
Answer

B. Java12. Java works left to right: "Java" + 1 gives the string "Java1", then + 2 gives "Java12". After a string, + glues text. For Java3, write "Java" + (1 + 2).

Interview questions

  • What is the difference between JDK, JRE and JVM?
  • What does "Write Once, Run Anywhere" mean, and how does bytecode make it possible?
  • What does javac produce, and what does the java command do?
  • Why must a public class name match its file name?
  • What is the difference between print and println?
  • What is the output of "5 + 3 = " + 5 + 3, and why?
  • Is the JVM platform-independent? (No: bytecode is platform-independent; each OS has its own JVM.)

Summary

  • Java: launched 1995, maintained by Oracle, used in Android, banking and backend systems.
  • javac compiles Main.java into bytecode (Main.class); the JVM runs it on any OS: Write Once, Run Anywhere.
  • JDK ⊃ JRE ⊃ JVM: developers install the JDK.
  • Every program needs public class Main and public static void main(String[] args); statements end with ;.
  • print stays on the line, println moves to the next line.
  • After a string, + glues; put maths in brackets.
  • Escape sequences: \n, \t, \", \\.
  • Read error messages: file, line, problem. Three kinds of comments: //, /* */, /** */.

Next episode: variables and Java's eight primitive data types (int, double, char, boolean and the rest), and how they are stored in memory.