What is “String args[]”? parameter in main method Java

前端 未结 17 1474
有刺的猬
有刺的猬 2020-11-21 23:58

I\'m just beginning to write programs in Java. What does the following Java code mean?

public static void main(String[] args)
  • What

17条回答
  •  醉酒成梦
    2020-11-22 00:17

    Those are for command-line arguments in Java.

    In other words, if you run

    java MyProgram one two

    Then args contains:

    [ "one", "two" ]

    public static void main(String [] args) {
        String one = args[0]; //=="one"
        String two = args[1]; //=="two"
    }
    

    The reason for this is to configure your application to run a particular way or provide it with some piece of information it needs.


    If you are new to Java, I highly recommend reading through the official Oracle's JavaTM Tutorials.

提交回复
热议问题