Your First Java Program
A Hello World program is a trivial program that does nothing but print a message, typically 'Hello World!'.
Why Start With Hello World
Learning a new programming language usually starts with a small, complete program that does one visible thing. The program most often used for this purpose is called Hello World. A Hello World program is a trivial program that does nothing but print a message, typically 'Hello World!'. Its job is deliberately modest, and nothing more is asked of it.
The value of such a small program is that it lets you check the whole chain of steps at once. You write text in a file, run that file through the language's tools, and see a result appear. If the result is the expected message, you know the basic workflow is working. The interesting question is what actually happens between writing the file and seeing the message. That is what this article traces.
Opening HelloWorld.java
The source for this article is HelloWorld.java, the Java version of the Hello World program. The file name tells you two things. The .java extension identifies the file as Java source code, and the name HelloWorld describes what the program is for. The code below is an illustrative version written for this article. Read it once for its overall shape before looking at the individual lines.
Hello World!- public class HelloWorld: declares a class named HelloWorld, which must match the file name
HelloWorld.java - public static void
main(String[] args): the method where Java begins running the program System.out.println("Hello World!"): the single action that prints the message to the console
Notice what the program does not do. It does not ask for any input, perform any calculation, or make any decision. Its entire behavior is one printed line. The printed text is the only visible output, and it is exactly the text the source describes: Hello World!
Tracing the Path From File to Output
When you run a Hello World program, several stages happen in order. First, the program source is handled by the language's tools. Then the program starts, prints its one message, and stops. Because the program has only one action, the flow is a straight line with no branches and no loops. Each step happens exactly once, in the order shown. This is what makes the program a good first trace: there are no changing values to track and no paths to choose between.
Tracing one run of HelloWorld
Follow the program from the moment it starts to the moment it ends, listing each step exactly once.
- 1Start
The launcher loads the compiled HelloWorld program and begins at the main method.
- 2Run
Execution reaches the single statement
System.out.println("Hello World!"). - 3Print
The text Hello World! is sent to the console, followed by a line break.
- 4End
main has no more statements, so the program stops. Nothing else happens.
The trace is a straight line: start, run, print, end. No value changes along the way, so the only observable change is the printed message.
Predicting the Printed Text
Before reading the answer, try to predict what the program prints. Look only at the single print action in the code and decide what text will appear on the console. Think about two things: the words inside the quotation marks, and whether anything else is added to the output.
What do you think happens?
Given the line System.out.println("Hello World!"); what exact text appears on the console?
Reveal answer
Hello World!
println prints the text inside the quotation marks, without the quotation marks themselves, and then moves the cursor to a new line. The program has no input, so nothing else is needed to produce this output.
print Versus println
The HelloWorld example uses println, and the difference from print shows up on the screen. A print call writes its text and leaves the cursor where the text ended. A println call writes its text and then moves the cursor to the start of the next line. Seeing both side by side makes the difference concrete.
Hello World!
Hello
World!- The first two calls share one line because print does not add a line break
- The last two calls each end their own line because println adds a line break
Mistakes Beginners Make With the First Program
Common mistake Expecting the quotation marks to appear in the output
What it looks likeSystem.out.println("Hello World!"); printing "Hello World!" with quotesWhy it failsThe quotation marks are syntax that marks where the text begins and ends. They are not part of the text.
Do this insteadExpect the output Hello World! with no surrounding quotes.
Common mistake Assuming a file name can be anything when the class has a fixed name
What it looks likeSaving the code as Program.java while the class is named HelloWorld
Why it failsThe class name and the file name must agree in the HelloWorld example, so the tools can find the program.
Do this insteadSave the file as HelloWorld.java to match the class HelloWorld.
Common mistake Thinking a missing message means the program ran and printed nothing
Why it failsCompilation happens before running, so a problem in the source can stop the program before any output appears.
Do this insteadCheck whether the compile step succeeded before looking for the printed line.
Common mistake Confusing print with println and expecting the next output to start on a new line when it does not
Practice Tracing a Program
Without running it, predict the exact console output of this program: a main method containing System.out.println("Learning Java"); followed by System.out.println("Step by step");. Write down each line you expect to see, then check your answer by trace.
Hints
- Each println call produces its own line, so the two messages should appear on separate lines.
- The quotation marks are not printed. Only the text inside them appears.
- The statements run in order from top to bottom, one time each.
Key Takeaways
- A Hello World program is a trivial program whose only job is to print a message, typically 'Hello World!', and programmers traditionally write one first to check the whole workflow.
- In
HelloWorld.java, the class contains a main method, and the single print action nested inside main produces the visible output. - Running a Hello World program is a straight-line sequence: the source is handled by the language's tools, the program starts, prints its one message, and stops.
- println prints its text and then moves the cursor to a new line, while print leaves the cursor where the text ended.
- To predict output, read only the text inside the quotation marks in the print statement, and expect nothing else to be added.