Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
302 views
in Technique[技术] by (71.8m points)

java - 什么是“ String args []”? 主方法Java中的参数(What is “String args[]”? parameter in main method Java)

I'm just beginning to write programs in Java.

(我刚刚开始用Java编写程序。)

What does the following Java code mean?

(以下Java代码是什么意思?)

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

    (什么是String[] args ?)

  • When would you use these args ?

    (您什么时候使用这些args ?)

Source code and/or examples are preferred over abstract explanations

(源代码和/或示例优先于抽象解释)

  ask by freddiefujiwara translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

(在Java中, args包含作为String对象数组提供的命令行参数 。)

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

(换句话说,如果您以java MyProgram one two运行您的程序,则args将包含["one", "two"] 。)

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

(如果要输出args的内容,可以像这样循环遍历它们...)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...