The first program we will make in java/eclipse, give the output, or as you should say in the programming language: prints, the words “Hello World”. This is traditionally used to introduce new programmers to a programming language.

1. To start, we use the example model. For this “program”, we will name the package: HelloWorld. And the class also: HelloWorld, because this is the main (and only) class in the package.

package HelloWorld;

class HelloWorld {

	HelloWorld() {
	}

	void start(){
	}
	
	public static void main(String[] args) {
		new HelloWorld().start();
	}
}

2. The program is the print the words “Hello World”. We will do this in the method “void start”. Java has a default output stream, which puts everything to the console. We could print something like the following:

system.out.println("Hello World")

This is allowed in the Java coding language, but when you are writing bigger programs it is easier and shorter to only use: out.printf(” .. “).

To use this option we have to adjust some things. Adjustments:

  1. We have to import PrintStream, this is just a built-in class in Java.
import java.io.PrintStream;

2. We have to create a class variable; this is a function what is available for the whole class.

class HelloWorld {
	PrintStream out;

	HelloWorld() {
		PrintStream out = new PrintStream(System.out);
	}

We have now changed the default word “System” to the word “out”.

All together:

Hello World Code
package HelloWorld;

import java.io.PrintStream;

class HelloWorld {
	PrintStream out;

	HelloWorld() {
		PrintStream out = new PrintStream(System.out);
	}

	void start() {
		out.printf("Hello World");
	}

	public static void main(String[] args) {
		new HelloWorld().start();
	}
}

For a more advanced version of Hello World click here.