Hello World

Goals

Concepts

Preview

package com.example;

public class HelloWorld
{
  public static void main(String[] args)
  {
    System.out.println("Hello, World!");
  }
}

Preparation

  1. Download and install the latest version of the Java Development Kit (JDK). This course is based on Java 11.
    • Make sure the download says “JDK” and not “JRE”.
    • Unpack the archive contents into the directory of your choice. 7-Zip is recommended.
    • Don't forget to set the JAVA_HOME environment variable to indicate the root directory where you unpacked the download file.
    • You will want to add the Java …/bin subdirectory to your system path.
  2. Install a good text editor that supports the UTF-8 encoding.
    Visual Studio Code (recommended)
    Modern, extensible, and supports other user interface languages in addition to English.
    Atom
    Modern and extensible, but can be buggy and unstable.
    Notepad++
    Has localizations for other user interface languages.

Lesson

The Java programming language became available in 1995. It was primarily created by James Gosling at Sun Microsystems, which was later sold to Oracle Corporation. Nowadays Oracle provides a distribution of Java that requires a license for commercial use, but the open-source version of Java used in this course, the OpenJDK, can be used without restrictions.

Java is object-oriented and runs on the Java Virtual Machine (JVM). The Java compiler javac takes source code, stored in .java files, and compiles them into object code, stored in .class files. Java prefers to call its object code bytecode.

Java compile workflow
Compiling Java source code to bytecode and deploying it on different platforms.

For Java to run on a platform there must therefore exist a JVM implementation for that CPU. Usually the JVM will function together with libraries and tools, collectively known as the Java Runtime Environment (JRE).

Project

It is best to create a separate directory for each project you will working on. For now, this project root directory will contain a simple subdirectory named src for storing the source code. Create a directory structure named helloworld/src.

Creating a helloworld project directory on Linux.
mkdir helloworld
cd helloworld
mkdir src
cd src
Creating a helloworld project directory on Windows.
md helloworld
cd helloworld
md src
cd src

Source Code

Packages of Classes

Generally speaking, each .java file contains code for a Java class, which is a bundle of program logic. The Java class will indicate a package, which is a way to group related classes. You need to choose a package name that is unique.

In the src directory, place the source code in a series of subdirectories that match the package structure, considering each component of the package to be a directory. For example a package of com.example would result in the directory structure com/example. The example below assumes you have changed to the src directory, continuing the steps above.

Creating com/example directories inside src on Linux.
cd src
mkdir com/example
Creating com/example directories inside src on Windows.
cd src
md com\example

Now using your editor, create the source code file inside the helloworld/src/com/example directory. The .java source code file should have the same name as the class.

Source code for helloworld/src/com/example/HelloWorld.java.
/*
 * Copyright © 2015-2018 GlobalMentor, Inc. (http://www.globalmentor.com/)
 */

package com.example;

/**
 * "Hello world" sample Java program.
 *
 * <p>This application prints a greeting to the standard output.</p>
 *
 * @author Garret Wilson
 *
 * @see <a href="https://en.wikipedia.org/wiki/%22Hello,_World!%22_program">"Hello, World!" program</a>
 */
public class HelloWorld
{

  /**
   * Main program entry point; called by the Java Runtime Engine (JRE).
   * @param args The array of arguments passed on the command line.
   */
  public static void main(String[] args)
  {
    System.out.println("Hello, World!"); //this is the only line to execute
    //TODO make the program do something more interesting
  }

}

Methods

Java is based on an imperative programming paradigm: each line in the source code is an instruction telling Java to perform some action. The JVM will read the codes in the .class file representing the instructions you originally placed in your source code, and execute the commands, one at a time. You could modify the above program to print more information by adding more lines containing the command System.out.println(…).

Adding more instructions to the HelloWorld.java program.
…
public static void main(String[] args)
{
  System.out.println("Welcome!");  //welcome the students
  System.out.println("Good luck in the course.");
  System.out.println("Don't hesitate to ask the teacher if you have any questions.");
  System.out.println("Welcome!");  //welcome the students again
  System.out.println("Good luck in the course.");
}
…

The JVM would then execute each of these five instructions, one after the other. You'll note that the first two instructions are exactly the same as the last two instructions. Java doesn't care how many whether instructions are duplicated—it will execute them as provided. But from the perspective of program design, duplication of code or data makes the program larger and harder to maintain.

Java additionally allows for procedural programming, allowing you to group together multiple instructions into a “procedure” to prevent repeating the individual instructions yourself in the code. Java calls these procedures methods, and they will appear outside the main(…) section. The simplest method begins with the words static void and is followed by parentheses (), as you saw above.

In order to reduce the number of repeated instructions in the above code, you can place the instructions inside a method named printWelcome. In each of the original locations of the duplicated code, you “call” the method using the method name followed by parentheses: printWelcome(). A method therefore acts as a single instruction representing several instructions. Don't forget to add Javadoc documentation to your new method!

Adding a method to the HelloWorld.java program.
…
public static void main(String[] args)
{
  printWelcome();
  System.out.println("Don't hesitate to ask the teacher if you have any questions.");
  printWelcome();
}

/**
 * Prints a welcome to the students in the course.
 */
static void printWelcome()
{
  System.out.println("Welcome!");  //welcome the students
  System.out.println("Good luck in the course.");
}

Compiling the Source Code

From the source code root directory src, use javac to compile the source code. It will produce a .class file containing bytecode in the same directory as the source code.

Compiling source code on Linux or Windows.
cd helloworld/src
javac com/example/HelloWorld.java -encoding UTF-8

Running the Bytecode

From the source code root directory src, use java to run the compiled class, making sure to indicate the entire package name. Don't indicate the name of the .class file; Java will find it automatically based upon the package and class name.

Running bytecode on Linux or Windows.
cd helloworld/src
java com.example.HelloWorld

Generating Documentation

The javadoc tool will examine all your source code for some package(s) and generate HTML documentation for you automatically, based upon the Javadoc tags you have included. This documentation can be distributed to other developers to quickly and easily communicate how to interact with your program. Correct and thorough Javadoc documentation is therefore very important within the code.

From the project root directory, create a separate directory such as apidocs for the documentation. Then from the source code root directory src, use javadoc to create documentation for all classes in the com.example package. With this form of the Javadoc comment, don't specify the individual filenames, just the name of the package to be included in the documentation.

Generating Javadoc documentation on Linux.
mkdir apidocs
cd src
javadoc -d ../apidocs com.example -encoding UTF-8
Generating Javadoc documentation on Windows.
md apidocs
cd src
javadoc -d ..\apidocs com.example -encoding UTF-8

The documentation will be generated and placed in the helloworld/apidocs directory. You can view it by loading helloworld/apidocs/index.html in a web browser.

Review

Gotchas

In the Real World

Think About It

Self Evaluation

Task

Create your own “Hello World” program from scratch. Type it all in manually; don't copy and paste.

Put your entire project directory and contained files into an archive such as .zip or .7z and send it to your teacher. The 7-Zip application, indicated in the Resources, uses an especially efficient, open-source .7z file format and is recommended for homework submission.

See Also

References

Resources