Home >Java >javaTutorial >Java public static void main(String args) Java public static void main function (String parameter)

Java public static void main(String args) Java public static void main function (String parameter)

王林
王林forward
2023-08-18 20:33:021661browse

Java public static void main(String args) 

Java 公共静态 void 主函数(String 参数)

When the JVM calls the main() method, the Java program begins execution. Java applications start with this method. Without the main method, the Java file compiles successfully because at compile time, the compiler does not check the main method, but at runtime the JVM checks if the main() method is available. Therefore, we will get an exception at runtime.

In this article, we will learn why we should follow the convention "public static void main(String[] args)."

grammar

public class class_name {
   // This line must be written as it is
   public static void main(String[] args) {  
      // code will be wriiten here
   }
}
The Chinese translation of

Example 1

is:

Example 1

public class Tutorialspoint {
   public static void main(String []args) {
      System.out.println("Hello, you are on tutorials point");
   }
}

Output

Hello, you are on tutorials point

In our java file, there should be at least one public class available. By convention, the main method must be defined in a class, because in Java everything is inside the class. This shows that java is an object-oriented language.

In the above example, the class 'Tutorialspoint' contains the main() method. Let’s discuss the various parts of the main() method −

public

is translated as:

public

public is an access modifier used to define the visibility and accessibility of variables and methods. Variables and methods defined using the public keyword can be accessed by any class or package. Previously we discussed that the JVM calls the main() method, which is not located in the current class. Therefore, main() method is declared as public so that we can access it in global scope or anywhere.

What happens if we don’t use the public keyword in the main() method?

Example 2

is translated as:

Example 2

public class Tutorialspoint {
   static void main(String []args){
      System.out.println("Hello, you are on tutorials point");
   }
}

Output

Error: Main method not found in class Tutorialspoint, please define the main method as:
   public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application

We encountered this error because the JVM could not find the main() method.

static

Normally, we call methods by creating an object of a class, but static methods can be called without using an object. JVM calls the main() method before creating any object, that's why we need to declare it as static.

The Chinese translation of

Example 3

is:

Example 3

Most members of the built-in class Math are static. We can use them directly without creating objects.

public class Main {
   public static void main( String[] args ) {
      double x = 6.55;
      double y = 4.32;
      System.out.println(" Ceil value of x: " + Math.ceil(x) );
      System.out.println(" Floor value of y: " + Math.floor(y) );
   }
}

Output

 Ceil value of x: 7.0
 Floor value of y: 4.0

The above example demonstrates the use of the static methods ceil() and floor() of the Math class. We can see that they are used directly in our program without creating any objects of the Math class.

Let's see what happens if we don't declare the main() method as static.

The Chinese translation of

Example 4

is:

Example 4

public class Tutorialspoint {
   public void main(String []args){
      System.out.println("Hello, you are on tutorials point");
   }
}

Output

Error: Main method is not static in class Tutorialspoint, please define the main method as:
   public static void main(String[] args)

JVM cannot call the above code.

void

The return type void indicates that the method does not return anything. In Java, the main() method is the entry and exit point of the program. When the main() method completes execution, the Java program also ends execution. If we provide any return type like int or double, it will throw an error at compile time.

Let us understand through an example -

Example 5

public class Tutorialspoint {
   public int main(String []args){
      System.out.println("Hello, you are on tutorials point");
   }
}

Output

Tutorialspoint.java:4: error: missing return statement
    }
    ^
1 error

This is a compile-time error. The compiler requires a return statement, but it is of no use to the JVM.

main()

main is the name of the method, do not mistake it for the keyword. It is always written in the form "main".

The Chinese translation of

String[] args

is:

String[] args

String[] args is a parameter that accepts String type parameters. It allows us to pass parameters through the terminal and store these parameters in a string array. We can say that String[] args is a command line argument.

Example 6

This example will illustrate how to pass parameters from the terminal to a Java file.

public class Arg {
   public static void main(String []args){
      // for each loop to print argument taken from terminal
      for(String arg : args) {  
         System.out.println(arg);
      }
   }
}

Output

To compile the code from the terminal, type the following command: javac Arg.java

To run the code from the terminal, enter the following command: java Arg "Your String"

PS D:\Java Programs> java Arg "Hello, You are on Tutorials Point"
Hello, You are on Tutorials Point

in conclusion

In this article, we discussed the Java program for understanding "public static void main(String[] args)". We also discussed how to receive input from the terminal using the parameters ‘String[] args’.

The above is the detailed content of Java public static void main(String args) Java public static void main function (String parameter). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete