Command Line Argument

Command Line Argument

Command Line Argument

Java command line argument allow you to pass arguments or parameters to your Java program when you run it from the command line. These arguments are passed as strings and can be accessed by your program using the “args” parameter of the “main” method.

Here’s an example of a Java program that takes command line arguments:

				
					public class Example {
    public static void main(String[] args) {
System.out.println("The following arguments were passed to the program:");
        for (String arg :args) {
System.out.println(arg);
        }
    }
}

				
			

In this example, the program simply prints out any arguments that were passed to it. To run this program with command line arguments, you would invoke the “java” command followed by the name of the class and the arguments you want to pass, separated by spaces:

				
					java Example arg1 arg2 arg3 
				
			

When you run the above command, the “main” method of the “Example” class will be executed, and the program will print out the following:

				
					The following arguments were passed to the program: 
arg1 
arg2 
arg3 

				
			

You can pass as many command line arguments as you want, and your program can access them using the “args” parameter of the “main” method. Note that the arguments are always passed as strings, so you may need to parse them into the appropriate data types if you need to use them in calculations or other operations.

 

 

Join To Get Our Newsletter
Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *