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 LearnJava, TutorialsPoint and MeritCampus.

LearnJava is an Java instructional website. It features short topics and has code and result windows that allow you see the code being discussed in the topic and then run that code and see the result. You can also modify the code in the code window and run it to see the result of your changes. Because it is a learning system (like the one you are in now), it may prompt you to move to its next topic. Just cancel these prompts as we will guide you to each topic or tell you when you should move to the next topic in LearnJava. LearnJava topics open in a new tab, so you can close the tab when  you are done with the topic or leave it open if you wish.

TutorialsPoint is another Java instructional website. It also features Java topics and in some topics, 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.

MeritCampus is another instructional website and while they charge for some of thier courses, they have a free Java introductory course which we will reference. Again, don't go beyond the topic we link to unless directed by this guide.

We will use videos from the Programming Video Tutorials website as well as topics from the TutorialsPoint website and the official Oracle Java tutorial and documentation.

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 descripton 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 souce 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 Compile button followed by the Execute button. The Compile button runs the source code (HelloWorld.java) through the Java compiler javac (as you can see in the console window). After compiling you can see the HelloWorld.class file produced by the compiler. The .class file is contains the bytecode the JVM will execute. The Execute button runs the JVM and passes the HelloWorld.class file to be executed and the output appears in the console window. Note that the new version of CodingGround only has an Execute button, which does both the compile and execute steps. 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, that doesn’t mean they did not happen.


Lesson navigation: