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.

0001 /**
0002  * HelloWorld.java
0003  *
0004  * This class is a simple demonstration for the basics of Java.
0005  *
0006  * @author B[]Array
0007  **/
0008 public class HelloWorld{
0009   /**
0010    * main()
0011    *
0012    * Program entry point responsible for creating an instance the
0013    * HelloWorld class.
0014    *
0015    * @param args The command line arguments to the program.
0016    **/
0017   public static void main(String[] args){
0018     // Instantiate and store an instance of HelloWorld
0019     HelloWorld hw = new HelloWorld(5);
0020     // Program self terminates here
0021   }
0022   
0023   /**
0024    * HelloWorld()
0025    *
0026    * Constructor for class, responsible for running all code in instance.
0027    *
0028    * @param count The number of lines "Hello World" lines to be printed.
0029    **/
0030   public HelloWorld(int count){
0031     // Itterate over the number of prints we wish to do
0032     for(int i = 0; i < count; i++){
0033       // Print line with line number
0034       System.out.println(i + " Hello World");
0035     }
0036   }
0037 }

Summary

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