0% found this document useful (0 votes)
131 views

Command Line Arguments

Command line arguments allow values to be passed into a Java program when it is run. These arguments are stored as strings in the args array of the main method. The first argument is at args[0], second at args[1], and so on. Example programs are shown that print out the number of arguments and values passed, and another that calculates the sum of two integer arguments.

Uploaded by

gopi9966957145
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
131 views

Command Line Arguments

Command line arguments allow values to be passed into a Java program when it is run. These arguments are stored as strings in the args array of the main method. The first argument is at args[0], second at args[1], and so on. Example programs are shown that print out the number of arguments and values passed, and another that calculates the sum of two integer arguments.

Uploaded by

gopi9966957145
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Command line arguments:

The java command-line argument is an argument, i.e. passed at the time of


running the java program.

The arguments passed from the console can be received in the java program and
it can be used as an input.

It provides a convenient way to check the behavior of the program for the
different values.

You can pass N (1,2,3 and so on) numbers of arguments from the command
prompt.

The command line arguments can be accessed easily, because they are stored as
Strings in String array passed to the args in the main method.

The first command line argument is stored in args[0], the second argument is
stored in args[1], the third argument is stored in args[2], and so on.

Program:

//command line args

class Test

public static void main(String args[])

System.out.println(“Number of arguments=”+args.length);

for(int i=0;i<args.length;i++)</args.length;i++)

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

}}

Output:
Program 2:

//command line args -sum of 2 nos

class Test2

public static void main(String args[])

int x,y;

x=Integer.parseInt(args[0]);

y=Integer.parseInt(args[1]);

System.out.println("Sum="+(x+y));

Output:

You might also like