0% found this document useful (0 votes)
3 views7 pages

Command Line Arguments in Java

Command-line arguments in Java are values passed to a program at startup, received as strings in the main method's String[] args parameter. Important points include that arguments are space-separated, require conversion for non-string types, and can be accessed using args.length. Various examples demonstrate how to use command-line arguments for different purposes, such as summing numbers and checking even or odd values.

Uploaded by

Dipali Gangarde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views7 pages

Command Line Arguments in Java

Command-line arguments in Java are values passed to a program at startup, received as strings in the main method's String[] args parameter. Important points include that arguments are space-separated, require conversion for non-string types, and can be accessed using args.length. Various examples demonstrate how to use command-line arguments for different purposes, such as summing numbers and checking even or odd values.

Uploaded by

Dipali Gangarde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

COMMAND LINE ARGUMENTS IN JAVA

What are Command Line Arguments?


Command-line arguments are values passed to a Java program when it starts running from
the command line (or terminal). These arguments are passed as strings to the main method
in the String[] args parameter.

Basic Syntax
In Java, the main method is defined as:
public static void main(String[] args)
Here:
• args is an array of strings (String[] args).
• Each argument entered in the command line is stored in this array

Important Points About Command Line Arguments

✔ Arguments are always received as strings → Need conversion if using numbers.


✔ They are space-separated → If an argument contains spaces, use quotes (e.g., "Hello
World").
✔ args.length gives the number of arguments.
✔ Use Integer.parseInt(), Double.parseDouble(), etc., for conversions.
Example 2: Sum of Two Numbers Passed as Arguments
Example 3: Checking Even or Odd from Command Line
Example 4: Passing Multiple Values (Concatenating Strings)
Example 5: Handling Boolean Arguments
Example 6: Handling Floating-Point Numbers

You might also like