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.
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!
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).
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.
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 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.
A program is simply a list of instructions that a computer follows, one by one, from top to bottom.
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.
System.out.println() tells Java: "Show this on the screen."
It's the most basic way to make Java display something to you.
println() prints text and moves to the next line.
print() prints text but stays on the same line .
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.
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.
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.
Comments are notes you leave in your code. Java completely ignores them — they exist only for you and other developers to read.
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."
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!
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'.