Java Notes
Create a small Java program that prints a message on the screen. You will save the code, compile it, and run it step by step.
Before you start
Java needs to be installed before a program can run on your computer. Open Command Prompt or PowerShell and check that both java --version and javac --version show a version number. If either command does not work, complete the JDK, JRE, and JVM lesson first, then come back here.
Write the program
Create a file called HelloWorld.java. Then type this code exactly. You can also run it directly in the live editor below.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
When you run it, Java prints the words Hello, World!. Now change the text inside the quotes to your own message and run it again. The text in quotes is called a String, and System.out.println displays that text on the screen.
What each line means
| Code | Simple meaning |
|---|---|
public class HelloWorld | Start a container named HelloWorld for this program. |
main | This is where Java starts reading your program. |
System.out.println(...) | Show something on the screen, then move to the next line. |
"Hello, World!" | The message you want to show. |
; | Marks the end of an instruction. |
Do not worry about memorising every word in public static void main(String[] args) yet. Copy it exactly in every basic Java program. You will learn each part later.
Run it from a terminal
Open a terminal in the folder that contains HelloWorld.java.
- Type
javac HelloWorld.javaand press Enter. This checks your code and createsHelloWorld.class. - Type
java HelloWorldand press Enter. This starts your program.
The first command needs the file name including .java. The second command needs the class name only. Do not write java HelloWorld.class.
Print more than one line
Most programs do not show only one message. Put each message in its own System.out.println(...) statement when you want the next message to begin on a fresh line.
For example, a profile has three separate pieces of information: name, city, and goal. Writing three println statements makes the output easy for a person to read.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Read the code from top to bottom. Java prints the first message, moves the cursor down one line, prints the second message, moves down again, and prints the third message. Change the three sentences to your own details, run the code, and see each answer appear on its own line.
The class is named AboutMe, so if you save it on your computer, the file name must be AboutMe.java.
print and println are different
Both commands show text on the screen. The only difference is what Java does after showing the text:
System.out.print(...)shows the text and keeps the cursor on the same line.System.out.println(...)shows the text and then moves the cursor to the next line.
Think of println as “print line”: it prints the message and finishes that line. This is useful for lists, headings, and separate answers. Use print when two or more pieces must join together on one line.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
The first command prints Java but does not finish the line. The next command immediately adds is fun. after it, then finishes the line. The last println therefore starts below the first sentence. Run the code, then replace the first print with println and run it again—this one small change shows the difference clearly.
You will commonly use println while learning because it keeps output readable. Use print later when you intentionally want values together, for example Name: followed by a name typed by a user.
Two mistakes beginners often make
Errors are normal while learning. Read the first error message, compare your code with the example, fix one problem, and run the program again. These two mistakes happen most often in a first Java file.
1. File name and class name do not match
In this code, the public class is named HelloWorld:
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Because the class is public, the file must be saved as HelloWorld.java—the spelling and capital letters must match exactly. Saving this same code as Hello.java makes Java stop and report that HelloWorld should be declared in HelloWorld.java.
You can fix it in either of two ways:
- Rename the file to
HelloWorld.java; or - Change
HelloWorldin the code toHello, then save the file asHello.java.
For a beginner, the easiest habit is: first choose the class name, then use that exact name for the file.
2. Capital and small letters are different in Java
Java treats capital and small letters as different characters. String is correct, but string is another name that Java does not know. The same rule applies to System and system.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Copy String and System exactly as shown. If Java says cannot find symbol, check spelling and capital letters before changing anything else. String, System, Main, and main are not interchangeable names.
Try it yourself
You have printed fixed messages. Now make a small program using your own information. This practice helps you use the class name, main method, and System.out.println without copying the earlier example word for word.
Your task
Create a class named Profile. Inside main, print these three details on separate lines:
- Your name.
- Your city.
- One reason you want to learn Java.
If you save the program on your computer, name the file Profile.java. If you use the live editor, replace the example text with your own details and press Run.
Hint
Start with the same structure as HelloWorld. Keep public class Profile and the main line unchanged. Add three System.out.println("..."); lines inside the curly brackets. The text you want to print goes inside the double quotes.
Check your work
Try it yourself first. Then compare your structure with this runnable example:
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Your words can be different; the important part is that the program has the Profile class and three separate println statements. Change one line, run it again, and confirm that only that line changes in the result.
Continue the lesson path