简体   繁体   中英

Why are command line arguments passed as a String array in Java?

In Java, the command line arguments are passed into my program as a String array, like this:

public static void main(String[] args ) { }

Why does this use an array, and not a single String ?

I think it would be more flexible / adaptable if the command line arguments were passed in as a single String, and let my program do with it what I want. Is there any design advantage achieved by using a String array instead of a single String ?

Why does this use an array

When a programs start they are passed an array of C strings and this is natural and simplest translation of this input.

, and not a single String ?

This is because there is a difference between hello world (2 words) and hello world (1 word)

  1. C programmers were familiar with this format.

  2. Green team decided to pass parameters this way.

  3. Separating the command line to elements is platform dependant and (I think) done by the shell. So, passing command line as a array of Strings allows creating portable, cross-platform code.

The real reason is because Unix does it that way.*

Every Unix program, regardless of what language it's written in, receives an array of string arguments. It's been that way since Unix was created back in 1970-something. Long before Windows or even MS-DOS existed.


Some of the other answers said, "...because C does it that way." That's pretty close to the same thing: C does it that way because C was the original, most-favored programming language of Unix.


*Linux too. Linux is not Unix because "Unix" is a trademark that costs a lot of money, but Linux strives to be as close to Unix as it possibly can be.

Lets consider the below program

public static void main(String[] commandLineArguments) {
        int argumentLength = commandLineArguments.length;
        if (argumentLength == 2) {
            System.out.println("User's 1st Name:" + commandLineArguments[0]
                    + " Last Name :" + commandLineArguments[1]);
        } else {
            System.out.println("User's only entered 1st name :"
                    + commandLineArguments[0]);
        }
    }

If user enters James Gosling the runtime system interprets the space character as a separator for command line arguments and out put of this program is

User's 1st Name:James & Last Name is:Gosling

Now if command line arguments passed as a String instead of String array then how user will pass multiple arguments in the program? String array allows user to pass multiple arguments and not the String . But what if you want to pass a multiple Strings as single argument? The answer is : you would join them with double quotes (which the system consumes) like "This is a single argument"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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