Learn How To Use Java Command Line Arguments With Examples
Java programming language is versatile in every aspect, being platform-independent it makes Java a clear cut winner for any developer. The execution of any Java program is hustle-free and precise. We can even pass arguments during the execution of a program using the command-line arguments. In this article, you will learn how you could use command-line arguments in Java. Following are the topics discussed in this article:
- What Are Command Line Arguments?
- Java Command Line Arguments Example
- Factorial Of A Number
- Sum Of Two Numbers
- Fibonacci Series
- Important Points To Remember
What Are Command Line Arguments?
The command-line arguments are passed to the program at run-time. Passing command-line arguments in a Java program is quite easy. They are stored as strings in the String array passed to the args parameter of the main() method in Java.
class Example0{
public static void main(String[] args){
System.out.println("edureka" + args[0]);
}
}
Output:
To compile and run a java program in the command prompt follow the steps written below.
- Save your program in a file with a .java extension
- open the command prompt and go to the directory where your file is saved.
- Run the command — javac filename.java
- After the compilation run the command — java filename
- Make sure the Java path is set correctly.
Java Command Line Arguments Example
Here are a few examples to show how we can use the command-line arguments in a Java program.
The beauty relies on the parseInt method in the Integer class. Every Number classes such as Integer, Float, Double and so on have parseXXX methods that convert String into the respective object of their type.
As we all know that array starts its index with zero. Therefore args[0] is the first index in this String[] array which is taken from the console. Similarly, args[1] is second, args[2] is the third element and so on.
When an application is launched, the run-time system passes the command-line arguments to the application’s main method via an array of Strings.
Factorial of a number using command-line arguments
class Example1{
public static void main(String[] args){
int a , b = 1;
int n = Integer.parseInt(args[0]);
for(a = 1; a<= n ; a++)
{
b = b*a;
}
System.out.println("factorial is" +b);
}
}
Output:
Sum of two numbers using command-line arguments
class Example2{
public static void main(String[] args){
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int sum = a + b;
System.out.println("The sum is" +sum);
}
}
Output:
Fibonacci Series program using command-line arguments
class Example3{
public static void main(String[] args){
int n = Integer.parseInt(args[0]);
int t1 = 0;
int t2 = 1;
for(int i = 1; i <=n; i++){
System.out.println(t1);
int sum = t1+t2;
t1 = t2;
t2 = sum;
}
}
}
Output:
Important Points To Remember
- While launching your application, you can use the command-line arguments to specify the configuration information.
- When you are using command-line arguments, there is no limitation on the number of arguments. You can use as many as per your requirement.
- Information in the command-line arguments is passed as Strings.
- The command-line arguments are stored in the String args of the main() method of the program.
This brings us to the end of this article where we have learned about the Java command line arguments with examples. I hope you are clear with all that has been shared with you in this tutorial.
This brings us to the end of this article where we have learned about the Java command line arguments with examples. I hope you are clear with all that has been shared with you in this tutorial.
If you wish to check out more articles on the market’s most trending technologies like Artificial Intelligence, Python, Ethical Hacking, then you can refer to Edureka’s official site.
Do look out for other articles in this series that will explain the various other aspects of Java.
1. Object Oriented Programming
5. Java String
6. Java Array
8. Java Threads
9. Introduction to Java Servlets
11. Exception Handling in Java
12. Java Tutorial
14. Java Programs
15. Kotlin vs Java
16. Dependency Injection Using Spring Boot
22. Socket Programming In Java
25. Library Management System Project in Java
26. Trees in Java
28. Top Data Structures & Algorithms in Java
30. Top 55 Servlet Interview Questions
34. Java Collections Interview Questions and Answers
35. How to Handle Deadlock in Java?
36. Top 50 Java Collections Interview Questions You Need to Know
37. What is the concept of String Pool in Java?
38. What is the difference between C, C++, and Java?
39. Palindrome in Java- How to check a number or string?
40. Top MVC Interview Questions and Answers You Need to Know
41. Top 10 Applications of Java Programming Language
42. Deadlock in Java
43. Square and Square Root in Java
Originally published at https://www.edureka.co on September 19, 2019.