Lesson: Hello World!

Explore the classic Hello World! first Java program example.

Understand the most basic Java program and its component parts.


The most common example of a first Java program is the famous Hello World! program. Before we get into the details of Hello World, I am going to introduce some of the supplemental resources we will be using.

TutorialsPoint is a programming instructional website and we will reference some of it's Java topics. In some topics there is a code window where you can execute examples of code discussed in the topic. They also have a Java coding website called CodingGround where you can write, compile and execute example code or your own code. We will use CodingGround to demonstrate topics in our lessons. In those cases, the code from our lesson will be loaded into the CodingGround code window and you can compile and execute that code right in your browser.

We will also link to videos on YouTube that give a different view of topics and show examples. 

We will refer to the official Oracle Java tutorial and documentation often and other Java resource sites when appropriate.

Now let's do some Java! Every programmer’s favorite program is one that will print “Hello World!” on the screen of the device you’re working on. That will be our first task.

In Java, everything is an object, also called a class, and all code must be contained in a class. A class is the description of an object. The terms object and class, while used interchangeably, are not quite the same thing. We will get into the details of objects and classes later. For now, just know that every Java program requires at least one object. This object's name is the same as name of your program, for example: HelloWorld. We define this object by creating the class HelloWorld and storing it in a file called HelloWorld.java. Our source code is always stored in a file with the .java extension and that file must have the same name as the class it contains.

The HelloWorld class must have a method (also called a function) called main. The main method allows the JVM to know where to start your program. We will discuss methods in more detail later. So the simplest Java program looks like this:

In this example the key thing to see is the statement that prints Hello World! on the system console. System.out is a built-in Java API and println( ) is a method in that API that prints text on the display.

Jump over (finish reading this paragraph first) to the Hello World example in CodingGround. You can run the example by clicking the Execute button. The Execute button runs the source code (HelloWorld.java) through the Java compiler javac (as you can see in the console window). Compiling produces the HelloWorld.class file. The .class file contains the bytecode the JVM will execute. The Execute button then runs the JVM and passes the name of the .class file to be executed and the output appears in the console window. Before you go, notice that the example main method is not quite correct. You can edit the code in the code window to correct the problem and create the result we want. After editing you will need to click compile to generate a new .class file with your changes.

A very important concept to keep in mind regardless of what code you are writing is that many steps in the programs that we write will not have visible results. In this example, the only thing that we end up actually seeing on the screen are the words we have put in the quotation marks inside the System.out.println( ) function. Simply looking at the output, we would not have been able to know exactly the steps that went into the original program. This is very important when trying to fix problems with a program that is not behaving as expected. When we get more complex programs, in order to even know which part of our program is executing we need to make sure to have some visual result (printing something to a screen, moving a motor or servo in a particular way) that will help us identify what step in the program is running. Don’t forget: steps in code can run without having any visible result, but that does not mean they did not happen.