Java for Beginners · Module 1: Getting Started
🎯 Why Setup Matters
A chef needs a kitchen before cooking. A Java programmer needs the JDK (Java Development Kit) before writing a single program. The JDK contains the compiler (javac) that turns your code into bytecode and the launcher (java) that runs it. Setup takes about ten minutes, and you only do it once.
1️⃣ Get Java Ready in Four Steps
a JDK installer for your OS
run the installer, accept defaults
so the terminal finds javac and java
java -version and javac -version
Figure 1: The four steps to get Java ready on your computer.
Step 1: Download a JDK
Choose a recent LTS version (17, 21 or 25). Two good free sources are Oracle’s JDK download page and Adoptium (Eclipse Temurin), which is an open-source OpenJDK build. Pick the installer for your operating system.
Step 2: Install
- Windows: run the downloaded installer (.msi or .exe) and accept the defaults. Some installers have an option such as “Set JAVA_HOME” and “Add to PATH”. Turn it on.
- macOS: run the installer package, or install with a package manager such as Homebrew.
- Linux: use your package manager, for example
sudo apt install openjdk-21-jdkon Ubuntu or Debian.
Step 3: Make sure the PATH is set
The PATH is a list of folders your terminal searches when you type a command. If the JDK’s bin folder is not on it, the terminal says “javac is not recognized”. Installers that offer the PATH option do this for you. If yours did not, add the JDK’s bin folder to PATH in your system’s environment settings.
Step 4: Verify
Open a new terminal (Command Prompt, PowerShell or Terminal) and type these two commands. Each should print a version number. The exact number depends on what you installed.
java -version
javac -version
2️⃣ Your Working Folder
Create a folder just for your programs, for example JavaPrograms, and open your terminal inside it. Keeping files together makes compile and run commands short.
3️⃣ Compile and Run: The Real Process
Java has two commands where C had one. First you compile, then you run.
Welcome.javayou write this
javac Welcome.java (compile)Welcome.classbytecode made by javac
java Welcome (run, no .class)Figure 2: Compile with javac, then run with java.
Program 1: Hello, World! Save this as HelloWorld.java.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Hello, World!
In the terminal you would type:
javac HelloWorld.java
java HelloWorld
javac HelloWorld.javacompiles. If there are no errors, it prints nothing and createsHelloWorld.class.java HelloWorldruns the program. Write the class name only, without.javaor.class.- The file name must match the public class name exactly, including capital letters.
| Folder: JavaPrograms | Before javac | After javac |
| Welcome.java | ✔ present | ✔ present |
| Welcome.class | ✘ not yet | ✔ created by javac |
Figure 3: javac creates a .class file next to your source file.
Program 2: Reading words typed after the command
public class Welcome {
public static void main(String[] args) {
System.out.println("Number of arguments: " + args.length);
if (args.length > 0) {
System.out.println("Welcome, " + args[0] + "!");
} else {
System.out.println("Welcome, stranger!");
}
}
}
Number of arguments: 0
Welcome, stranger!
Number of arguments: 1
Welcome, Ajay!
argsholds the words you type after the class name. This is the same idea as command-line arguments in C.args.lengthsays how many words were typed. With none, the program takes theelsebranch.args[0]is the first word. Counting starts at 0.
java HelloWorld.java. It compiles in memory and runs. Still learn the two-step way, because bigger projects use it.4️⃣ Choosing a Tool to Write Code
You can write Java in any text editor, but a code editor or IDE (Integrated Development Environment) highlights mistakes and saves time.
| Tool | Type | Good for |
| Notepad + terminal | Plain editor | Learning how compile and run really work |
| VS Code | Light editor | Beginners; add the Extension Pack for Java |
| IntelliJ IDEA Community | Full IDE | Bigger projects; smart suggestions |
| Eclipse | Full IDE | Long used in colleges and companies |
| NetBeans | Full IDE | Simple and beginner friendly |
Figure 4: Common tools for writing Java.
Our advice: write your first five programs with a simple editor and the terminal so you understand javac and java. Then move to an IDE.
5️⃣ The Three Errors Every Beginner Sees
Error 1: A missing semicolon
public class Hello {
public static void main(String[] args) {
System.out.println("Hello")
}
}
Hello.java:3: error: ‘;’ expected
System.out.println(“Hello”)
^
1 error
- Read the line number (3) and the arrow. The compiler points just after the place where the semicolon should be.
Error 2: File name does not match the class name (this code is saved in a file named Hello.java)
public class Hi {
public static void main(String[] args) {
System.out.println("Hello");
}
}
Hello.java:1: error: class Hi is public, should be declared in a file named Hi.java
public class Hi {
^
1 error
- A public class must live in a file with the same name. Rename the file or the class so they match.
Error 3: Wrong capital letter
public class Hello {
public static void main(String[] args) {
system.out.println("Hello");
}
}
Hello.java:3: error: package system does not exist
system.out.println(“Hello”);
^
1 error
- Java is case-sensitive.
Systemmust start with a capital S.
java Hello when no Hello.class exists prints Error: Could not find or load main class Hello. Check that you compiled first and that you are in the right folder.⚠️ Common Setup Problems
| Problem | Likely cause | Fix |
|---|---|---|
| “javac is not recognized” | JDK’s bin folder is not on PATH, or the terminal was open during install | Fix PATH, then open a new terminal |
java -version works but javac does not | Only a JRE is installed | Install the full JDK |
| “Could not find or load main class” | Wrong folder, not compiled, or .class typed in the command | Compile first; run java ClassName |
| Two different versions show up | Several JDKs installed | Put the one you want first on PATH |
Editor saved Hello.java.txt | Windows hides known extensions | Turn on “File name extensions” in File Explorer |
❓ Frequently Asked Questions
Q1. Which Java version should I install?
Any recent LTS: 17, 21 or 25. All examples in this series work on them.
Q2. Do I need an IDE?
No. A text editor and a terminal are enough, and teach you the basics.
Q3. What is JAVA_HOME?
An environment variable that points to the JDK folder. Some tools need it. Many installers set it for you.
Q4. Can I install more than one JDK?
Yes, but the first one on PATH is the one the terminal uses.
✅ Key Takeaways
- Install the JDK, not just the JRE, and choose an LTS version.
- Make sure the JDK’s
binfolder is on the PATH, then open a new terminal. - Check the setup with
java -versionandjavac -version. - Compile with
javac File.java. Run withjava ClassName. - The file name must match the public class name, and Java is case-sensitive.
- Read compiler messages: the line number and the arrow show where to look.
🎓 Practice questions: (1) What is the difference betweenjavacandjava? (2) Which file doesjavac Welcome.javacreate? (3) Why do we writejava Welcomeand notjava Welcome.class? (4) Install the JDK and runjava -version. What version do you see? (5) Create the three errors from this lesson on purpose and read each message.
All 30 lessons in this series (open course page)
Module 1 · Getting Started
- Lesson 1: What is Java? Features and Uses
- Lesson 2: Installing the JDK and Running Your First Program
- Lesson 3: JDK, JRE, JVM and WORA Explained
- Lesson 4: JVM Architecture and JIT Compilation
- Lesson 5: Hello World Program Explained Line by Line
Module 2 · Building Blocks
- Lesson 6: Comments, Identifiers and Keywords
- Lesson 7: Variables and Literals
- Lesson 8: Primitive Data Types in Java
- Lesson 9: Type Conversion and Casting
- Lesson 10: Taking Input with the Scanner Class
- Lesson 11: Escape Characters and Variable Scope in Java
Module 3 · Operators
- Lesson 12: Arithmetic Operators and Increment/Decrement in Java
- Lesson 13: Relational and Logical Operators in Java
- Lesson 14: Bitwise and Shift Operators in Java
- Lesson 15: Assignment, Ternary Operator and Operator Precedence in Java
Module 4 · Control Flow
- Lesson 16: if, if-else and else-if Ladder in Java
- Lesson 17: switch Statement in Java (Classic and Modern)
- Lesson 18: while and do-while Loops in Java
- Lesson 19: for Loop and Nested Loops (Patterns) in Java
- Lesson 20: break, continue and Labelled Loops in Java
Module 5 · Data and Methods
- Lesson 21: Arrays in Java (One-Dimensional)
- Lesson 22: Multidimensional Arrays in Java
- Lesson 23: Strings and the String Pool in Java
- Lesson 24: String Methods and StringBuilder in Java
- Lesson 25: Methods and Method Overloading in Java
- Lesson 26: Recursion Basics in Java
- Lesson 27: ArrayList and Wrapper Classes in Java
- Lesson 28: Exception Handling Basics in Java