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 ?

Jonas Czech
  • 12,018
  • 6
  • 44
  • 65
  • 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. – duffymo May 08 '15 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 :) – NiziL May 08 '15 at 12:07
  • I really can't see any advantage to be honest. You can always append your Strings together if you really insist. – Pieter De Bie May 08 '15 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 ? – Jonas Czech May 08 '15 at 12:10
  • You can look at http://stackoverflow.com/questions/11952804/explanation-of-string-args-and-static-in-public-static-void-mainstring-a – Chester Mc Allister May 08 '15 at 12:11
  • @JonasCz I remove the little part, because it was primarily opinion-based ^^ Having a single string, I think one of the first step to extract the options would be split the string, and get the array... But again, it's primarily opinion-based and certainly due to my habits – NiziL May 08 '15 at 12:18
  • Related: http://stackoverflow.com/questions/890966/what-is-string-args-parameter-in-main-method-java – Jayan May 08 '15 at 12:30

4 Answers4

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)

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
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.

Jonas Czech
  • 12,018
  • 6
  • 44
  • 65
AlexR
  • 114,158
  • 16
  • 130
  • 208
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.

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57
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"

Bacteria
  • 8,406
  • 10
  • 50
  • 67