Ticker

6/recent/ticker-posts

L4, Synatx of main method, Understanding Arguments, package q10793

The below code demonstrates how command line arguments are passed to a class called CommandLineArgumentDemo

>> javaCommandLineArgumentDemoRedBlueGreenIn the above code :

the first argument Red can be accessed as args[0]

the second argument Blue can be accessed as args[1]

the third argument Green can be accessed as args[2]

How the arguments passed on command line are available to the main method.

In the above example, if we want to print the first argument the code will be as given below:

System.out.println(args[0]);

Similarly, if we want to print the third argument the code will be as given below:

System.out.println(args[2]);

Note that the index values passed in args[index] always start with 0 (zero) for first argument, 1 for second argument and so on.

Assume that three command line arguments will be passed to the below class with name CommandLineArgumentDemo.

Write code in the main(String[] args) method to print only the second argument.

package q10793 :-

package q10793;
public class CommandLineArgumentDemo {
    public static void main(String[] args) {
        //Write the code fragment in the below println(     ) method to print only the
second argument
       
        System.out.println(args[1]);
       
    }
}

Post a Comment

0 Comments