Writing and Running First Java Program Step by Step

Learn how to run your first Java program. Step by step guide to install JDK, writing Java program, compilation and then execution of it.

In this tutorial we will learn to write and execute our first Java Program. We will calculate the sum of 2 and 2. After calculation we will display that in console (terminal).

Step 1: Installing JDK

We need Java Development Kit (JDK) installed on our machine for writing our Java application.

Most of the computers comes with JRE (Java Run time Environment) installed with it. JRE is sufficient for executing Java Program. Though for developing Java based program we need JDK (Java Development Kit) installed on the system.

You can download latest version of JDK from Oracle’s Java SE page. At the time of writing this article, Java SE 7u51 is available.

Before moving ahead on writing your first Java Program, we want to make sure that java has been installed and java & javac command are available for execution.

Note: I am considering you are using Windows (For Mac and Linux you can execute same command over terminal) Step 1: Open Run Box (Windows Button + R) and type cmd

Step 2: On command prompt type java

Step 3: On command prompt type java -version to see the version of java installed and on path.

Step 4: On command prompt type javac to confirm if java compilation program installed and available on path.

If all goes well and you are seeing output as shown in the picture, we can move ahead with writing our first java program.

Step 2: Writing, Compiling & Executing Program

We need or plan few more things before start writing our first program.

1. Text editor: We need to type our java program in a text editor. I will strongly suggest to start with basic text editor like Notepad. It doesnt give you syntax highlighting or code assistance. And both of the feature will reduce your learning capability of core concept.

2. Folder structure: We should better decide where we will keep our java and class (compiled java file) files. Lets create a main project folder with name learning java. Inside this folder create two sub folders src and bin. In src folder we will keep our java files while in bin folder we will have class files. You may can choose to keep java and class file in same folder. But why not following best practices from beginning.

Well we are good to start writing our first java program.

Writing Program

Create a file in src folder with name TwoPlusTwo.java. Open that file in Notepad and type the following code:

 This is multiple line comment
class TwoPlusTwo {
 public static void main (String\[\] args) {
    // This is a single line comment 
  System.out.println("Sum of 2+2 = "+ (2+2));



Compiling Program

Compilation is a mandatory step before we can execute a java program. It takes .java source files (called compilation unit) and generate .class files. Lets compile our java file. Open command (terminal) window and go to src directory and then execute the javac command:

\>javac TwoPlusTwo.java

TwoPlusTwo.class get generated into folder bin.

Compiler javac validates the program for any compilation error. If there is any compilation error, javac compiler display that on screen along with line number. An example of compilation error has been given below:

> javac TwoPlusTwo.java
TwoPlusTwo.java:3: error: ';' expected
                System.out.println("Sum of 2+2 = "+ (2+2))
1 error

We had removed ; from 3rd line which is a mandatory syntax for java program.

Executing Program

Lets execute our first java program now. Open command or terminal window and go to bin directory then use java command to execute:

> java TwoPlusTwo
Sum of 2+2 = 4

Program successfully executed with desired output Sum of 2+2 = 4.

Understanding Program

  This is multiple line comment

This is a comment inside the program source. A comment start with /* and ends with */, anything came between it is ignored by the compiler. We use comment for providing non code information in source code. There is another way of writing comment, which we will see soon.

class TwoPlusTwo {

class is keyword use to define new class, here TwoPlusTwo. { is starting of class and } is the end. Anything comes between is called attributes or member of class. It can be variable or method (function).

public static void main (String\[\] args) {

Here we had declared the main method. Every Java program begin with a call to main method.

public is an access modifier, which define the visibility of the method from different access level. main is the origin method and it need to be access from anywhere, hence public has been used. There are few other types of access modifier available: private, protected, default (when no access modifier mentioned).

static keyword enable access to a method or variable defined in class without its creation.

// This is a single line comment

A single line comment starts with // and ends with the line.

System.out.println("Sum of 2+2 = "+ (2+2));

This line does all the tricks for the output we saw. It outputs Sum of 2+2 = 4. System is a predefined class and out is an output stream connected with console. System.out collectively represent console output. Remember In real application console input and output rarely used. There are graphical user interface as well as web interface used prominently.

println takes a String as a parameter and print it out. "Sum of 2+2 = " is a String and then we concatenate it with expression which evaluate 2+2.

Mrityunjay Kumar
Mrityunjay Kumar
Principal Architect

My interests include software architecture, cloud computing, data & Artificial Intelligence.

comments powered by Disqus