Command Line Arguments

Introduction

Command Line Arguments वे values होती हैं जो Java program को run करते समय command prompt या terminal से दी जाती हैं। Java में command line arguments main() method के अंदर लिखे गए String[] args में store होते हैं।

Command line arguments की help से user program को run करते समय input दे सकता है। इसके लिए program में input लेने के लिए Scanner class की जरूरत नहीं पड़ती।

Simple Definition

Command line arguments are the values passed to a Java program at the time of execution through command prompt or terminal.

Hindi Meaning

Command line arguments वे values होती हैं जो Java program को run करते समय command prompt या terminal से दी जाती हैं।

Syntax of main Method

public static void main(String[] args)

इस syntax में String[] args command line arguments को store करता है। यहाँ args एक string array है।

Meaning of String[] args

String[] args का मतलब है string type का array। जब user command line से values pass करता है, तो वे values args array में store हो जाती हैं।

Example

java Demo Rahul BCA

इस command में:

args[0] = Rahul
args[1] = BCA

How to Pass Command Line Arguments

सबसे पहले Java program को compile किया जाता है। उसके बाद program को run करते समय class name के बाद values लिखी जाती हैं।

Compile Command

javac Demo.java

Run Command with Arguments

java Demo Rahul BCA

यहाँ Rahul और BCA command line arguments हैं।

Simple Command Line Argument Program

class Demo {
    public static void main(String[] args) {
        System.out.println("First Argument: " + args[0]);
        System.out.println("Second Argument: " + args[1]);
    }
}

Run Command

java Demo Rahul BCA

Output

First Argument: Rahul
Second Argument: BCA

Explanation

इस program में command line से दो values pass की गई हैं। पहली value args[0] में और दूसरी value args[1] में store होती है।

Important Point

Command line arguments हमेशा string form में store होते हैं। अगर user number pass करता है, तब भी वह string के रूप में store होता है। Calculation करने के लिए string को number में convert करना पड़ता है।

Example: Print Name Using Command Line Argument

class NameDemo {
    public static void main(String[] args) {
        System.out.println("Hello " + args[0]);
    }
}

Run Command

java NameDemo Amit

Output

Hello Amit

Example: Print Student Details

class StudentInfo {
    public static void main(String[] args) {
        System.out.println("Name: " + args[0]);
        System.out.println("Course: " + args[1]);
        System.out.println("Semester: " + args[2]);
    }
}

Run Command

java StudentInfo Rahul BCA First

Output

Name: Rahul
Course: BCA
Semester: First

Command Line Arguments with Numbers

Command line से pass की गई values string होती हैं। इसलिए numbers पर calculation करने के लिए उन्हें integer या double में convert करना पड़ता है।

Java में string को integer में convert करने के लिए Integer.parseInt() method use किया जाता है।

Example: Addition Using Command Line Arguments

class AddNumbers {
    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("Sum = " + sum);
    }
}

Run Command

java AddNumbers 10 20

Output

Sum = 30

Explanation

इस program में args[0] और args[1] string form में values store करते हैं। Integer.parseInt() method इन strings को integer में convert करता है। फिर दोनों numbers का addition किया जाता है।

Example: Multiplication Using Command Line Arguments

class MultiplyNumbers {
    public static void main(String[] args) {
        int a = Integer.parseInt(args[0]);
        int b = Integer.parseInt(args[1]);

        System.out.println("Multiplication = " + (a * b));
    }
}

Run Command

java MultiplyNumbers 5 6

Output

Multiplication = 30

Example: Double Values Using Command Line Arguments

String को double में convert करने के लिए Double.parseDouble() method use किया जाता है।

class AverageDemo {
    public static void main(String[] args) {
        double marks1 = Double.parseDouble(args[0]);
        double marks2 = Double.parseDouble(args[1]);
        double average = (marks1 + marks2) / 2;

        System.out.println("Average = " + average);
    }
}

Run Command

java AverageDemo 80.5 90.5

Output

Average = 85.5

args.length

args.length command line arguments की total संख्या बताता है।

Example

class CountArguments {
    public static void main(String[] args) {
        System.out.println("Total Arguments: " + args.length);
    }
}

Run Command

java CountArguments Java BCA Student

Output

Total Arguments: 3

Display All Command Line Arguments

class DisplayArguments {
    public static void main(String[] args) {
        for (int i = 0; i < args.length; i++) {
            System.out.println("Argument " + i + ": " + args[i]);
        }
    }
}

Run Command

java DisplayArguments Java Programming BCA

Output

Argument 0: Java
Argument 1: Programming
Argument 2: BCA

Explanation

इस program में for loop की help से command line से pass किए गए सभी arguments print किए गए हैं।

Command Line Arguments और Scanner में Difference

Command Line ArgumentsScanner
Input program run करते समय दिया जाता है।Input program run होने के बाद दिया जाता है।
Values String[] args में store होती हैं।Values Scanner object से read होती हैं।
सभी values string form में होती हैं।Different data types directly read किए जा सकते हैं।
Small programs के लिए useful है।Interactive programs के लिए useful है।

Advantages of Command Line Arguments

  • Program run करते समय input दिया जा सकता है।
  • Simple और fast method है।
  • Scanner class की जरूरत नहीं पड़ती।
  • Testing के लिए useful है।
  • Multiple values एक साथ pass की जा सकती हैं।

Limitations of Command Line Arguments

  • सभी values string form में store होती हैं।
  • Numeric calculation के लिए type conversion जरूरी है।
  • User-friendly input method नहीं है।
  • Arguments न देने पर error आ सकता है।
  • Complex input के लिए Scanner ज्यादा useful होता है।

Common Error: ArrayIndexOutOfBoundsException

अगर program में args[0] use किया गया है लेकिन run command में कोई argument pass नहीं किया गया, तो error आएगा।

Example

class ErrorDemo {
    public static void main(String[] args) {
        System.out.println(args[0]);
    }
}

Wrong Run Command

java ErrorDemo

Error

ArrayIndexOutOfBoundsException

Correct Run Command

java ErrorDemo Hello

Output

Hello

Safe Program Using args.length

class SafeArgument {
    public static void main(String[] args) {
        if (args.length > 0) {
            System.out.println("Argument: " + args[0]);
        } else {
            System.out.println("No argument provided");
        }
    }
}

Run Command

java SafeArgument

Output

No argument provided

Run Command with Argument

java SafeArgument Java

Output

Argument: Java

Exam-Oriented Short Answer

Q. What are command line arguments in Java?

Command line arguments वे values होती हैं जो Java program को run करते समय command prompt या terminal से pass की जाती हैं। ये values main() method के String[] args array में store होती हैं। Command line arguments हमेशा string form में होते हैं। अगर numeric calculation करनी हो, तो इन्हें Integer.parseInt() या Double.parseDouble() methods की help से convert करना पड़ता है।

Important Points to Remember

  • Command line arguments program run करते समय pass किए जाते हैं।
  • ये arguments String[] args में store होते हैं।
  • args एक string array है।
  • First argument args[0] में store होता है।
  • सभी command line arguments string form में होते हैं।
  • Numeric values के लिए type conversion जरूरी है।
  • args.length total arguments count बताता है।
  • Arguments न देने पर ArrayIndexOutOfBoundsException आ सकता है।

Practice Questions

Short Answer Questions

  • Command line arguments क्या होते हैं?
  • String[] args का क्या use है?
  • args.length क्या बताता है?
  • Command line arguments और Scanner में अंतर लिखिए।
  • Command line argument को integer में कैसे convert करते हैं?
  • ArrayIndexOutOfBoundsException कब आती है?

Long Answer Question

Command line arguments को Java program के example के साथ explain कीजिए।

Homework Program

Command line arguments की help से दो numbers input लेकर उनका sum, subtraction और multiplication print करने का Java program लिखिए।

Hint

class CalculatorArgs {
    public static void main(String[] args) {
        int a = Integer.parseInt(args[0]);
        int b = Integer.parseInt(args[1]);

        System.out.println("Sum = " + (a + b));
        System.out.println("Subtraction = " + (a - b));
        System.out.println("Multiplication = " + (a * b));
    }
}

Run Command

java CalculatorArgs 20 10

Output

Sum = 30
Subtraction = 10
Multiplication = 200

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top