Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is String[] args required in Java?

Tags:

java

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[]?

like image 329
Catharsis Avatar asked Nov 04 '09 06:11

Catharsis


People also ask

Why do we need String [] args?

args is an array of parameters of type String, whereas String is a single parameter. A String[] will also fulfill the same purpose but the main point here is that String… args provides more readability to the user.

Why is String args [] compulsory in main method in Java?

What happens if the main() method is written without String args[]? The program will compile, but not run, because JVM will not recognize the main() method. Remember JVM always looks for the main() method with a string type array as a parameter.

What is String args [] used for in Java?

5. String[] args. It stores Java command-line arguments and is an array of type java. lang. String class.

Is String args required in main method?

Since the main method is the entry point of the Java program, whenever you execute one the JVM searches for the main method, which is public, static, with return type void, and a String array as an argument. If anything is missing the JVM raises an error.


3 Answers

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.

like image 115
Jon Skeet Avatar answered Sep 24 '22 13:09

Jon Skeet


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.

like image 28
Greg Hewgill Avatar answered Sep 22 '22 13:09

Greg Hewgill


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.

like image 30
paxdiablo Avatar answered Sep 22 '22 13:09

paxdiablo