18

I understand that String[] args is an array of strings passed into main as parameters.

java Print "Hello, World!"

 

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

However, when you don't include it as a parameter (even if you don't use), it throws an exception. So why is it required? Also, why can't it be an int[] or boolean[]?

Catharsis
  • 466
  • 3
  • 9
  • 20

6 Answers6

35

It's a String because the command line is expressed in text. If you want to convert that text into integers or booleans, you have to do that yourself - how would the operating system or Java bootstrapper know exactly how you wanted everything to be parsed? I suppose Java could look for a main method with the same number of parameters as command line arguments, and then try to parse them using Integer.parseInt etc... but this would be pretty complicated, and would still be inadequate in many situations.

As for it being mandatory - basically the Java designers decided to stick to a single method signature, which is frankly simpler than allowing for it to be optional. It would be possible though - in C#, you can have any of

static void Main()
static void Main(string[] args)
static int Main()
static int Main(string[] args)

Ultimately it doesn't make a lot of difference, to be honest. There's no significant downside in having to include the parameter.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • +1 - it doesn't make any difference. Move along, nothing happening here :-) – Stephen C Nov 04 '09 at 07:02
  • @paxdiablo: A little fun due to my talk at DevDays, that's all. Will revert in a few days or a week. – Jon Skeet Nov 04 '09 at 07:05
  • Thanks for the answer Jon! Btw, your latest article was very, very good. – Catharsis Nov 04 '09 at 07:13
  • I was just wondering: who the heck has a reputation of 111k but is not Jon? Oh, Jon. That’s who. :) – Bombe Nov 04 '09 at 07:51
  • Why such a signature? Its gives an Exception if not written! – abhishah901 Nov 01 '15 at 08:07
  • @abhishah901 What do you mean? – Jon Skeet Nov 01 '15 at 08:07
  • It gives an exception if not mentioned! Why the interpreter expects a signature? – abhishah901 Nov 01 '15 at 08:10
  • @abhishah901: *What* interpreter? What exception? My answer explains why the bootstrap expects this signature - what part of the answer don't you understand? What would you expect the alternative to be? – Jon Skeet Nov 01 '15 at 09:29
  • @abhishah901: "because the command line is expressed in text". Java could have gone the same way as C#, allowing the method to have no parameters instead... but what alternative would you suggest otherwise? What would you expect to happen if the `Main` method had a parameter of `Button` for example? – Jon Skeet Nov 01 '15 at 12:15
  • In response to "There's no significant downside in having to include the parameter", 1. Some might consider it a significant downside to be forced to consume the max allowable memory that `String[] args` permits. 2. If the language optimizes this by discovering that `args` is not referenced elsewhere, the requirement seems even more wrong as it is an anti-pattern to declare something that will never be accessed during runtime. – John B Feb 12 '17 at 02:03
4

The Java runtime system looks specifically for a method with a single String[] type parameter, because it wants to pass the parameters to your main method. If such a method is not present, it informs you through an exception.

If you want to treat the (string) command line parameters as integers or booleans, you are expected to do the conversion yourself. This allows you to handle the condition where somebody enters "ponies" where you expect an integer.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • Why? If you know the reason tell me here-http://stackoverflow.com/questions/33454612/significance-of-string-args-while-writing-main – abhishah901 Nov 01 '15 at 08:08
3

That's how the language was designed. C and C++ both do it in a similar fashion, though with a separate count, information that's included in the string array for Java.

You could have designed it to take integers or booleans but that would have added more work to the Java runtime for potentially little benefit.

Strings can be used to transmit any sort of information into the main program (including integers and booleans). Not so for integers if you want to pass in a string (short of the user having to manually encode it as UTF-8 or something, not something that would endear people to the language).

Just remember to declare it. By all means, raise a change request on the language if you wish (to make it optional, or allow other signatures) but I suspect that will not get very far.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

It's a convention. The method is used by command line interpreter and that's how it expects it. Moreover - the compiler expects the signature in this particular form too and won't compile if you omit the parameter, for example

Bostone
  • 36,858
  • 39
  • 167
  • 227
0

The string[] parameter is used to save command line arguments with first being the class name. Moreover,if you call your main method without string[] it will simply be an overload and JVM will not be calling that method.And if you skip this signature main method JVM will throw an exception

  • The first command line argument is not the class name in Java. If we are not passing any command line argument, i.e. just saying java yourClassName, this won't put the className in the String array. In C/C++ the executable File Name is the first argument. – Vivek Oct 28 '12 at 14:04
-1

Because the code that runs your main() function is looking very specifically for that signature. Think of it as having, somewhere:

YourClass.main(new String[] { ... });

Obviously, if main takes anything else, this will fail.

bdonlan
  • 224,562
  • 31
  • 268
  • 324