Introduction to Java
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.
Learn Introduction to Java in our free Java course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Java course at LearnCodingFast — hands-on lessons with examples you run in your browser, 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:
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:
Open any text editor (Notepad, VS Code, or IntelliJ). Create a new file and name it exactly:
⚠️ The filename must match the class name exactly (including capitalization). This is a Java rule — not optional.
Save it somewhere you can find it easily (like your Desktop).
- Windows: Search "cmd" in Start menu → open Command Prompt
- macOS: Open Finder → Applications → Utilities → Terminal
- Linux: Press Ctrl+Alt+T or search "Terminal"
Use the cd command to go to where you saved your file:
Unlike Python, Java needs to be compiled first . This turns your code into something the computer can run:
This creates a file called HelloWorld.class — that's the compiled version.
Notice: no .java or .class extension when running — just the class name.
If you see this — congratulations! You just compiled and ran your first Java program.
1️⃣ What Is Java? (Plain English)
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:
- Your code runs on any computer — Windows, Mac, Linux, Android
- Errors are caught before your program runs (not during)
- Large teams can work on the same codebase without chaos
Java is used for:
- Android apps (Instagram, Spotify were built with Java)
- Banking and financial systems (nearly every bank uses Java)
- Web servers and APIs (Netflix, Amazon, LinkedIn)
- Big data processing (Hadoop, Apache Spark)
- Enterprise software and government systems
- Game development (Minecraft was written in Java!)
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.
- Show a message on the screen
- Add two numbers together
- Display the 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()
System.out.println() tells Java: "Show this on the screen."
It's the most basic way to make Java display something to you.
- System → a built-in Java class that gives you access to system functions
- out → the output stream (where text goes — usually your screen)
- println → "print line" — displays text and moves to the next line
- ("Hello, World!") → the text you want to display (must be in quotes!)
- ; → the semicolon at the end — every statement in Java must end with one
println() prints text and moves to the next line.
print() prints text but stays on the same line .
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.
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:
Single quotes 'A' are for single characters only (we'll learn about char later).
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.
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.
6️⃣ Curly Braces — Code Blocks
Curly braces { } group code together into blocks . Every opening brace { must have a matching closing brace } .
💡 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.
- To explain why you did something (not what the code does)
- To leave notes for yourself or teammates
- To temporarily disable code while testing
8️⃣ How Java Actually Runs: Compile → Run
Java is different from languages like Python or JavaScript. Java needs two steps to run:
💡 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.
Java tells you: the file, the line number (3), and exactly where the problem is (^). Just add the ; !
Fix: make sure the class name matches the filename exactly (including uppercase/lowercase).
Fix: it's System with a capital S. Java is case-sensitive!
- Look at the line number — that's where the problem is
- Read the error message — Java usually tells you exactly what's wrong
- Look at the ^ arrow — it points to the exact character
- Fix that one thing, then compile again
Common Beginner Mistakes
- ❌ Filename doesn't match class name: If your class is HelloWorld , the file MUST be HelloWorld.java — not helloworld.java or hello.java
- ❌ Missing semicolons: Every statement must end with ; — this is the #1 beginner error
- ❌ Wrong capitalization: system.out.println won't work — it must be System (capital S)
- ❌ Using single quotes for strings: 'Hello' is wrong for strings. Use "Hello" . Single quotes are only for single characters like 'A'
- ❌ Forgetting the main method: Without public static void main(String[] args) , Java doesn't know where to start
- ❌ Mismatched braces: Every { must have a matching } . Count them if you get errors!
💡 Pro Tips for Beginners
- 💡 Use an IDE: Download IntelliJ IDEA (Community Edition is free) or VS Code with the Java extension. They highlight errors as you type, saving you hours of debugging.
- 💡 Type, don't copy-paste: When learning, type the code yourself. Your fingers will memorize the patterns faster than your eyes.
- 💡 Read errors from the top: When you get multiple errors, fix the FIRST one and compile again. Often, fixing one error removes several others.
- 💡 Save often, compile often: Don't write 50 lines then compile. Write 3-4 lines, compile, fix errors, repeat. Small steps = fewer bugs.
📋 Quick Reference
Concept
Syntax
Notes
Class
public class Name { }
Must match filename
Main method
public static void main(String[] args)
Entry point — JVM starts here
Print with newline
System.out.println("text")
Most common output method
Print without newline
System.out.print("text")
Stays on same line
Statement ending
;
Required after every statement
Single-line comment
// comment
Ignored by Java
Multi-line comment
/* comment */
For longer notes
Compile
javac FileName.java
Creates .class bytecode
Run
java FileName
Runs 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.
Next up: Variables & Data Types — learn how to store numbers, text, and other data so your programs can remember and work with information.
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
- Next: Next Lesson