in , ,

Java Command-Line Arguments

Java Command-Line Arguments
Java Command-Line Arguments

In this tutorial, we will learn about the Java command-line arguments with the help of examples.

The command-line arguments in Java allow us to pass arguments during the execution of the program.

As the name proposes arguments are passed through the command line.


Example: Command-Line Arguments

class Main {
  public static void main(String[] args) {
    System.out.println("Command-Line arguments are");

    // loop through all arguments
    for(String str: args) {
      System.out.println(str);
    }
  }
}

Let’s try to run this program using the command line.

  1. To compile the code
javac Main.java

2. To run the code

java Main

Presently assume we need to pass a few arguments while running the program, we can pass the arguments after the class name. For instance,

java Main apple ball cat

Here apple, ball, and cat are arguments passed to the program through the command line. Presently, we will get the accompanying output.

Command-Line arguments are
Apple
Ball
Cat

In the above program, the fundamental() strategy incorporates a variety of strings named args as its parameter.

public static void main(String[] args) {...}

The String array stores all the arguments passed through the command line.

Note: Arguments are constantly stored as strings and consistently by white-space.


Passing Numeric Command-Line Arguments

The fundamental() strategy for each Java program just accepts string arguments. Subsequently, it is not possible to pass numeric arguments through the command line.

However, we can later change over string arguments into numeric values.

Example: Numeric Command-Line Arguments

class Main {
  public static void main(String[] args) {

    for(String str: args) {
      // convert into integer type
    int argument = Integer.parseInt(str);
    System.out.println("Argument in integer form: " + argument);
    }

  }
}

Let’s try to run the program through the command line.

// compile the code
javac Main.java

// run the code
java Main 11 23

Here 11 and 23 are command-line arguments. Now, we will get the following output.

Arguments in integer form
11
23

In the above example, notice the line

int argument = Intege.parseInt(str);

Here, the parseInt() strategy for the Integer class changes over the string argument into a integer number.

Additionally, we can use the parseDouble() and parseFloat() technique to change over the string into double and float individually.

Note: If the arguments can’t be changed over into the predetermined numeric value then an exemption named NumberFormatException happens.


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

Java Wrapper Class

Java Wrapper Class

JavaScript Comments

JavaScript Comments