Command-Line Arguments
Command-line arguments are values passed to a Java program at the moment it is run from the terminal, rather than typed in while the program is running. They arrive inside the String[] args parameter of the main method.
1. What are Command-Line Arguments?
Command-line arguments are values passed to a Java program at the moment it is run from the terminal, rather than typed in while the program is running. They arrive inside the String[] args parameter of the main method.
2. Why is it used?
They let a program behave differently based on values supplied right when it starts — useful for configuration options, file names, or settings, without needing to modify the program's code itself.
3. Real-Life Example
Think of starting a washing machine and selecting a wash mode (quick wash, heavy wash) using the dial before pressing start. You're supplying a setting upfront, before the machine begins its main task — similar to how command-line arguments are supplied before the program runs.
4. Syntax
javapublic static void main(String[] args) { // args[0], args[1], etc. hold the values passed }
java ProgramName value1 value25. Example Program
javapublic class CommandLineDemo { public static void main(String[] args) { if (args.length > 0) { System.out.println("Hello, " + args[0] + "!"); } else { System.out.println("No name provided."); } } }
Output (when run as `java CommandLineDemo Rahul`):
Hello, Rahul!6. Key Points to Remember
argsis always aStringarray, even if you pass numbers — you must convert them manually if needed (e.g., usingInteger.parseInt()).args.lengthtells you how many values were passed.- Command-line arguments are supplied before the program starts, unlike
Scannerinput which is read while the program is running.
Practice Problems
Try each question yourself first, then check the answer.
1. The basics of args
What is the type of args, and how do you find out how many values were passed?
Answer: args is a String[] (an array of String). args.length tells you how many arguments were passed.
2. Predict the output
javapublic class Main { public static void main(String[] args) { if (args.length > 0) { System.out.println("Hello, " + args[0] + "!"); } else { System.out.println("No name provided."); } } }
What is printed when run as java Main Rahul? And when run as java Main with no arguments?
Answer: java Main Rahul prints Hello, Rahul!. java Main prints No name provided. (because args.length is 0).
3. Why the numbers do not add
You run java Sum 8 5 and write System.out.println(args[0] + args[1]);. It prints 85, not 13. Why, and how do you fix it?
Answer: args[0] and args[1] are the Strings "8" and "5", so + concatenates them. Convert first: Integer.parseInt(args[0]) + Integer.parseInt(args[1]) gives 13.
4. Compare with Scanner
Name two differences between command-line arguments and Scanner input.
Answer: (1) Command-line arguments are supplied once, on the launch command, before the program runs; Scanner reads input while the program is running. (2) Arguments always arrive as String and must be converted manually; Scanner has typed methods like nextInt() and nextDouble().