Hello World

Every Java program starts off as a set of class definitions. Each class definition has an identifier that begins with a capital letter and each class resides in a file having the same name as the class and .java as the file extension.

For example, below is a definition for a class named Hello. This class resides in a file named Hello.java.

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

Every program must have exactly one class that contains a function named main with the following signature:

public static void main (String[] args) { 
    // function body 

When the program is executed, the computer begins executing the code within main(). When the program above is executed, the println() method is called which prints “Hello World!” to the screen.

Compiling Source Code

We can compile source code via the command prompt using the following command.

$ javac Hello.java

The compiler (named javac) will create a file named Hello.class which contains bytecode and can be executed with a Java Runtime Engine (JRE).

Executing Java Bytecode

To execute the bytecode in Hello.class from the command prompt we use the following command:

$ java Hello

Here the JRE (named java) that we do not include the file extension. Here, we only specify the name of the class that contains the main() method.

© 2017, Eric. All rights reserved.