Java Tutorial

What is Java


Control Statements


Java Object Class


Java Inheritance


Java Polymorphism


Java Abstraction


Java Encapsulation


Java Array

Java OOPs Misc


Simple Program of Java

In this page, we will learn how to write the simple program of java. We can write a simple hello java program easily after installing the JDK.

To create a simple java program, you need to create a class that contains main method. Let's understand the requirement first.

Requirement for first Java Example

  1. install the JDK if you don't have installed it, and install it.
  2. set path of the jdk/bin directory.
  3. create the java program
  4. compile and execute the java program


package com.ashu;
  public class Simple {
    public static void main(String[] args) {
    System.out.println("Java64bit");
    }
}

save this file as Simple.java



To compile:javac Simple.java

To execute:javac Simple.java

Output: Java64bit

Understanding first java program

Let's see what is the meaning of class, public, static, void, main, String[], System.out.println().

  1. class keyword is used to declare a class in java.
  2. Public public keyword is an access modifier which represents visibility, it means it is visible to all.
  3. Static is a keyword, if we declare any method as static, it is known as static method. The core advantage of static method is that there is no need to create object to invoke the static method. The main method is executed by the JVM, so it doesn't require to create object to invoke the main method. So it saves memory.
  4. voidis the return type of the method, it means it doesn't return any value.
  5. Mainrepresents startup of the program.
  6. String{[]args is used for command line argument. We will learn it later.
  7. System.out.println

To write the simple program, open notepad by start menu -> All Programs -> Accessories -> notepad and write simple program as displayed below:

As displayed in the above diagram, write the simple program of java in notepad and saved it as Simple.java. To compile and run this program, you need to open command prompt by start menu -> All Programs -> Accessories -> command prompt.

To compile and run the above program, go to your current directory first; my current directory is c:\new . Write here:

To compile: javac Simple.java

To execute: java Simple

How many ways can we write a java program

There are many ways to write a java program. The modifications that can be done in a java program are given below:

  1. By changing sequence of the modifiers, method prototype is not changed.

  2. package com.ashu;
      public class Simple {
        static void public main(String[] args) { //method prototype is not changed.
        System.out.println("Java64bit");
        }
    }


  3. subscript notation in java array can be used after type, before variable or after variable.
  4. package com.ashu;
      public class Simple {
        public static void main(String[] args)
    public static void main(String []args)
    public static void main(String args[]){ // you can write different main method
        System.out.println("Java64bit");
        }
    }


  5. You can provide var-args support to main method by passing 3 ellipses (dots)
  6. Let's see the code of using var-args in main method. We will learn more var-args later.

    package com.ashu;
      public class Simple {
        public static void main(String... args) {
        System.out.println("Java64bit");
        }
    }


  7. You can use valid main method
  8. public static void main(String...args)
    public static void main(String ...args)
    public static void main(String args[])
    public static void main(String... args)
    static public void main(String[] args)
    public static final void main(String[] args)
    final public static void main(String[] args)
    final strictfp public static void main(String[] args)


  9. public void main(String[] args) // its not error but you not run because of the main method is run by JVM.
  10. public void static main(String[] args) // its error because of Return type for method is missing.

Copyright 2017 Design& development