Your first Java program
20 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)Showing steps for Windows. They are marked “On Windows” below.
A small Java program is a class with a main method and a few statements. Write one, save it as Hello.java and run it, in your IDE or in the terminal.
The parts of a Java program
To make the computer print a message, you write a complete program. Every Java program has three parts: a class, a main method inside it, and statements inside main.
Example · Hello.java
public class Hello { public static void main(String[] args) { System.out.println("Hello, World!"); System.out.println("I am learning Java."); }}Hello, World! I am learning Java.
What this code does
public class Hello { … }is the class: the block that holds your code. Its curly brackets{ }mark where it starts and ends.public static void main(String[] args)is the main method: the program starts running here.- Each
System.out.println(...)is a statement: it prints one line and ends with a semicolon;. - The statements run from top to bottom, so the two lines print in that order.
Save the file with the right name
A public top-level class (one not inside another class) named Hello must be saved in a file named Hello.java, with the same capital letters: hello.java is a different name.
In an IDE, create a class named Hello and it saves Hello.java for you. Without one, type the program in Notepad or TextEdit and save it as Hello.java in a practice folder (next section).
Hello.java.txt. On a Mac, TextEdit must be in plain text mode (Format → Make Plain Text).Open a terminal in your practice folder
Skip this if you only use your IDE. To use the terminal, keep your files in one simple folder and open the terminal there. Do this once; later just open the folder again.
On Windows
Use a folder like C:\java-practice. Your Desktop may be inside OneDrive, so cd Desktop often fails; a simple folder avoids that.
- Open Command Prompt. Type
mkdir C:\java-practiceand press Enter (only the first time). - Save
Hello.javaintoC:\java-practice. - Type
cd C:\java-practiceand press Enter. - Type
dirand press Enter. You should seeHello.javain the list.
cmd and press Enter. Command Prompt opens in that folder.Run it: IDE or terminal
In the terminal, type these two commands in your practice folder. They are the two steps from What is Java? (opens in a new tab): compile, then run.
javac Hello.javajava HelloHello, World! I am learning Java.
In your IDE (from the previous lesson), just click Run: the IDE does both steps at once and shows the output at the bottom.
javac Hello.java– the compiler checks your code and makesHello.class(bytecode).java Hello– the JVM runs your program. Type the class name only: neverjava Hello.class.
java Hello.java. This course uses javac and then java Hello, so you see both steps.Common beginner mistakes
Everyone gets compile errors at first. The compiler shows the file, the line number and what is wrong:
Hello.java:3: error: ';' expected System.out.println("Hello, World!") ^1 errorHere line 3 has no ; at the end. Fix that line and compile again. These are the usual causes:
- Missing semicolon – every statement, like
System.out.println("Hi");, ends with;. - Wrong file name – a
public class Hellomust be saved inHello.java. - Wrong capital letters – write
System, notsystem. Java sees them as different words. - Missing quotes or brackets – text needs double quotes, and every
{needs a}.
If something else goes wrong, use the Common errors → fixes list in the Summary tab. Check from the top: first the JDK, then the folder and file name, then the code. Next, the programs start to store values: Variables (opens in a new tab).
java -version works, javac -version works, and your program prints Hello, World!. If all three are true, your Java setup is complete.What's next
You finished the preview of Getting Started
Java Programming has 26 sections, 252 modules and 1949 lessons. An account opens every lesson and saves your progress.
Next step
Check your understanding
6 quick questions