Lesson: Hello World!

Overview: 
Explore the classic Hello World! first Java program example.
Objectives: 

Understand the most basic Java program and its component parts.

Content: 

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 use videos from the Programming Video Tutorials (PVT) website. Note that this site was abandoned after this curriculum was finished and now the PVT site may say it is down. Just refresh and the page should appear. Once the page appears, the video will not play but the text discussing the same topic is still quite good. We are in the process of replacing PVT with YouTube videos.

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 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, but that does not mean they did not happen.

 

Navigation: