Your first Java Program

There are two items you will need to write your first Java program. These are:

  1. OpenJDK Development Kit (JDK)
  2. A text editor – vim, nano, featherpad, gedit etc.

If you haven’t yet installed OpenJDK Development Kit (JDK), then you may look how to install one here.

Next, you need to open your text editor and type the following code –

//Your first Java code
class HelloWorld {
                  public static void main (String args [])  {
                      System.out.println("Hello World!");
          }
}

Save as HelloWorld.java, the file has .java extension and file name is same as the name of our class. Although you could choose filename of your choice but, it is always better to have one that resembles name of your class. It is worth mentioning here that Java is case-sensitive.

We will discuss the code after we have compiled and run it.

To compile your Java code, you need to use javac. It will generate a Java bytecode file, HelloWorld.class. A Java bytecode file contains all the instruction which are to be executed by the Java Virtual Machine (JVM). So, to compile the code –

javac HelloWorld.java

The compiler generates a bytecode file. To run the program –

java HelloWorld

It will return with the output-

Hello World!

Till now, we have learnt how to write, compile and run our Java code. Next, we will be discussing the code we just wrote in detail.

Code Explained
//Your first Java code
class HelloWorld {
         public static void main (String args [])  {
                 System.out.println("Hello World!");
         }
}

The code starts with //, this is used for comments. Java supports three types of comments – single-line comment, multi-line comment and documentation comment. We will discuss each of those in a separate article.

Next is – class HelloWorld {

class is a keyword which is used to declare a new class HelloWorld. The class is defined in between curly braces {}.

Hence, the class can be declared and defined as –

class class_name {
         //Your code and data are defined here
}

After class declaration, we have –

public static void main (String args [])  {

main() is a method, it is necessary to have the main() method in Java programs as it is considered a starting point. However, there are many programs that may not require main() method at all.

public keyword is an access modifier, it signifies that the class member so declared inside the class can also be accessed from outside the class too.

static keyword allows us to call our main() method without having to create an object. If we remove static keyword then, we need to create an object first to call main() method.

void keyword is used when our method doesn’t return any value. In this case, we inform our Java compiler that our main() method doesn’t return any value.

String args[], we have declared a parameter named – args, an array of type String.

System.out.println(“Hello World!”); System is a class, out is for the standard output. println() is a built-in method. Anything passed to the println() method would be displayed as the output. Then, at last there is a semi-colon.

In conclusion, we have learnt how to write our first Java program.