DEV Community

Deepikandas
Deepikandas

Posted on

#24 known is a drop! main method in JAVA

Q1: Why String args[] as parameter in main()?

    String args are called command line arguments in the form of arrays.
    These are used to give input during run time.
    Actually not during run time, before the program runs  itself ,we give input. 

    In cmd prompt:
    eg: java filename input1 input2       

    🟩input1 is stored in args[0],input2 is stored in args[1].  

    🟩all inputs are taken in the form of Strings. 

    🟩 if we want to perform any arithmetic operations, we have to parse the value using Integer.parseInt(String args[0]) 
    i.e., converting String to primitive int type   

    🟩  Same for other float values, boolean. We have to parse using corresponding Wrapper class parse method.  

    In Eclipse,                                                                                                        Right click->Run configurations->select Arguments tab-> enter values separated by space.(not commas in between)                               _______________                                                                                                                                                                                                 If we use Scanner class, we can inform the user to enter values as inputs are fed during run time
Enter fullscreen mode Exit fullscreen mode

but using command line args, we cannot prom

Top comments (0)