First Program in Java

Learn development in Java

Java is a pure object oriented programming language. It means in java every program has to be written inside class/interface block.

In our first program we will print some message on terminal. For printing message on terminal we will use print method of java.
Print method in java requires inclusion of java.lang library.

In java import keyword is used to include library. So first statement will be

Including libraries :

// This library is included in every program by default
import java.lang.*; 

Important Java compiler includes java.lang library in every java program by default so there is no need to import it.

Next we need to write a main method. Java is pure object oriented language means everything in java is strictly written in class/interface.

 class one 
    // here we include main method. 

In java main method has one argument of string array type.

 class one 
    public static void main(String arg[]) 
	   // here we use print method to print msg on screen 

Important public static void main(String ... arg) can also be used. Tripple dots used as variable arguments.

Why public and static keyword with main method ?

Java compiler(javac) is stored in "C:\Program Files\Java\Jdk1.8\bin" directory. Our program might be stored anywhere so public keyword is used so that javac[java compiler] can call main() method. A method with public keyword can be called from anywhere.

Warning Program having main() method without public keyword will be compiled but can't be executed.

Static keyword allows calling of main method without creating object of class. As we know in order to call method of a class we need to create a object and using that object methods of class can be called. Another way is to declare method as static. Static method can be called by using class name, That's why static keyword is used so that main method can be executed without creating object.

For printing in java we use

// message will be printed on terminal

Remember !
  • System is a class in java available in java.lang library.
  • out is a object of PrintWriter class.
  • println is the name of method.

Hello world program :

 // file name : 
 class one 
    public static void main(String arg[]) 
	   System.out.println("Hello World");

Important Java is a case sensitive language. It means if a letter is capital write it as capital otherwise there will be error during compilation. In above program first letter of String and System are capital.

Executing first program

For execution you need to install jdk on our machine. Refer Java Installation for installing jdk.

Instructions for writing program in java first time

Step 1 Open your notepad

Open run utility and type notepad.

	Start --> Run --> notepad

Step 2 Write program in notepad

Step 3 Save program as

Save your file on Desktop with name[file can be saved anywhere but for demonstration purpose we have saved on desktop]. Remember to change Save as Type to All Files otherwise notepad will save your file as text file.

Step 4Open your command interpreter

	Start --> Run --> cmd

Step 5 Change your directory

Now change directory to desktop because our file is on desktop.

Step 6 Compile your program

Java program execution involve two phase first is compilation and second is interpretation. Java compiler name is javac. Using javac compile your first program.


Java compiler convert java's program into a assembly like program. This newly created file is known as Byte code. This file has ".class" extension.

Step 7 Execute your program

Java program is executed with help of interpreter.