Why only String type arguments in java main method
I want to know the reason behind allowing only String type arguments and not other primitiv开发者_Python百科e types. Why the developers have created it in that way
Pretty much all common operating systems provide only simple strings as the arguments to newly launched processes.
The decision to do the same for main
simply mirrors the most common interface.
The arguments were for command-line arguments, strings are what the user enters at the command line. Seems reasonable to me.
When you pass arguments from a console (cmd.exe for instance), the command entered is in fact a String (eg: "java -jar myApp.jar" & your arg1, arg2.. etc)
Since you can enter numeric arguments, string arguments, it takes it all as a String since (almost) everything can be interpreted as characters.
You can parse a String as ints, floats, etc. anyway so where's the problem?
Any args that you pass to the process can be represented as a String
. This is not true of the other primitive types, so String
is really the only option.
The signature of the Java main
method was taken from C++ (with slight improvements to make it more type-safe), to make it easier for C/C++ developers to pick up Java. C++'s main
in turn was inherited from C. Which was probably inherited (or at least influenced by) B and BCPL, its predecessors. Which probably used string parameters for the reasons explained in other answers.
精彩评论