×

Java Command Line Arguments

Java command line arguments

Command line argument is an input or argument to the program when you run the program. In Java, we can take the inputs from the console, and it can be received in the Java program. These inputs are stored as a string array in the main method.

Naturally, it is just a method that was used until Scanner function is not introduced.

There is no restriction limit of Java command line inputs so that you can specify the n number of inputs.

See at the below example

Example

class CLexample{
     public static void main(String args[]){
         System.out.println("first argument= "+args[0]);
         System.out.println("Second argument= "+args[1]);
    }
}

Output

Java Command line 1

In the above example, I have passed x and y as arguments.

Command Line example for passing many arguments

Example2

class CLexample2{ 
public static void main(String arguments[]){ 
for(int i=0;i<arguments.length;i++) 
System.out.println(arguments[i]); 
  } 
}

Output

Java Command line 2

As we can see in the above example, I have passed many arguments. It concludes that we can pass more than one argument in a single command line.

Parse method

It is a method that takes String as user input and converts it into various forms like Integer, Float, and Double.

Types of parse method

There are three types of parse method.

  • parseInt();
  • parseDouble();
  • parseFloat();

The parseint() method uses the function Parseint(). It is a member function in Integer class.

The parseDouble() method uses the function ParseDouble(). It is a member function in Double class.

The parseFloat() method uses the function ParseFloat(). It is a member function in Float class.

All of the parse methods have the same syntax.

Syntax

Integer.parseInt(String s)
Float.parseFloat(String s)
Double.parseDouble(String s)

Example

class CLexample3
{  public static void main(String args[])
  {
  int a=Integer.parseInt(args[1]);
  int b=Integer.parseInt(args[0]);
   System.out.println(a+5);
   System.out.println(b+5);
}
}

Output

Java Command line 3

Related Topics

Armstrong Number Program in Java

Armstrong Number Program in Java: A positive number is called an Armstrong number if the sum of the cube of each digit is equal to the number itself. There are...

6 minutes read.

Java Math log() Method

The log() method of Math class returns the natural logarithmic value for the specified double argument. Syntax: public static double log(double a) Parameters: The parameter ‘a’ represents the value. Return Value: The log() method returns the...

1 minute read.

Java String substring() method

Java String substring() method returns a part of the String. Syntax: public String substring(int startIndex) public String substring(int startIndex, int endIndex) Parameters: startIndex : starting index endIndex : ending index Returns: It returns a specified String. Throws: StringIndexOutOfBoundsException if start...

2 minutes read.

String Coding Interview Questions in Java

What is String in Java?In Java, a String is a Class that is defined in the java.lang package. It isn't a basic data type like int or long. Character Strings...

5 minutes read.

How to Convert String to long in Java

How to Convert String to long in java It is used when you want to perform the mathematical operation on the String which contains long number then the conversion from String...

4 minutes read.

How to run Java Program in Eclipse

How to run Java Program in Eclipse In this section, we will learn how to write, save, compile, and execute or run a Java program in Eclipse. Eclipse is one of...

2 minutes read.

Find the Frequency of Each Element in the Array in Java

We may count the occurrence of each element in the array of items. Maintaining one array to store the counts of each array element is one strategy for solving this issue....

3 minutes read.

Java Characters

Normally, when we work with characters, we use primitive data types char. When we have to work with the objects of char, we use Character class. Character class has many important...

2 minutes read.

Java Integer shortValue() method

The shortValue() method of Java Integer class returns a short value for this Integer after a narrowing primitive conversion. Syntax public short shortValue () Parameters NA Overrrides: The shortValue () overrides : shortValue in class Number  Return Value This...

1 minute read.

How to Create a Package in Java?

For the most part, how to create a package in Java generally, a package is defined as a collection of relevant or irrelevant items together in a very major way....

7 minutes read.

Java Integer compare() method

The compare() method of Integer class compares the two specified int values. Syntax public static int compare(int x, int y) Parameters The parameters ‘x’ and ‘y’ represent the first and second int values to...

2 minutes read.

CopyOnWriteArrayList in Java

The CopyOnWriteArrayList is used to implement the List Interface. It is the improved version of ArrayList. The operations like add, remove, set, update etc, these operations are done by creating...

5 minutes read.

Anagram Program in Java using String

Anagram Program in Java Anagrams are those words that are generated by rearrangement of the letters of another phrase or word. Usually, while doing the rearrangement, the original letters are used...

8 minutes read.

How to Convert String to char in Java

How to Convert String to char in Java There are two methods to convert String to char are: Using charAt() method Using tocharArray() method Using charAt() method This is the method of String class that...

3 minutes read.

Difference Between Data Hiding and Abstraction in Java

Abstraction: Data Abstraction and Data Hiding ideas are utilised to show the expected data to the end client and conceal the superfluous subtleties, however, for specific purposes like decreasing the framework's...

10 minutes read.

Radix Sort in Java

Radix Sort in Java Radix sort in Java uses the digits of the numbers given in the sorted array to sort the numbers. The radix sort uses the place value of...

5 minutes read.

House Numbers in Java

In this section, we will discuss about house number in Java. It is a sum of cubes, each of which has a dimension of h + 1. There is a...

3 minutes read.

Convert Integer to Roman Numerals in Java

The main objective of this article is to convert the integers that are decimal values to the roman numbers. Problem statement: Write a software/program/code to convert any integer to a roman number. You...

10 minutes read.

Interleaving string in Java

If the string Str3 contains all of the characters from Str1 and Str2, it is considered interleaving Str1 and Str2. Keep in mind that the order of all characters in...

5 minutes read.

Parallel Arrays Sort in Java

Sorting is a technique of arranging a sequence of numbers in ascending order, that is, the first number being lowest and gradually incrementing the numbers such that the last number...

7 minutes read.