Why Java Only Allows String[] args as Main method Argument? [duplicate]

This is how you would run a java program in a command line:

java YourMainClass

The command line arguments comes after that:

java YourMainClass arg1 arg2 arg3

This would make the args string array contain "arg1", "arg2" and "arg3". If you make the args array an int[], how on earth is arg1 going to be put into an int?

From the JVM’s perspective, putting the command line arguments in Strings is the safest option. Anything that the user enters, it is gotta be a bunch of chars.

As per the JLS,

Section 12.1.4:

Finally, after completion of the initialization for class Test (during
which other consequential loading, linking, and initializing may have
occurred), the method main of Test is invoked.

The method main must be declared public, static, and void. It must
specify a formal parameter (ยง8.4.1) whose declared type is array of
String
.

Leave a Comment