Operators & Input · Lesson 23 of 150
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.
01What 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.
02Why 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.
03Real-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.
04Syntax
javapublic static void main(String[] args) { // args[0], args[1], etc. hold the values passed }
java ProgramName value1 value205Example 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!06Key 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.
The basics of args
What is the type of args, and how do you find out how many values were passed?
Show answerHide answer
args is a String[] (an array of String). args.length tells you how many arguments were passed.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?
Show answerHide answer
java Main Rahul prints Hello, Rahul!. java Main prints No name provided. (because args.length is 0).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?
Show answerHide 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.Compare with Scanner
Name two differences between command-line arguments and Scanner input.
Show answerHide answer
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().