Introduction to Java

Reviewed & published by Brayan K

Java is a high-level, class-based, object-oriented programming language designed to run on any platform via the Java Virtual Machine (JVM), widely used for enterprise systems, Android apps, and large-scale backends.

Part of the free Java course at LearnCodingFast — hands-on lessons with worked examples and the output they print, plus practice exercises and a quick quiz.

Beginner-Safe, No Assumptions — Learn Java from absolute zero. Every concept is explained before it's used.

What You'll Learn in This Lesson

🔧 Before You Start: Do You Have Java?

To write and run Java programs on your own computer, you need the JDK (Java Development Kit). Let's check if it's already installed.

Open Command Prompt (search "cmd" in Start menu) and type:

java --version

Open Terminal and type:

If you see something like "java 21.0.x" — you're ready!

📁 Running Java on Your Computer

Let's create and run your very first Java program. Follow these steps exactly:

Step 1: Create a file

Open any text editor (Notepad, VS Code, or IntelliJ). Create a new file and name it exactly:

HelloWorld.java

⚠️ The filename must match the class name exactly (including capitalization). This is a Java rule — not optional.

Step 2: Write this code

Type (or copy) this into your file:

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

Step 3: Save the file

Save it somewhere you can find it easily (like your Desktop).

Step 4: Open your terminal

Step 5: Navigate to your file

Use the cd command to go to where you saved your file:

cd Desktop

Step 6: Compile the program

Unlike Python, Java needs to be compiled first. This turns your code into something the computer can run:

javac HelloWorld.java

This creates a file called HelloWorld.class — that's the compiled version.

Step 7: Run the program

java HelloWorld

Notice: no .java or .class extension when running — just the class name.

Hello, World!

If you see this — congratulations! You just compiled and ran your first Java program.

1️⃣ What Is Java? (Plain English)

Java is a programming language.

A programming language is a way for humans to give instructions to a computer. Think of it like learning a new language — but instead of talking to people, you're talking to machines.

Computers do nothing by themselves. They only follow instructions exactly as written. Your job is to write those instructions clearly.

Java is designed so that:

Java is used for:

Java consistently ranks in the top 3 most in-demand programming languages worldwide. As of 2024, over 9 million developers use Java, and the average Java developer salary is $100K+ in the US.

2️⃣ What Is a Program?

A program is simply a list of instructions that a computer follows, one by one, from top to bottom.

Example in human language:

In Java, this looks like:

System.out.println("Let me add some numbers...");
int result = 5 + 3;
System.out.println("5 + 3 = " + result);

Don't worry about understanding every word yet. The key idea is: each line is one instruction, and Java runs them in order, top to bottom.

3️⃣ Your First Java Instruction: System.out.println()

What does System.out.println() do?

System.out.println() tells Java: "Show this on the screen."

It's the most basic way to make Java display something to you.

System.out.println("Hello, World!");

Let's break this down piece by piece:

println() prints text and moves to the next line.

print() prints text but stays on the same line.

System.out.print("Hello ");
System.out.print("World");
// Output: Hello World  (all on one line)
public class Main {
    public static void main(String[] args) {
        // println prints text and then moves to a new line.
        System.out.println("Hello, World!");
        System.out.println("Welcome to Java!");

        // print stays on the SAME line (no newline at the end).
        System.out.print("Hello ");
        System.out.print("World");
        System.out.println("!");

        // You can print numbers too — no quotes needed.
        System.out.println(42);
        System.out.println(3.14);

        // Joining text and a number with + (String concatenation).
        System.out.println("5 + 3 = " + (5 + 3));
    }
}

4️⃣ Quotes and Strings — VERY IMPORTANT

This is one of the most important things to understand early:

Java must know what is text and what is code.

Text MUST be wrapped in double quotes. Without quotes, Java gets confused.

System.out.println("Hello");   // ✅ Correct

This does NOT work:

System.out.println(Hello);     // ❌ Error!

Why does this fail?

Without quotes, Java thinks Hello is a variable name (a container for data). But no variable called Hello exists yet — so Java throws an error:

error: cannot find symbol
    symbol: variable Hello

Text = must be in double quotes "like this"

Single quotes 'A' are for single characters only (we'll learn about char later).

In programming, text is called a "String":

"Hello"              // This is a String
"I love Java!"       // This is a String
"12345"              // This is also a String (not a number!)
""                   // This is an empty String

Important: "123" is a String (text), but 123 (no quotes) is a number. They are completely different things in Java.

5️⃣ Semicolons — Every Statement Needs One

In Java, every instruction (called a statement) must end with a semicolon ;

Think of it like a period at the end of a sentence. Without it, Java doesn't know where one instruction ends and the next begins.

System.out.println("Line 1");   // ✅ Good
System.out.println("Line 2");   // ✅ Good
System.out.println("Line 3")    // ❌ Missing semicolon!

What happens without a semicolon?

The compiler gives you an error like:

error: ';' expected

This is the #1 most common error for beginners. If you see this error, just add the missing ; at the end of the line.

Class declarations and method declarations do NOT end with semicolons — only statements inside them do.

public class MyApp {              // No semicolon here
    public static void main(String[] args) {  // No semicolon here
        System.out.println("Hi");  // Semicolon here ✅
    }
}

6️⃣ Curly Braces — Code Blocks

Curly braces { } group code together into blocks. Every opening brace { must have a matching closing brace }.

public class HelloWorld {          // Opens the class
    public static void main(String[] args) {  // Opens the method
        System.out.println("Hi");  // Inside both blocks
    }                              // Closes the method
}                                  // Closes the class

💡 Analogy: Think of curly braces like nesting boxes. The class is the outer box, the method is the inner box, and your instructions go inside the innermost box. Every box that opens must close.

7️⃣ Comments — Notes for Humans

Comments are notes you leave in your code. Java completely ignores them — they exist only for you and other developers to read.

// This is a single-line comment
System.out.println("Hello");  // You can also put them after code

/* This is a multi-line comment.
   It can span as many lines
   as you need. */

/** This is a Javadoc comment.
 *  Used to generate documentation.
 *  You'll use these when you're more advanced.
 */

When should you use comments?

// print hello
System.out.println("Hello");

We can already see it prints hello!

// Greet user before showing menu
System.out.println("Hello");

Explains the purpose, not the code.

public class Profile {
    public static void main(String[] args) {
        // Print some facts about yourself.
        System.out.println("My name is Alice");   // change to your name
        System.out.println("I am 25 years old");  // change to your age
        System.out.println("I'm learning Java!");

        System.out.println(); // an empty line for spacing

        // Variables store values you can reuse.
        int age = 25;
        int daysOld = age * 365;
        System.out.println("I am approximately " + daysOld + " days old!");

        // String concatenation — joining text together.
        String firstName = "Alice";
        String lastName = "Smith";
        System.out.println("Full name: " + firstName + " " + lastName);

        // You can mix text and numbers with +.
        System.out.println("Next year I will be " + (age + 1));
    }
}

8️⃣ How Java Actually Runs: Compile → Run

Java is different from languages like Python or JavaScript. Java needs two steps to run:

HelloWorld.java  →  javac  →  HelloWorld.class  →  JVM  →  Output
   (your code)     (compile)    (bytecode)        (run)   (screen)

💡 Analogy: Think of it like cooking a recipe. You write the recipe (code), then a translator converts it into a universal language (bytecode), and any kitchen in the world (JVM) can follow it. That's "Write Once, Run Anywhere."

9️⃣ When Things Go Wrong: Reading Error Messages

Errors are completely normal. Even experienced developers get errors constantly. The key is learning to read them.

Error 1: Missing semicolon

System.out.println("Hello")   // forgot the ;
HelloWorld.java:3: error: ';' expected
        System.out.println("Hello")
                                   ^

Java tells you: the file, the line number (3), and exactly where the problem is (^). Just add the ;!

Error 2: Wrong class name

// File is called HelloWorld.java but code says:
public class Helloworld {   // lowercase 'w' — doesn't match!
error: class Helloworld is public, should be declared
in a file named Helloworld.java

Fix: make sure the class name matches the filename exactly (including uppercase/lowercase).

Error 3: Wrong capitalization

system.out.println("Hello");   // lowercase 's'!
error: package system does not exist

Fix: it's System with a capital S. Java is case-sensitive!

🧠 How to read any Java error:

public class Profile {
    public static void main(String[] args) {
        System.out.println("===== MY PROFILE =====");

        // Basic information.
        String first = "Alice";
        String last = "Smith";
        int age = 25;

        System.out.println("Name: " + first + " " + last);
        System.out.println("Age: " + age);
        System.out.println("City: London");
        System.out.println("Favourite Language: Java");

        // A simple calculation.
        int birthYear = 2024 - age;
        System.out.println("Birth Year: " + birthYear);

        long hoursLived = (long) age * 365 * 24;
        System.out.println("Hours Lived (approx): " + hoursLived);

        // Escape characters: \t is a tab, \n is a newline,
        // and \" prints a literal double quote.
        System.out.println("Tab:\tIndented text");
        System.out.println("Line 1\nLine 2");
        System.out.println("She said \"Java is awesome!\"");
    }
}
// 🎯 YOUR TURN — replace each ___ using the hint beside it.

// 1) The file's class. javac writes Main.class from it.
public ___ Main {                              // 👉 replace ___ with class

    // 2) Every Java program starts in this exact method signature.
    public static void ___(String[] args) {    // 👉 replace ___ with main

        // 3) Print a line and move to the next one.
        System.out.___("Hello, Java!");        // 👉 replace ___ with println

        // 4) This one does NOT move to the next line, so the next print joins on.
        System.out.___("No newline here. ");   // 👉 replace ___ with print
        System.out.println("So this joins on.");

        String course = "Java";
        int lessonNumber = 1;

        // 5) + joins text to a number by turning the number into text.
        System.out.println("Lesson " ___ lessonNumber + " of " + course);  // 👉 replace ___ with +

        // A comment never reaches the output.
        System.out.println("Ready to compile with javac and run with java.");
    }
}

Common Beginner Mistakes

💡 Pro Tips for Beginners

📋 Quick Reference

ConceptSyntaxNotes
Classpublic class Name { }Must match filename
Main methodpublic static void main(String[] args)Entry point — JVM starts here
Print with newlineSystem.out.println("text")Most common output method
Print without newlineSystem.out.print("text")Stays on same line
Statement ending;Required after every statement
Single-line comment// commentIgnored by Java
Multi-line comment/* comment */For longer notes
Compilejavac FileName.javaCreates .class bytecode
Runjava FileNameRuns on JVM (no extension!)

🎉 Lesson Complete!

Amazing work! You've learned how to set up Java, write your first program, use System.out.println(), add comments, and read error messages. You understand the compile → run process that makes Java special.

Practice quiz

What does System.out.println() do?

  • Reads input from the user
  • Prints text and stays on the same line
  • Prints text and moves to a new line
  • Deletes a file

Answer: Prints text and moves to a new line. println prints its argument and then moves to the next line; print stays on the same line.

Every Java statement must end with which character?

  • A semicolon ;
  • A period .
  • A comma ,
  • A colon :

Answer: A semicolon ;. Every statement in Java ends with a semicolon — it is the #1 beginner error to forget it.

If your public class is named HelloWorld, what must the file be named?

  • helloworld.java
  • hello.java
  • Main.java
  • HelloWorld.java

Answer: HelloWorld.java. The filename must match the public class name exactly, including capitalization: HelloWorld.java.

Which command compiles a Java source file into bytecode?

  • java HelloWorld
  • javac HelloWorld.java
  • run HelloWorld
  • compile HelloWorld

Answer: javac HelloWorld.java. javac compiles HelloWorld.java into HelloWorld.class (bytecode).

How do you RUN a compiled Java program?

  • java HelloWorld
  • java HelloWorld.class
  • java HelloWorld.java
  • javac HelloWorld

Answer: java HelloWorld. You run it with 'java HelloWorld' — just the class name, no .java or .class extension.

Why does System.out.println(Hello); cause an error (no quotes)?

  • Hello is too long
  • println cannot print text
  • Java thinks Hello is a variable that does not exist
  • It needs a semicolon

Answer: Java thinks Hello is a variable that does not exist. Without quotes Java treats Hello as a variable name; since none exists it reports 'cannot find symbol'.

What is printed by: System.out.println("5 + 3 = " + (5 + 3));

  • 5 + 3 = 53
  • 5 + 3 = 8
  • 8
  • 53

Answer: 5 + 3 = 8. The parentheses force the addition first, giving 8, so it prints '5 + 3 = 8'.

What turns your .java source into a portable .class file the JVM can run?

  • The text editor
  • The operating system
  • The println method
  • The compiler (javac), producing bytecode

Answer: The compiler (javac), producing bytecode. javac compiles source into bytecode (a .class file), which the JVM then runs — 'write once, run anywhere'.

Which is a valid single-line comment in Java?

  • # a comment
  • // a comment
  • <!-- a comment -->
  • ** a comment

Answer: // a comment. // starts a single-line comment; /* */ is for multi-line comments.

Why does 'system.out.println' (lowercase s) fail?

  • It needs more arguments
  • Comments are not allowed
  • Java is case-sensitive — it must be System
  • It is missing a semicolon

Answer: Java is case-sensitive — it must be System. Java is case-sensitive, so it must be System with a capital S; lowercase gives 'package system does not exist'.

Continue this course