简体   繁体   English

* java的主要args列表中的含义是什么?

[英]What does * mean in java's main args list?

I wrote a class like this and named it Solution.java . 我写了一个这样的类,并将其命名为Solution.java

public class Solution {
    public static void main(String[] args) {
        System.out.println(args.length);
    }
}

BUt when I run it in Terminal, I got a result like this: 当我在终端中运行它时,我得到了这样的结果:

>  /Users/WangWei  java Solution *
18
>  /Users/WangWei

Why 18? 为什么18?

That's probably the number of files in your working directory. 这可能是工作目录中的文件数量。

The result of * is not specific to Java. *的结果不是特定于Java。 It is specific to the environment you are working in, ie the working directory and the kind of shell (Windows command prompt, bash, ...) you are using to run the java command. 它特定于您正在使用的环境,即工作目录和用于运行java命令的shell类型(Windows命令提示符,bash,...)。 This is because the shell processes and evaluates the command line before starting the process. 这是因为shell在启动进程之前处理并评估命令行。 It replaces the * . 它取代了*

To preserve a * as a command line argument, you need to quote it: 要将*保留为命令行参数,您需要引用它:

java Solution '*'

你的shell正在解释当前文件夹的内容 - 传入了18个文件。使用单引号来避免解释星号

java Solution '*'

Just add this to your code and you'll see: 只需将其添加到您的代码中,您就会看到:

for(String line: args)
  System.out.println(line);

The number of files in your directory. 目录中的文件数。

18 shows that 18 files are there in your directory. 18显示您的目录中有18个文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM