1.1   Your First Java Program:   Hello World

In this section, our plan is to lead you into the world of Java programming by taking you through the three basic steps required to get a simple program running. As with any application, you need to be sure that Java is properly installed on your computer. You also need an editor and a terminal application. Here are system specific instructions for three popular home operating systems. [ Mac OS X · Windows · Linux ]

Programming in Java.

We break the process of programming in Java into three steps:
  1. Create the program by typing it into a text editor and saving it to a file named, say, MyProgram.java.
  2. Compile it by typing "javac MyProgram.java" in the terminal window.
  3. Execute (or run) it by typing "java MyProgram" in the terminal window.

The first step creates the program; the second translates it into a language more suitable for machine execution (and puts the result in a file named MyProgram.class); the third actually runs the program.


Most errors are easily fixed by carefully examining the program as we create it, in just the same way as we fix spelling and grammatical errors when we type an e-mail message. One of the very first skills that you will learn is to identify errors; one of the next will be to be sufficiently careful when coding to avoid many of them.

Input and output.

Typically, we want to provide input to our programs: data that they can process to produce a result. The simplest way to provide input data is illustrated in UseArgument.java. Whenever this program is executed, it reads the command-line argument that you type after the program name and prints it back out to the terminal as part of the message.
% javac UseArgument.java
% java UseArgument Alice
Hi, Alice. How are you?
% java UseArgument Bob
Hi, Bob. How are you?


  1. Write a program TenHelloWorlds.java that prints "Hello, World" ten times.
  2. Modify UseArgument.java to make a program UseThree.java that takes three names and prints out a proper sentence with the names in the reverse of the order given, so that for example, "java UseThree Alice Bob Carol" gives "Hi Carol, Bob, and Alice.".

Web Exercises

  1. Write a program HelloWorldMultilingual.java that prints "Hello World!" in as many human languages as you can.
  2. Write a program Initials.java that prints your initials using nine rows of asterisks like the one below.
    **        ***    **********      **             *             **
    **      ***      **        **     **           ***           **
    **    ***        **         **     **         ** **         **
    **  ***          **          **     **       **   **       **
    *****            **          **      **     **     **     **
    **  ***          **          **       **   **       **   **
    **    ***        **         **         ** **         ** **
    **      ***      **        **           ***           ***
    **        ***    **********              *             *

  3. Describe what happens if, in HelloWorld.java, you omit
    1. main
    2. String
    3. HelloWorld
    4. System.out
    5. println
  4. Describe what happens if, in HelloWorld.java, you omit
    1. the ;
    2. the first "
    3. the second "
    4. the first {
    5. the second {
    6. the first }
    7. the second }
  5. Describe what happens if, in HelloWorld.java, you misspell (by, say, omitting the second letter)
    1. main
    2. String
    3. HelloWorld
    4. System.out
    5. println
  6. I typed in the following program. It compiles fine, but when I execute it, I get the error java.lang.NoSuchMethodError: main. What am I doing wrong?
    public class Hello {
       public static void main() {
          System.out.println("Doesn't execute");   
    Answer: you forgot the String[] args. It is required.