First Java Program

We’re gonna write, compile and execute our first Java program. It’s a simple ‘hello world’ program. The longstanding tradition in the programming world is that the first program in any programming language you learn is to print the text ‘hello world’ on the screen. We’re gonna stick to it.

I hope you already set up the Java Development Kit in your system. If not, here’s the article on how to do it.

Once you set up the JDK, what you need to have is a text editor to type in the program. You can use Notepad on Windows, and the default text editor on Linux for that. (The default text editor in Linux varies according to the distribution you’re using. Most probably it is Kate or GEdit).

Here’s the program:

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

Type in the program on the text editor.

Save it with the filename (Filename has to match with the name of the class. In this case it’s A)

Open the command window for the particular folder in which you have saved the file. (An easy way to open command window like this is to go to the required folder on Windows Explorer. Right click on the window by pressing the shift key. Select the option, open command window here.

enter the command javac

If it shows an error that the command javac is not found, your path is not yet set. Set the java bin folder’s path to the system variable.

If it doesn’t show any warning, the program is compiled successfully.

Now enter the command java A

Voila! We just wrote our first Java program. If everything goes right the text ‘Hello World’ will be displayed on the screen.

Now let’s dissect the program and see what each line means.

Line 1:  class A { Class is a java keyword. That means it is a word in Java that has a predefined meaning. It is the definition of a class. The class name is given as A. The opening curly brace indicates the starting of a block of code. Here the entire class is enclosed in curly braces. Read my post on Object-Oriented Programming to get an idea of classes.

Line 2:  public static void main (String[] args) { It is the definition of the main method. For now, just keep in mind that these terms are required as such in the programs we write. Each of the words has a meaning, we’ll come back to it later. It’s hard to explain now. The main method is where the execution of the program starts. When the program is run, the JVM looks for the main method in the program and starts execution from there. A program can have more than one method. No matter however many methods a program has, the execution always starts from the main method. If you don’t know what a method is, don’t worry, we’ll see it later in detail. Now just understand that methods are named units of codes for carrying out a task.

Line 3: System.out.println(“Hello World”); This is the only statement in our program. It just displays on the screen what is given in quotes. For instance, if the statement is System.out.println(“First Program”); The text ‘First Program’ will be displayed on the screen as such. Note that the line ends with a semicolon (;). Every statement in Java ends with a semicolon. There will be a newline character followed by the text. That means, each command prints character on a new line. If the command print was used instead of println, The text will get printed on screen without the newline. I’m leaving it to you as an exercise. Use the commands System.out.println(); and System.out.print(); to print text on the screen.

Line 4: The closing brace closes the main method.

Line 5: The closing brace closes the class.

For all the programs we write in the beginning, the structure of our program will be like.

 class ClassName {
 public static void main(String args[]){

Our statements will be contained inside this structure.


 class className {
 public static void main(String args[]){




We call it boilerplate codes. Each word is very important and has a particular meaning. Now it will be hard to comprehend. So we just use it as such in all our programs.

Let’s review what we learned.

  • Each program should be made of at least a class.
  • The execution of the program starts with the main method
  • In Java, blocks of codes are enclosed in braces. The class definition and main method definitions are enclosed in braces.
  •  System.out.println(); displays on screen what is given in quotes. If println is given, it will make a newline. If the statement System.out.print() is used, it will print text without a new line.
  • Each opened curly brace must be closed.

If you don’t understand something, or you’re experiencing any difficulty in doing it, just let me know by adding a comment.

Hi, I’m Shiju P John. A Computer Science Engineering student. I run this site as my hobby and passion.

Leave a Reply