Coffee Space


Listen:

Java Hello World

Below is an incredibly simple example of code that can be written in Java. In the example, "Hello World" is printed 5 times with the number of the print proceeding the String.

Code

To use the code, save the following code block to a file called HelloWorld.java. To compile, run javac HelloWorld.java and then java HelloWorld to run.

/**
 * HelloWorld.java
 *
 * This class is a simple demonstration for the basics of Java.
 *
 * @author B[]Array
 **/
public class HelloWorld{
  /**
   * main()
   *
   * Program entry point responsible for creating an instance the
   * HelloWorld class.
   *
   * @param args The command line arguments to the program.
   **/
  public static void main(String[] args){
    // Instantiate and store an instance of HelloWorld
    HelloWorld hw = new HelloWorld(5);
    // Program self terminates here
  }
  
  /**
   * HelloWorld()
   *
   * Constructor for class, responsible for running all code in instance.
   *
   * @param count The number of lines "Hello World" lines to be printed.
   **/
  public HelloWorld(int count){
    // Itterate over the number of prints we wish to do
    for(int i = 0; i < count; i++){
      // Print line with line number
      System.out.println(i + " Hello World");
    }
  }
}

Summary

As you can see, Java is a really quick and easy program to get started with!