On this page, we will learn about simple Java programs, the first Java program, the Hello World example, how it compiles, parameters used in the first Java programs, Ways of writing a Java program: valid and invalid Java main method signatures.
For creating simple java program, we need to create a class that will contain the main method of java programming. Now we shall understand the requirement first.
import java.io.*;
class Simple{
public static void main(String args[]){
System.out.println("Hello Java");
}
}
Hello Java
When we assemble Java program using javac tool, java writer converts the source code into byte code.
Let's find out what's the meaning of class
, public
, static
, void
, main
, String[]
, System.out.println()
.
Class
keyword is used to declare a class in java.Public
keyword is an access modifier which represents visibility. It means it is visible to all.Static
is a keyword. If we declare any method as static, it is known as the static method. The core advantage of the static method is that there is no need to create an object to invoke the static method. The main method is executed by the JVM, so it doesn't require to create an object to invoke the main method. So it saves memory.Void
is the return type of the method. It means it doesn't return any value.Main
represents the starting point of the program.String[]
args is used for command line argument. We will learn it later.System.out.println()
is used to print statement. Here, System is a class, out is the object of PrintStream
class, println()
is the method of PrintStream class. We will learn about the internal working of System.out.println statement later.There are many different ways to write a Java program. The modifications that we can do in a Java program are given below:-
static public void main(String args[])
public static void main(String[] args)
public static void main(String []args)
public static void main(String args[])
public static void main(String... args)
class Sample{
static public void main(String... args){
System.out.println("Wellcome to Java Word");
}
};
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)
public void main(String[] args)
static void main(String[] args)
public void static main(String[] args)
abstract public static void main(String[] args)