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

Command Line Arguments in JAVA

Java command-line arguments are inputs passed during the execution of a program, allowing users to test different behaviors. These arguments are received in the main() method as a string array, enabling the program to handle various data types. An example demonstrates how to access and print the first command-line argument using the args[] array.

Uploaded by

k58462549
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)
10 views

Command Line Arguments in JAVA

Java command-line arguments are inputs passed during the execution of a program, allowing users to test different behaviors. These arguments are received in the main() method as a string array, enabling the program to handle various data types. An example demonstrates how to access and print the first command-line argument using the args[] array.

Uploaded by

k58462549
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/ 2

Java 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.

So, 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 users can pass the arguments during the execution bypassing the command-
line arguments inside the main() method.

Working command-line arguments

We need to pass the arguments as space-separated values.


We can pass both strings and primitive data types(int, double, float, char, etc) as command-
line arguments. These arguments convert into a string array and are provided to the main()
function as a string array argument.
Internally, JVM wraps up these command-line arguments into the args[ ] array that we pass
into the main() function.
We can check these arguments using args.length method.
JVM stores the first command-line argument at args[0], the second at args[1], the third at
args[2], and so on.

Simple example of command-line argument in java


In this example, we are receiving only one argument and printing it. To run
this java program, you must pass at least one argument from the command
prompt.
1. class CommandLineExample{
2. public static void main(String args[]){
3. System.out.println("Your first argument is: "+args[0]);
4. }
5. }
1. compile by > javac CommandLineExample.java
2. run by > java CommandLineExample sonoo

Output: Your first argument is: sonoo

Example:2
Parsing String in java is known as converting data in the String
format from a file, user input, or a certain network. Parsing String is
the process of getting information that is needed in the String
format. String parsing in java can be done by using a wrapper class.

You might also like