What's the point of String[] args in Java?

前端 未结 7 1797
感情败类
感情败类 2021-01-17 06:50

Whenever you declare the main method in a class, you always have to do a String array called \"args\". What\'s the point? Unless I live under a rock, command li

7条回答
  •  无人及你
    2021-01-17 07:10

    Because that is the signature of the main method that is called when you execute a Java class. There needs to be some convention which method will be executed. By convention it is the

    public static void main(String[] args) method
    

    And yes, you do live under the rock, there are plenty of situations when command line arguments are used. Why would they not be used?

    You could ask: why require it? Why not just pick any other main method? The answer is that it would be adding complexity with 0 benefit. As is now, main function looks distinctive. If you look at it, you know it is the one that will get called. If any main would be called, you would have to always ask yourself: is the main I am looking at the one to be invoked, or is there another main in this class which takes precedence?

提交回复
热议问题