Hello World: The Application

At least since the first edition of Kernighan and Ritchie’s The C Programming Language it’s been customary to begin programming tutorials and classes with the “Hello World” program, a program that prints the string “Hello World” to the display. Being heavily influenced by Kernighan and Ritchie and not ones to defy tradition we begin similarly.

The following is the Hello World Application as written in Java. Type it into a text file or copy it out of your web browser, and save it as a file named HelloWorld.java.

class HelloWorld {

  public static void main (String args[]) {

    System.out.println("Hello World!");

  }

}

To compile this program make sure you’re in the same directory HelloWorld.java is in and type javac HelloWorld.java at the command prompt. Hello World is very close to the simplest program imaginable. Although it doesn’t teach very much from a programming standpoint, it gives you a chance to learn the mechanics of writing and compiling code. If you’re like me your first effort won’t compile, especially if you typed it in from scratch rather than copying and pasting. Here are a few common mistakes:

  • Did you put a semicolon after System.out.println("Hello World")?
  • Did you include the closing bracket?
  • Did you type everything exactly as it appears here? In particular did you use the same capitalization? Java is case sensitive. class is not the same as Class for example.
  • Were you in the same directory as HelloWorld.java when you typed javac HelloWorld.java?Once your program has compiled successfully, the compiler places the executable output in a file called HelloWorld.class in the same directory as the source code file. You can then run the program by typing java HelloWorld at the command prompt. As you probably guessed the program responds by printing Hello World! on your screen.

    Congratulations! You’ve just written your first Java program!

  • Comments are closed.