×

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

How to encrypt password in Java

Every software program needs a username and password to identify a legitimate user. A username can be any number of things, including an email address or a string of characters....

6 minutes read.

Java String startsWith() method

Java String startsWith() method checks whether current String starts with given prefix or not . Syntax: public boolean startsWith(String prefix) public boolean startsWith(String prefix, int offset) Parameters: prefix : It is sequence of character Returns: It returns...

2 minutes read.

Minimum Number of Platforms Required for a Railway Station

The train station problem is one of the most significant problems typically posed in the programming round interview to gauge a candidate's aptitude for logic and problem-solving. Problem of Railway Station The...

6 minutes read.

How to Create Singleton Class in Java

In this tutorial, we will discuss the singleton class and how to create it. Introduction Java is a purely object-oriented programming language. It consists of classes and objects. But Singleton is one...

4 minutes read.

Thread States in Java

An Item's Life Cycle (Thread States) A thread can be in any of the states mentioned above at any given time in Java. They are as follows: New Active I'm blocked or waiting Temporary...

3 minutes read.

Java ArrayList

Java ArrayList Class A Java ArrayList class is a dynamic array which is used to store the elements. It is a part of collection framework. It implements the List Interface and inherits the...

12 minutes read.

Sealed Class in Java

What is a Sealed Class in Java? In programming, the two main issues that must be taken into account when creating an application are security and control flow. The use of...

5 minutes read.

Getter and Setter Method in Java Example

In Java programming, getter and setter methods are often employed. The values of class fields can be accessed and changed using Java's getter and setter methods. A private access specifier...

6 minutes read.

Longest Arithmetic Progression Sequence in Java

The task is to find the length of the largest sequence in an array to form an arithmetic progression. The array arr[] is given. Longest Arithmetic Progression Sequence in Java Algorithm set...

5 minutes read.

Java Boolean toString() Method

The toString() method of Java Boolean class returns a String corresponding to this Boolean object. It returns a string value “true”, if the defined object is true else it returns...

2 minutes read.

Getting Synchronized Set from Java HashSet

The synchronizedSet() technique for java.util.Collections class is utilized to return a synchronized (string safe) set supported by the predetermined set. To ensure sequential access, it is important that everything admittance...

4 minutes read.

Java Math atan() Method

The atan() method of Math class computes the trigonometric Arc Tangent (inverse of tangent ) of an angle. The value returned is between -pi/2 to pi/2. Syntax: public static double atan(double a) Parameters: The...

2 minutes read.

Volatile keyword in Java

Multiple threads can change a variable's value by using the volatile keyword. Making classes thread-safe is another application for it. It indicates that using a method or an instance of...

3 minutes read.

Bucket Sort in Java

Bucket Sort in JavaBucket sort is also called bin sort. Bucket sort first puts the elements of the array or list into different buckets. The first bucket contains elements of the smallest value. The...

11 minutes read.

Java Wrapper Class

Wrapper class in Java encapsulates a primitive type within an object. In other words, it is a unique mechanism of Java to convert primitive type in to object and object into primitive type....

3 minutes read.

Least Operator to Express Number in Java

In this article, we will learn about how to obtain a target number using a single number or a single integer by leveraging least operators in Java. There can be...

3 minutes read.

Centered Square Numbers in Java

In this tutorial, we will understand how to check the number is a centered square number. It is one of the prevalent interview questions of IT companies. Firstly, we will...

3 minutes read.

Java Math round() Method

The round() method of Java Math class returns a long or an int value that is closest to the argument and is rounded to positive infinity. Syntax: public static int round(float a)public...

2 minutes read.

What is Core Java?

The fundamental Java, which includes the fundamental idea of the Java programming language, is referred to as "Core Java." The definition of "Core" is the core idea of something. Core...

3 minutes read.

Fork Join in Java

Multithreaded processors are being introduced in new computer systems today. The operation is faster due to multicore CPUs. Therefore, it becomes essential for a programmer to leverage multithreaded processors effectively...

4 minutes read.