Contribute Us | Ask Question |

Java Example

Let's have our first example.The first example is a basic one ,a welcome program.

class Simple
public static void main(String args[])
System.out.println("Welcome to the world of Java...");

way for compile and run program

java Simple

Output: Welcome to the world of java...

Explanation of welcome java program

The meaning of javac ,java command ,class, public, static, void, main, String[], System.out.println() that use in above program is given below:

class It is a keyword used to declare a class in java.

public keyword is an access modifier(1 out of 4 access modifiers) which represents visibility, it means it is visible to all.We will discuss about access modifiers later

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.

void is the return type of the method, it means it does not return any value.

main represents the start point, from where the execution of the program starts.

String[] args is used for command line argument. We tell you about String later.

System.out.println() is used as print statement. Bu using it we will print the output on screen.

javac is a command to start compiling a java program. After running javac command the class file is obtained
java produce Simple.class file

java is a command to run the program.
java Simple is used to produced the output by running the program

Ways to write public static void main(String args[])

psvm img

The way or order of writing public static void main(String args[]) is not fixed or a specific rule.It can be also be written as:

1) public static void main(String args[])

2) public static void main(String [] args)

3) public void static main(String args[])