Java Hello world: Your first program

Writing the first program in Java

Until now, we learned what is the Java programming. After setting up the environment (JDK) to execute the Java code, it is time to write our first program of Java i.e. the “hello world” program.

It is up to you that which IDE you like to write the code for Java programs. For our example, we are using the Eclipse IDE to write the “hello world” program.

Watch Video of this tutorial

So without explaining any other thing, this is the “hello world” example code in Java. The example will display “java Hello World” as the output.

Hello world example code

As you run this code in the eclipse, the output will be: “Hello World”

Exploring code of hello word Java program

In the above “hello world” example, a few things should be noted and learned:

The first line:

public class hello_world {

Java programs are the combination of one or more classes. Any code to be executed in Java is written in the class. This line defines a class in the program along with its scope. The class name is given as our example “hello_world” while it is given the public scope, that means any other class may access it.

The name of this public class must be the same as the Java file name, otherwise, it will generate an error.

For our example, the file name of Java program is “hello_world.java”.

The next line:

   public static void main(String []args) {

This is the main method containing our code for “hello world” to be displayed. Where:

  • public means this method can be accessed anywhere
  • static = generally you need to create the instance. Static means you can execute code in this method without creating an instance.
  • void means this method will not return any value.
  • main is the method name.

Let us move to the next line to display “hello world”.

System.out.println(“Hello World”);

This line prints “Hello World” to the screen.

Java provides pre-defined or built-in class System while println is the method in System class to print the given string, (both are explained in their respective chapters. )