Java – First Program

In this Java tutorial, we will learn how to write your first java program, Hello World in Java. After that, we will also discuss how to compile and run a simple java program with example.

First Program in Java (Hello World)

Let’s start with the first example in java. To write Java code, open your favourite text editor (notepad) and write below code.

class Hello
	public static void main(String arg[])
		System.out.println("Hello World");

After writing Java code, you need to save the file with .java extension. In my case, I am going to save this file as

How to Compile and Run Java Program

Now, it’s time to compile and run the java program. Java programs are written in English, but the computer understands only machine(binary) code. Compile java Program is a process to convert Java code into Byte code. Run a Java program means to execute the Byte code and process the output. You have to open the command or terminal window of your computer and move to your java source folder.

  • Compile Java – In this phase, java program compiles (translated) in Byte code and create a file with .class extension, known as a class file.
  • Run Java – In this phase Byte codes convert in machine language and execute the program.


javac    //compile java program
java className        //run java program


java Hello

Let’s Discuss the Java Code.

Let’s discuss each line of the above first java example step by step.

  1. class Hello – In Java, the program always begins with the declaring a class name. In this line, we are creating a class Hello.
  2. public static void main(String arg[])
    • main() is the first method or entry point of the program. Every Java application must have the main() method to start with.
    • public – Access specifier, a public member can call anywhere.
    • static – Access modifier, a common property for all the objects.
    • void – Return type, it means the main does not return any value.
    • String arg[] – main() method always accept array parameter of String type.
  3. System.out.println() – It is an output statement, it prints the result on the user screen.
  4. { } – You can notice opening and closing curly brackets in the program, it creates a block of code. Every time class and methods must be start and end with curly brackets.

Comments in Java

Proper use of comments in a program gives the more readability to the code, we can explain what is going on our program through commenting. Comments are ignored by the compiler.

Single Line Comment

We can make a single line as a comment by placing // at the beginning of the line.

// this is example of single line comment

Multiple Line Comment

We can create multiple lines as a comment by placing /* at the beginning and */ at the end of the comment.

example of 
multi-line comment
in Java