Understanding public static void main(String[] args) in Java

java dev.to

What is the main() method?

The main() method is the entry point of a Java application.

When you run a Java class, the JVM looks for a valid main() method and starts executing the program from there.

public static void main(String[] args) {
    System.out.println("Hello Java");
}
Enter fullscreen mode Exit fullscreen mode

You can think of it as the starting point of a standalone Java application.

When is the main() method called?

The main() method is called when we launch a Java application.
Example:

java Main
Enter fullscreen mode Exit fullscreen mode

The JVM loads the Main class and looks for the appropriate main() method.
If it finds one, execution starts from there:

java Main
   ↓
JVM starts
   ↓
Loads Main class
   ↓
Finds main()
   ↓
Executes main()
   ↓
Program continues
Enter fullscreen mode Exit fullscreen mode

So, you don't normally call main() yourself like:

main();
Enter fullscreen mode Exit fullscreen mode

The JVM invokes it as the application's entry point.

Why do we need the main() method?

The JVM needs to know:

"Where should It start executing this Java application?"

The main() method provides that starting point.
Example:

public class Main {

    public static void main(String[] args) {
        System.out.println("Step 1");
        System.out.println("Step 2");
        System.out.println("Step 3");
    }
}
Enter fullscreen mode Exit fullscreen mode

The JVM starts execution from:

main()
Enter fullscreen mode Exit fullscreen mode

and then executes the statements sequentially.
Without a valid entry-point main() method, a normal standalone Java application cannot start through the java launcher.

How does the main() method work?

public class Main {

    public static void main(String[] args) {
        System.out.println("Hello");
    }
}
Enter fullscreen mode Exit fullscreen mode

You compile it:

javac Main.java
Enter fullscreen mode Exit fullscreen mode

This produces:

Main.class
Enter fullscreen mode Exit fullscreen mode

Then you run:

java Main
Enter fullscreen mode Exit fullscreen mode

The process is approximately:

Main.java
   ↓
javac
   ↓
Main.class
   ↓
java Main
   ↓
JVM
   ↓
Finds main()
   ↓
Executes main()
   ↓
Hello
Enter fullscreen mode Exit fullscreen mode

What does main itself mean?

public static void main(String[] args)
Enter fullscreen mode Exit fullscreen mode

each part has a purpose:

The main() method is the entry point of a standalone Java application. When we run a Java program, the JVM looks for a valid main() method and starts execution from it. We use it to provide the starting point for program execution. The standard signature is public static void main(String[] args).

It is static because the JVM needs to invoke the main() method without creating an object of the class first.

What is String[] args?

String[] args is a String array parameter of the main() method.
String → each value is a String.
[] → it is an array, so it can store multiple Strings.
args → the variable name; it stands for arguments.

So, String[] args means an array of String values passed to the program from the command line.

When is it used?

It is used when we want to pass command-line arguments to a Java program while starting it.

java Main Vicky 5
Enter fullscreen mode Exit fullscreen mode

Here It Looks Like:

args[0] = "Vicky"
args[1] = "5"
Enter fullscreen mode Exit fullscreen mode

Example program:

public class Main {
    public static void main(String[] args) {
        System.out.println(args[0]);
        System.out.println(args[1]);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Vicky
5
Enter fullscreen mode Exit fullscreen mode

Why do we use it?

We use String[] args to allow the program to receive input from outside the program at the time of execution, without hard-coding that input inside the source code.

For Example:

java Calculator 10 20
Enter fullscreen mode Exit fullscreen mode

The program can receive 10 and 20 through args.

How does it work?

When we execute:

java Main Hello Java
Enter fullscreen mode Exit fullscreen mode

Java creates the args array approximately like this:

args
 ├── args[0] → "Hello"
 └── args[1] → "Java"
Enter fullscreen mode Exit fullscreen mode

Then the program can access them using their indexes:

System.out.println(args[0]); // Hello
System.out.println(args[1]); // Java
Enter fullscreen mode Exit fullscreen mode

String[] args is a String array parameter of the main method. It is used to receive command-line arguments when we run a Java program. We use it when we want to pass input to the program during execution instead of hard-coding it. Each command-line argument is stored as a String in the args array and can be accessed using its index.

Note:

args is just a variable name. You can technically write:

public static void main(String[] anything)
Enter fullscreen mode Exit fullscreen mode

and it will work. args is simply the conventional name.

References:

https://www.geeksforgeeks.org/java/java-main-method-public-static-void-main-string-args/
https://medium.com/@sunkavallisowjanya/what-does-public-static-void-main-string-args-df5790cb037c

Source: dev.to

arrow_back Back to Tutorials