Writing Java hello world example

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 what IDE you like to write the code of java. For our example, we are using the Eclipse IDE to write the “hello world” program.

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 java example code

As you run this code in the eclipse, the output will be: java 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 java “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 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. See the links below)

Also see System.out.println | Java main method

Was this article helpful?

Related Articles

Leave A Comment?