Command Line Arguments In Java
No input statement needed in the program itself. we can pass the input as an argument while running the program. Sometimes you will want to pass information into a program when you run it. This is accomplished by passing command line arguments to main function. To access the command line arguments inside a java program is quite easy are stored as string in string array passed to main function.
Example program of command line arguments in java
class CommandLineExample
{
public static void main(String args[])
{
System.out.println("\nMy first input is :"+args[0]);
}
}
Addition of two numbers in java using command line arguments
{
public static void main(String args[])
{
int num1=Integer.parseInt(args[0]);
//type cast
int num2=Integer.parseInt(args[1]);
//type cast
System.out.println("Answer is :"+(num1+num2));
}Input is not given in this program. Instead two integer
variables are declared, and its type casted. The number
to be added at runtime is given then the output can be seen to be generated.
How to access two strings in command line arguments
write a program that accepts two strings as command line arguments in java
No input is given in this program.We give string array in main function.
Input is given while running the program using that string array.
Nice 👍👍👍
ReplyDelete👍
ReplyDelete