Java Hello World Program
Every programmer's first program prints a short greeting. It sounds too simple to matter, but it proves that your whole setup works: the editor, the compiler and the JVM. If "Hello, World!" appears on your screen, everything after this is just learning the language.
Write the Program
Create a folder for your Java practice, for example java-practice in your Documents folder. Open any plain text editor (Notepad, TextEdit in plain-text mode, or VS Code) and type this program exactly as shown. Don't copy it with the formatting of a web page or word processor.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}Hello, World!Save it in your folder as HelloWorld.java. The file name must match the class name on line 1 exactly, including the capital H and W.
Compile and Run It
Open a terminal in your practice folder. Then run two commands: javac compiles, java runs.
cd %USERPROFILE%\Documents\java-practice
javac HelloWorld.java
java HelloWorldcd ~/Documents/java-practice
javac HelloWorld.java
java HelloWorldjavac HelloWorld.javareads your file, checks it, and writesHelloWorld.classnext to it. If it prints nothing, that's good news: no errors.java HelloWorldstarts the JVM, loadsHelloWorld.classand runs it. Notice there's no.classat the end of this command. You give it the class name, not the file name.
The terminal prints Hello, World!. You've just written, compiled and run a Java program.
What Each Line Means
You don't need to understand every word on day one. Here is the short version; later lessons go deeper.
| Line | Code | Meaning for now |
|---|---|---|
| 1 | public class HelloWorld { | Creates a class named HelloWorld. In Java, all code lives inside a class. |
| 2 | public static void main(String[] args) { | The main method: the point where your program starts. The lesson "The Main Method in Java" explains every word. |
| 3 | System.out.println("Hello, World!"); | Prints the text in quotes, then moves to a new line. |
| 4 | } | Ends the main method. |
| 5 | } | Ends the class. |
Two small rules you'll use in every program:
- Every statement ends with a semicolon
;. - Curly braces
{ }come in pairs. Every{needs a matching}.
Print More Than One Line
println prints and then moves to the next line. print prints and stays on the same line. Here's a small ticket printed with both.
public class TicketPrint {
public static void main(String[] args) {
System.out.println("Chennai Express");
System.out.println("Coach S4, Seat 32");
System.out.print("Fare: ");
System.out.print("Rs. 450");
System.out.println();
System.out.println("Have a safe journey!");
}
}Chennai Express
Coach S4, Seat 32
Fare: Rs. 450
Have a safe journey!How it works
- Lines 3 and 4 use
println, so each one ends with a new line. - Lines 5 and 6 use
print, soFare:andRs. 450end up on the same line. - Line 7, an empty
println(), just finishes that line. - Line 8 prints the last line.
Try it yourself: change line 5 from print to println. Before you run it, predict where Rs. 450 will appear.
Common Mistakes
Saving the file with the wrong name. A public class HelloWorld must be in HelloWorld.java. Saving it as helloworld.java or Hello.java gives:
Hello.java:1: error: class HelloWorld is public, should be declared in a file named HelloWorld.java
public class HelloWorld {
^
1 errorAdding .class when you run it. java HelloWorld.class doesn't work; Java looks for a class literally named HelloWorld.class:
Error: Could not find or load main class HelloWorld.class
Caused by: java.lang.ClassNotFoundException: HelloWorld.classForgetting the semicolon or writing system with a small s. Java is case-sensitive: System and system are different words. The lesson "Common Beginner Compile Errors" shows how to read and fix these messages.
Practice
- Easy: Change the program to print your name and your city on two separate lines.
- Harder: Print this box using only
println, with the edges lined up exactly:
+------------+
| Hello Java |
+------------+Show a hint
Use three println lines, one for each line of the box. Count the characters: each line is 14 characters wide.
Key Takeaways
Was this tutorial helpful?