0

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 ?

7
  • 5
    Left over from C: It traditionally took in a pointer to character pointers. I think this option allows users to either pass in multiple values or do as your suggesting and parse a single String. No advantage to either one. Opinion based; voting to close. Commented May 8, 2015 at 12:07
  • 3
    Actually, you can do java myProg "A short string", and access the string through args[0] if you really want to use a single string :) Commented May 8, 2015 at 12:07
  • I really can't see any advantage to be honest. You can always append your Strings together if you really insist. Commented May 8, 2015 at 12:07
  • 1
    Using an array is much more flexible, for example to handle and parse options. Having a single String and extracting the options from it myself would be more flexible, no ? Commented May 8, 2015 at 12:10
  • You can look at stackoverflow.com/questions/11952804/… Commented May 8, 2015 at 12:11

4 Answers 4

5

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)

Sign up to request clarification or add additional context in comments.

Comments

4
  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.

Comments

4

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.

1 Comment

Makes sense. Didn't expect so many good answers, but I can only accept one of them :-) Thanks !
1

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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.