Hello World (Java)
From LiteratePrograms
- Other implementations: Ada | ALGOL 68 | Alice ML | Amiga E | Applescript | AspectJ | Assembly Intel x86 Linux | Assembly Intel x86 NetBSD | AWK | bash | BASIC | Batch files | C | C, Cairo | C, Xlib | Candle | Clojure | C++ | C# | Delphi | Dylan | E | Eiffel | Erlang | Forth | FORTRAN | Fortress | Go | Groovy | Haskell | Hume | IBM PC bootstrap | Inform 7 | Java | Java, Swing | JavaScript | LaTeX | Lisp | Logo | Lua | Maple | MATLAB | Mercury | OCaml/F Sharp | occam | Oz | Pascal | Perl | PHP | Pic | PIR | PLI | PostScript | Prolog | Python | Rexx | Ruby | Scala | Scheme | Seed7 | sh | Smalltalk | SQL | Standard ML | SVG | Tcl | Tcl Tk | Visual Basic | Visual Basic .NET | XSL
In any programming language, a program to print "hello world" is a classic opener. It is also a useful indication of the complexity of the language, particularly from the point of view of the teacher and the learner. Any "hello world" program is going to be
<<hello world>>= print greeting
In this article we explore some of the complexities involved in implementing the classic "hello world" program in Java.
Contents |
Hello World in Java
In Java, implementing this is suprisingly complex. This is primarily because the programmer is not only responsible for writing the code to execute print greeting, but also for defining an initial context in which code to print the greeting can be declared, and initialising the runtime context in which the code can be executed.
Classes and Objects
Java is an object-oriented language. The key structuring concept is the class, and in Java all code must be contained in a class. Thus the body of "Hello World" must be defined within the scope of a class definition like this:
<<HelloWorld.java>>= public class HelloWorld { class body }
To use an object of the HelloWorld class, we must declare a reference to the type of the class, instantiate an object of that type using the new keyword, and assign the reference to the newly created object to the declared reference.
<<instantiate object of type HelloWorld>>= HelloWorld hw = new HelloWorld();
Methods
We want our object to print a greeting, so let us postulate that the object will be used like this:
<<print greeting>>= String message = hw.getMessage(); System.out.println(message);
Now System.out.println is a standard method provided by the Java run-time system. It prints a string to the console. The method hw.getMessage() is a method of our new class HelloWorld which returns a reference to a String. Here is its definition:
<<define getMessage method>>= public String getMessage() { return "Hello World!"; }
The Main Method
The hard part, which, because of the many subtleties that have to be covered before it can be fully understood, has to be taken on trust by starting programmers:
<<main method>>= public static void main(String[] args) { instantiate object of type HelloWorld print greeting }
Notice that he main entry point must be marked as both public and static (otherwise the Java Virtual Machine cannot invoke it).
The HelloWorld Program
Putting this all together, the simplest Java program that illustrates the essential features of the language is:
<<class body>>= define getMessage method main method
Compiling and Running the Program
To compile the java program you need to use the compiler. The standard compiler that is provided by the Java Standard Edition (JavaSE) Software Development Kit (SDK) is called javac.
$ javac HelloWorld.java
This will generate a class file HelloWorld.class containing byte code for the Java Virtual Machine (JVM). To execute the program, the class file has to be loaded into a JVM and executed. The execution starts at the main method. The Java SE SDK provides the java command which provides a run-time environment in which to launch a JVM, load the HelloWorld.class file and any needed library class files, and find and execute the main method.
$ java HelloWorld Hello World! $
Note the java command takes only the name of the compiled class file as an argument, not the name + extension. This is different from the javac command that always expects .java extension. This is a subtle point worth noting as it is one that trips up many newcomers to the Java system. (There is a reason for this convention that is related to Java's namespace mechanism, but it's a detail best left out of this introduction.)
Modifications
Using public static void main
Of course, you could argue that I've made this exposition more complex than it needs to be, and you'd be right. However, the standard form, which simply uses a class has a holder for the main method:
<<ClassicHelloWorld.java>>= public class ClassicHelloWorld { public static void main(String[] args) { System.out.println("Hello World"); } }
arguably obscures more than it reveals about the language.
Using a plain old java object
The main benefit of writing Hello World in Java would seem to be as a check that your Java development platform is working (see the The Official Java Tutorial) but as the Sun's discussion of HelloWorld illustrates, an awful lot of this is plumbing. For teaching, an Objects First approach which eliminates the main method, is clearer and truer to the style of Java the language.
<<ObjectsFirstHelloWorld.java>>= public class ObjectsFirstHelloWorld { define getMessage method }
Unfortunately (or perhaps fortunately) this style can only be shown to students using a programming environment like BlueJ or DrJava. In such environments, the code to instantiate an object of type ObjectsFirstHelloWorld and printing the greeting can be executed directly.
Download code |