什么是“字符串参数[]"?Java主方法中的参数 [英] What is "String args[]"? parameter in main method Java

查看:25
本文介绍了什么是“字符串参数[]"?Java主方法中的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始用 Java 编写程序.以下 Java 代码是什么意思?

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

public static void main(String[] args)

  • 什么是String[] args?

    你什么时候会使用这些args?

    When would you use these args?

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

    Source code and/or examples are preferred over abstract explanations

    推荐答案

    In Java args 包含提供的 命令行参数 作为 String 对象的数组.

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

    换句话说,如果你以 java MyProgram one two 运行你的程序,那么 args 将包含 ["one", "two"].

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

    如果你想输出args的内容,你可以像这样循环它们......

    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]);
            }
        }
    }
    

    这篇关于什么是“字符串参数[]"?Java主方法中的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆