David
David

Reputation: 195

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

I wrote a class like this and named it 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?

Upvotes: 11

Views: 417

Answers (4)

Daniel S.
Daniel S.

Reputation: 6640

That's probably the number of files in your working directory.

The result of * is not specific to Java. It is specific to the environment you are working in, i.e. the working directory and the kind of shell (Windows command prompt, bash, ...) you are using to run the java command. This is because the shell processes and evaluates the command line before starting the process. It replaces the *.

To preserve a * as a command line argument, you need to quote it:

java Solution '*'

Upvotes: 21

Reimeus
Reimeus

Reputation: 159794

Your shell is interpreting the contents of the current folder - there are 18 files being passed in. Use singles quotes to avoid interpreting the asterisk

java Solution '*'

Upvotes: 7

bidifx
bidifx

Reputation: 1650

Just add this to your code and you'll see:

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

Upvotes: 4

Rakesh KR
Rakesh KR

Reputation: 6527

The number of files in your directory.

18 shows that 18 files are there in your directory.

Upvotes: 3

Related Questions