Understanding first program

To create a simple java program, you need to create a class that contains main method.


Program :

class First {
    public static void main(String[] args) {
        System.out.println("Hello Java");
    }
}
Hello Java

Let us understand above program

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

  class class keyword is used to create the class in java. it is a keyword which represent special meaning to compile to create java class.

  public public keyword is a modifier which is use for providing maximum accessibility to java class. It means class is accessible from any where.

  static static keyword is used to create any method static so that method gets associated with class and hence object is not required to call 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.

  void void is the return type of the method, main() method does't return any value .

  main main is a method name configured in JVM . It is starting point of any java application.

  String[] args String[] args is a String type array passed to main() method's argument . Basically it is used for command line argument.

  System.out.println("Hello Java") System.out.println() is used print any statement on console.

In how many ways we can write valid java main() method

There are many ways two write valid java main() method which are accepted by JVM. Let us see :


//example of simple java program to add two number
class First{
	public static void main(String arr[]) {
		int a = 56;
		int b = 56;
		int c = a + b;
		System.out.println("Sum of " + a + " and " + b + " is " + c);

	}
}
Sum of 56 and 56 is 112

1): we can change the order of modifier .

class First {
	static public void main(String arr[]) {
		int a = 56;
		int b = 56;
		int c = a + b;
		System.out.println("Sum of " + a + " and " + b + " is " + c);

	}
}
Sum of 56 and 56 is 112

Note : we can't change the order of return type or main .

2) : we can use any type of String array declaration in main method argument .

//we can use any of these
public static void main(String[] args)  
public static void main(String []args)  
public static void main(String args[])  

3) : we can also use 3 .(dots) . This is var-args support .

class First {
	static public void main(String... str) {
		int a = 56;
		int b = 56;
		int c = a + b;
		System.out.println("Sum of " + a + " and " + b + " is " + c);

	}
}
Sum of 56 and 56 is 112

4) : we can also use some of modifiers in main method . for example synchronized, final , strictfp


public  synchronized static final void main(String[] args)  
final public static void main(String[] args)  
final strictfp public static void main(String[] args)  

Note : order of modifiers does not matter.

5) : semicolon ( ; ) is optional at the end of method or class in java


class First {
	static public void main(String... str) {
		int a = 56;
		int b = 56;
		int c = a + b;
		System.out.println("Sum of " + a + " and " + b + " is " + c);

	};
};
Sum of 56 and 56 is 112
woman avatar