Why we Pass String array as argument to main() method, why not any collection type or wrapper type or primitive type?

后端 未结 5 1115
栀梦
栀梦 2020-12-20 13:41

why is it mandatory to pass string arg[] as an argument in main method? why we cannot pass any other data type available in java? whats the importance of passing String arg[

5条回答
  •  天命终不由人
    2020-12-20 14:12

    Because by passing String arrays, we can pass all the necessary parameters like options/arguments related to the program in the form of String easily. There can be several parameters!

    Also, all the other datatypes can be easily converted from String!

    An example of calling a program with several parameters therefore resulting in storage of them in String array!

    java Sample_Example example1 example2 example3  
    

    Arguments:

      args[0]=example1
      args[1]=example2
      args[2]=example3 
    

    Here, you are calling Sample_Example Class and passing three parameters example1,example2 and example3 to be used by the program! So, it is always an better alternative to store them in an String array rather than other primitive data-types or in collections or in Wrapper data-types. Always we tend to make our work simpler and here Java makes it simpler for all of us by providing this facility!

提交回复
热议问题