Skip to main content

Practical-11

Write a Java Program that will accept Command-line Arguments and display the same.


Introduction

This Java program accepts command-line arguments and displays the same. Command-line arguments allow us to provide input to the program from the command-line/terminal/shell. The command-line arguments are stored in the args[] array, which is declared in the main() method.

Code

public class CommandLineArgs {
public static void main(String[] args) {
for(int i = 0; i < args.length; i++){
System.out.println(args[i]);
}
}
}

Output

Argument 1
Argument 2
Argument 3
Explanation

This code declares a public class named 'CommandLineArgs'. Then, the main() method is declared which stores command-line arguments provided (separated by a space) in the args[] array. A for loop is used to iterate through this array and display the argument values. : :