Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Command Line Arguments

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 589
    Comment on it

    Command Line Arguments:

    Command Line Arguments are those arguments which are passed during executing a program through console.

    Arguments passed from  console can be used in a java program and  can be taken as an input.

    Example:

    1. public class CommandLineArguments
    2. {
    3. public static void main(String args[])
    4. {
    5. /*args[0] gets the first value we passed as command line argument
    6. * and displaying that value
    7. */
    8. System.out.println(args[0]);
    9. /*args[1] gets the second value we passed as command line argument
    10. * and displaying that value
    11. */
    12. System.out.println(args[1]);
    13. }
    14. }
    • compile it > javac CommandLineArguments.java  
    • run by > java CommandLineArguments Hello Hey
    1. Output: Hello
    2. Hey

    In the above example we have passed two command line arguments 'Hello' and 'Hey'.


    We can pass any number of command line arguments and these elements can be accessed as “args[0]” , “args[1]” , “args[2]” etc or we can traverse the array using for loop.

    1. for(int i=0;i<args.length;i++)
    2. System.out.println(args[i]); //traversing the whole array
    3. }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: