Categories: Java

Command Line Argument in Java

The java command-line argument is a parameter that is passed to the java program when it is run.

The arguments passed from the console can be received and used as input in the Java program.

As a result, it provides a convenient way to check the program’s behavior for various values.

We can pass any number of arguments because the argument type is an array.

Example of command-line argument in java:
class CommandLineExample{  
public static void main(String args[]){  
System.out.println("Your first argument is: "+args[0]);  
}  
}  
  1. compile by > javac CommandLineExample.java
  2. run by > java CommandLineExample Coderzy
Output: Your first argument is: Coderzpy
Prints all the values using command-line argument:
class A{  
public static void main(String args[]){  
  
for(int i=0;i<args.length;i++)  
System.out.println(args[i]);  
  
}  
}  
  1. compile by > javac A.java
  2. run by > java A Coderzy JAVA-CODE 1 3 hello
Output: Coderzy
       JAVA-CODE
       1
       3
       hello
Java System.exit() Method:

The java.lang.System class contains the exit() method. The System class’s exit() method kills the current Java virtual machine on the system. The status code is passed as an argument to this method. This method is used to exit or terminate a program that is currently running. It can have a value of zero or non-zero. Successful termination is indicated by exit(0), while unsuccessful termination is indicated by exit(1) or exit(-1). There is no value returned by the exit() method.

Example:

import java.lang.*;  
public class SystemExitExample1 {  
  
    public static void main(String[] args) {  
        int a[]= {9,8,7,6,5,4,3,2,1};  
        for(int i=0;i<a.length;i++)  
        {  
            if(a[i]>5)  
            {  
            System.out.println("array["+i+"]="+a[i]);  
            }  
            else  
            {  
                System.out.println("terminating jvm,exiting");  
                System.exit(0);//Treminatejvm  
            }  
        }  
    }  
}  

Output:

array[0]=9
array[1]=8
array[2]=7
array[3]=6
terminating jvm,exiting

Note: also read about the Java object creation methods

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago

Binary Search Tree (BST)

A Binary Search Tree (BST) is a type of binary tree that satisfies the following…

1 year ago