Command Line Arguments in Java offer us a way to provide the input data for a program while the program is running. Hence these arguments enable us to conveniently test the working of a program.
The following program demonstrates the use of command line arguments. Basically, the main() method has a parameter that is a string array. The arguments that the user provides at the command line are assigned to this string array with each element represent the corresponding argument in the specified order.
Hence, in the following example, the two arguments that we provide in command line will be stored in args[0], and args[1]. Since the arguments are strings so we need to convert them in integer. Further, the program computes the power of a number using these arguments.
class Main{
public static void main(String[] args) {
int n=Integer.parseInt(args[0]);
int m=Integer.parseInt(args[1]);
System.out.println("First Number (n): "+n);
System.out.println("Second Number (m): "+m);
int power=1;
for(int i=1;i<=m;i++)
{
power=power*i;
}
System.out.println(n +" raise to the power "+m+" is "+power);
}
}
Output
First Number (n): 6
Second Number (m): 8
6 raise to the power 8 is 40320
Further Reading
- AI
- Android
- Angular
- ASP.NET
- Augmented Reality
- AWS
- Bioinformatics
- Biometrics
- Blockchain
- Bootstrap
- C
- C#
- C++
- Cloud Computing
- Competitions
- Courses
- CSS
- Cyber Security
- Data Science
- Data Structures and Algorithms
- Data Visualization
- Datafication
- Deep Learning
- DevOps
- Digital Forensic
- Digital Trust
- Digital Twins
- Django
- Docker
- Dot Net Framework
- Drones
- Elasticsearch
- ES6
- Extended Reality
- Flutter and Dart
- Full Stack Development
- Git
- Go
- HTML
- Image Processing
- IoT
- IT
- Java
- JavaScript
- Kotlin
- Latex
- Machine Learning
- MEAN Stack
- MERN Stack
- Microservices
- MongoDB
- NodeJS
- PHP
- Power Bi
- Projects
- Python
- Quantum Computing
- React
- Robotics
- Rust
- Scratch 3.0
- Shell Script
- Smart City
- Software
- Solidity
- SQL
- SQLite
- Tecgnology
- Tkinter
- TypeScript
- VB.NET
- Virtual Reality
- Web Designing
- WebAssembly
- XML