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

前端 未结 17 1478
有刺的猬
有刺的猬 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:02

    In Java args contains the supplied command-line arguments as an array of String objects.

    In other words, if you run your program as java MyProgram one two then args will contain ["one", "two"].

    If you wanted to output the contents of args, you can just loop through them like this...

    public class ArgumentExample {
        public static void main(String[] args) {
            for(int i = 0; i < args.length; i++) {
                System.out.println(args[i]);
            }
        }
    }
    

提交回复
热议问题