Concepts / Your First Java Program

Your First Java Program

A Hello World program is a trivial program that does nothing but print a message, typically 'Hello World!'.

  • Programming

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.

java
Output
Hello World!
Notes on the code
  • 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!

classdeclares the program'scontainerHelloWorldthe class name, matchingthe file namemain methodwhere execution beginsSystem.out.printlnthe single print action,nested inside mainHello World!the text that appears onthe consolenamed bycontainscontainsprints
What contains what in the HelloWorld.java file, and where does the line that prints the message sit?

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.

HelloWorld.javasource file saved as textjavac compilerthe language tool thathandles the sourceHelloWorld.classcompiled form the Javaruntime can executejava launcherstarts the program and runsmainHello World!the one printed line, thenthe program endsjavac HelloWorld.javaproducesjava HelloWorldprints and stops
What happens to the source file after I save it, and which tool turns it into something the computer can run?
Worked example

Tracing one run of HelloWorld

Follow the program from the moment it starts to the moment it ends, listing each step exactly once.

  1. 1
    Start

    The launcher loads the compiled HelloWorld program and begins at the main method.

  2. 2
    Run

    Execution reaches the single statement System.out.println("Hello World!").

  3. 3
    Print

    The text Hello World! is sent to the console, followed by a line break.

  4. 4
    End

    main has no more statements, so the program stops. Nothing else happens.

Result

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?

  • Hello World!

  • "Hello World!"

  • System.out.println("Hello World!")

  • Nothing, because the program has no input

Reveal answer
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.

print("Hello")text Hello, cursor stays onthe same lineprintln("Hello")text Hello, then the cursormoves downNext linethe cursor starts here forthe next outputchange to printlnline break added
How does the screen look after print("Hello") versus println("Hello") runs, and where does the cursor end up?
java
Output
Hello World!
Hello
World!
Notes on the code
  • 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 like
    System.out.println("Hello World!"); printing "Hello World!" with quotes
    Why it fails

    The quotation marks are syntax that marks where the text begins and ends. They are not part of the text.

    Do this instead

    Expect 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 like

    Saving the code as Program.java while the class is named HelloWorld

    Why it fails

    The class name and the file name must agree in the HelloWorld example, so the tools can find the program.

    Do this instead

    Save 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 fails

    Compilation happens before running, so a problem in the source can stop the program before any output appears.

    Do this instead

    Check 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

EASY

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.