First Java Program

In this tutorial, you will understand the basic structure of the java program by writing your first java program and will walk through the different ways to compile and run your java program.

The basic syntax of a java program

Let&rsquo's begin with the Hello World program which has been traditionally illustrated as the basic program. If you run this program it just displays a message Hello, World!!! on your screen. The main objective of this program is to give learners an idea of the fundamental structure of the java language.

Java IDE installation for windows

Comments in Java

The 1st line of the above java program is called a comment. Comments are, basically, texts either begins after two forward-slash (//) or are enclosed inside '/*' and ' */' to make the code more readable and meaningful. Here in our case from the very beginning line itself we understand, this is ‘Your first program- Hello World Program’.

In Java we can write comments in two ways:

  • Single line comments: are texts that begin with two forward slash  // and its life exists only in one line and ends when the cursor moves to the next line.
  • Multi-Line comments: are texts that start and end with /* and */ respectively. it can be a single line or multi-line. If you write anything between ' /*' and ' */'it will not be executed, rather the compiler will overlook whatever is written between these two notations. 

The comments section is not for the front-end user but the programmers. It helps them to keep track of every section of the program without any interference with the programming structure. Since comments are not a programming statement they can appear anywhere in the program but are optional.

Java class declaration

A java program is basically a collection of java instructions that executes in some order. Being an object-oriented programming language, everything in java is connected with classes and objects. Hence every instruction in java should reside inside a class and everything inside the class must be written inside a starting brace '{' and closing brace '}'. Unlike in other programming languages, the left brace in java starts in the same line where we define the class. So we can think of class in java as a container with a set of variables and its associated methods. 

A simple class definition is as follows:


public class HelloWorld {
 
}
 

Here, Helloworld is the name of the class. ‘class’ is the java keyword used for declaring a new java class, and ‘public’ is another java keyword used to denote the access of class, whether the class is publically accessible or privately accessible.

While saving the java code, you should bear in mind that,” the name of the java file should be the same as the name of the class”. That means here the filename of the java code should be HelloWorld followed by the extension .java. So HelloWorld.java is the file name.

The main() method in Java

Another inevitable part of a java program is the methods. Methods are a set of instructions used to manipulate variables. Methods are mainly located inside the class. A class can contain an ‘n’ number of methods but no less than one. In other words, a class must contain at least one method to signal the Java Virtual Machine where to start the execution of the program. This single method that triggers the execution of the java program is commonly referred to as the main() method in java. Everything inside the main() method must be written inside a starting brace '{' and closing brace '}'

The main() method declaration takes the form as given below:


public static void main(String[] args) {
   Statement/s;
}

Here,

public:  The keyword public denotes the access specifier of the method. Here the method is declared as public which means it is accessible from anywhere in the program. 

static: The java keyword static is used for memory management. It tells that the method main() is static to the class rather than to the instance of the class. 

void:  The java keyword void specifies that the main method does not return any type.

main(): This is the name of the only method which initiates the execution of the java program. 

String[] args:   it is simply an array of strings that stores user data passed by the command line when starting a program. To be specific ‘args’ stores all the command line arguments in the form of an array of strings.

Statements in Java

Statements in java are purely java instructions to the compiler when a program starts to execute. A print statement in java has the following signature:

Here, 

  • The System is a class inside the package java.lang which contains 3 main streams namely System.in, System.out and System.err. In java most often we use System.out to display the output from the application.
  • The out is both a static and public member of the system class and is of type PrintStream.
  • The println() is a method of PrintStream class used to print the message passed to the console and adds a new line.

Finally, the output of the above program is just the text inside the println() method. That is nothing other than the text “ Hello, World!!!”.

Note: A statement must always end with a semicolon

How to compile and execute a java program

After writing a java program, the next necessary step is to compile them. Compilation in Java is quite different from the other languages. Here the programmer's readable code is converted into byte codes which are easily interpretable by any computer that has JVM. To know more about Java Virtual Machine refer to our previous tutorials. Execution is the process of running the program to get our desired output.

USING COMMAND PROMPT 

Here we will see how Compilation and Execution can be performed using Notepad and  Command Prompt in the Windows environment step by step.

Step 1: Open notepad and write your first java program as given above.

Java IDE installation for windows

Step 2: Save the program with the “.java” extension like  “HelloWorld.java” in any workspace as you wish. Here I have saved it on desktop inside the folder JPRO which is highlighted in yellow color in the below picture.

Java IDE installation for windows

Step 3: Now open your command prompt and change the directory to where the program is saved.

Java IDE installation for windows

Step 4. Now provide the command for compilation as  “Javac HelloWorld.java”. 

  •  Without error: If the java code is perfect without any syntax errors then the following resultant screen will appear. 
    Java IDE installation for windows
  • With error: In case if you missed one brace or some other error happens the compiler will point out the error as shown below.
    Java IDE installation for windows

Step 5: Next run the program to get the desired output with the command 
“ java HelloWorld ”.

Java IDE installation for windows

USING ECLIPSE IDE

If you are a Windows user and have not downloaded eclipse IDE yet take your time to visit our tutorial-Java IDE installation for Windows to learn the installation and set up of IDE for hassle-free programming. Now let’s familiarize ourselves with the eclipse IDE to write your first program and then to compile and run it.

Step 1: Open the Eclipse IDE. The following window will appear.

Java First Program

Step 2: To create a java project follow the below step which is well picturized for easy understanding.

Click file -> New -> Java Project ->hit Enter.

Java First Program

Step 3: A window as shown below appear on your screen.

  1. Provide a suitable name for your java project. Here, our project name is MyJavaProject.
  2. You can choose the workspace as your wish. Here we stick with the default location.
  3. Then click on the Finish button.
Java First Program

Step 4: Now a screen like the one below will appear to you. The window contains 3 parts 

  1. The red box represents the project explorer area where all your works are listed. If you can’t find the project explorer on your LHS then follow the path  Window -> Show View -> Project Explorer.
  2. The orange box represents the editor area where you type all your java codes.
  3. The blue box represents the console area where all the outputs are displayed.
Java First Program

Step 5: Now to write your first program in the Eclipse IDE, you need to create a class under your java project. To create a class 

Right-click on the  MyJavaProject -> then click on New -> then click on the class

Java First Program

Step 6:   The following window will be the result    

  1. Given a class name. Here we gave the class name  “HelloWorld”.
  2. Make sure to tick the main method which is highlighted in green colour in the screenshot.
  3. Finally hit on the Finish button.
Java First Program

    

Step 7: On clicking the finish button, the below window will show up with some predefined codes in the editor area. The java class and the main method are created automatically, now you can start writing your codes in the statements.

 

Java First Program

On the project explorer, you can notice that a java file named HelloWorld.java is created. 

Note: Shortcut to run a java code is Ctrl key +F11.

Step 8: In Eclipse IDE both compilation and execution are done by a single button named the “Run” button as shown below.

Java First Program

Step 9: On executing the java code the output will be displayed on the console as in the screenshot below.

Java First Program