How To Write First Java Program – 5 Simple Steps

Before writing your first java program you’ll need two things:

      1. Java Development Kit

Follow this tutorial to download and install Java.

      2. Text Editor

You can use any text editor of your choice for writing your first Java Program. In this tutorial, we’ll use Notepad++ text editor.

1. Steps to Run Your First Java Program

Step 1.) Open your text editor and type following code. I’ll explain later what does it mean.

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

Step 2.) Now, save your file with ClassName.Java that means Demo.Java

  • Remember, always save your file with the name of the main class. It will be easy to compile and run the program.

Step 3.) Open CMD and go to the directory using below commands:

First Java Program

Step 4.) Compile your program with Java Compiler (javac).

  • A .class file will be saved at the same location as soon as you hit javac command.
  • This file is responsible that makes java platform-independent. You can easily copy this file to any other operating system and run your program.

Step 5.) Run your program with a command (java .classFileName).

java Demo

Congratulations! You wrote your first Java Program.

Note: Java is case sensitive programming language. That means you should use consistent casing while typing any method, code or command.

2. Difference Between JAVAC and JAVA?

  1. JAVAC: This is the Java compiler which converts your java code into the byte code.
  2. JAVA: This command executes the ByteCode/source file.

3. Understanding First Java Program

Let’s understand what is the meaning of the code written by us.

Sample Code:

class Demo {
    public static void main(String args[]) {
        System.out.println("Hello World");
Remember public static void main is the entry point of any program.
  1. Demo: This is the name of the class.
  2. public: This is the access modifier which means you can access your main method anywhere in the program.
  3. static: It means that JVM can invoke the main method without instantiating the class and can save memory.
  4. void: It simply means that the main method doesn’t return anything.
  5. main: It is the name of the Java main method which helps JVM to invoke it at first.
  6. String args[]: It means that you can pass String type argument in the Java Command line. You can name args[] anything. Maybe like String yourName[].
  7. System.out.println: It is used to print anything in java.