Why is String[] args required in Java?

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.

Leave a Comment