Java Hello World

Java is an Object Oriented Programming (OOP) Language. Objects in Java are called "classes". Now we are going to write an example program, which will print "Hello World.".

public class Main {
public static void main(String[] args) {
System.out.println("Hello World.");
}
}
[N.B.: Our recommendation is use Eclipse IDE for software development. If you are interested to make Android apps, this IDE is strongly recommended.]

>> Now we are going to explain

1    public class Main {
2        public static void main(String[] args) {
3            System.out.println("Hello World.");
4        }
5    }


In the first line we define a class called Main.

public class Main {

In Java Programming Language, every line of code that can really need to run inside a class. This line declare a class named Main, which is public. That means any other class can access it. Do not worry about it. Now, we just written our code in a class called Main and we will discuss later about Object. Notice it, when we declare a public class we must declare it inside a file with same name (Main.java). Otherwise we will get an error when we will go to compile it.
 
1    public class Main {
2        public static void main(String[] args) {
3            System.out.println("Hello World.");
4        }
5    }

In second line:

public static void main(String[] args) {


This is the entry point of our Java program. the main method has to have this exact signature in order to be able to run our program.

public means anyone can access it.
static means you can run this method without creating an instance of Main.
void means this method does not return any value.
main means name of the method.

The arguments we get inside a method. The arguments we get, when we run the program with parameters. It is an array of strings. We will use it step by step, So do not worry about it. Everything will be clear one by one during this course.

System.out.println("Hello World.");

System is a per-defined class that Java provides us and it holds some useful methods and variables.
out is a static variable within System that represents the output of your program.
println is a method of out that can be used to print a line.

Now time to run your program.

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello World.");
    }
}

You will get result:

Hello World.

>> More Example on Hello World.
Try all yourself to get more experience on this Hello World program.

Example ~ 01:

public class MyFirstJavaProgram {

    public static void main(String []args) {
       System.out.println("Hello World");
    }
}

Example ~ 02:

class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!"); // Display the string.
    }
}

Example ~ 03:

class HelloWorld {
    public static void main(String args[])
    {
        System.out.println("Hello world!");
    }
}

Example ~ 04:

class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!"); // Prints the string to the console.
    }
}

Example ~ 05:

class First {
  public static void main(String[] arguments) {
    System.out.println("Let's do something using Java technology.");
  }
}

Example ~ 06:

class YourName {
    public static void main(String[] args) {
        System.out.println("Type your name here.");
    }
}

Example ~ 07:

import java.io.*;
class MyFirstProgram {
  /** Print a hello message */
  public static void main(String[] args) {
    BufferedReader in =
        new BufferedReader(new InputStreamReader(System.in));
    String name = "Instructor";
    System.out.print("Give your name: ");
    try {name = in.readLine();}
        catch(Exception e) {
           System.out.println("Caught an exception!");
        }
    System.out.println("Hello " + name + "!");
  }
}

<<  < Previous Lesson < Java Tutorial > Next Lesson >  >>

 Regularly visit this site to get new information about Java Programming Language. Today, it is a robust and most powerful programming language in the Universe.