Main method prototype in Java readme

1. The basic prototype of the main method

public class Test {
      public static void main(String args[] ) {
        System.out.println("helloWord!");
    }
}

public: The main method access permission is set to public because the main method is called by the Java virtual machine when the program starts.
static: The main method can be called directly by the Java virtual machine without generating an object instance.
The void main method has no exit code when it exits, and uses System.exit() to return when needed, so the return type of the main method is void
String[] arg: This string is a parameter used to receive user input, and the user has no input If so, the length of the args array is 0

2. Modify the main method prototype

  • remove public test
public class Test {
       static void main(String args[] ) {
        System.out.println("helloWord!");
    }
}

remove public
The main method must be public, otherwise the Java virtual machine cannot be called.

  • remove static test
public class Test {
       public void main(String args[] ) {
        System.out.println("helloWord!");
    }
}

remove static test
The Java virtual machine calls the main method with static, and the required main method cannot be found here.

  • Remove the parameters of the main method to test
public class Test {
       public void main() {
        System.out.println("helloWord!");
    }
}

Remove the parameters of the main method to test
It also prompts that the main method cannot be found.

  • Modify the return value type of the main method
public class Test {
       public static int main(String[] args) {
        System.out.println("helloWord!");
        return 0;
    }
}

write picture description here
Hint that the main method must return a null type value.

Associated blog (blog garden):

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325980100&siteId=291194637