Using varargs in methods In Hindi

Using varargs in methods In Hindi

जावा प्रोग्रामिंग लैंग्वेज में Varargs (वेरिएबल-लेंथ आर्गुमेंट्स) जावा प्रोग्रामर को मौजूदा प्रोग्राम में फंक्शन मेथड को ओवरलोड किए बिना या कई प्रोग्राम वेरिएबल पैरामीटर लिस्ट क्रिएट किए बिना, यूजर डिफाइन फंक्शन मेथड में मल्टीप्ल नम्बर्स में डिक्लेअर पैरामीटर आर्गुमेंट्स वैल्यू को पास करने में हेल्प करते हैं। जावा प्रोग्राम में Varargs आर्ग्यूमेंट्स स्पेशल आर्डर में तब यूज़ किए जाते हैं, जब क्रिएटेड प्रोग्राम में डिक्लेअर आर्गुमेंट्स नंबर्स पहले से डिसाइड नहीं होते है।

Using varargs in methods In Hindi

How Varargs Arguments Work in Java Functions.

जब जावा डेवलपर किसी फंक्शन प्रोग्राम में varargs आर्ग्यूमेंट्स का यूज़ करते हैं, तो यहाँ प्रोग्राम में डिक्लेअर पैरामीटर आर्गुमेंट को एक ऐरे के रूप में ट्रीट किया जाता है, जबकि प्रोग्राम में डिक्लेअर फंक्शन मेथड को कॉल करते समय जावा प्रोग्रामर को क्लेअरली ऐरे को क्रिएट या पास करने की आवश्यकता नहीं होती है। इस प्रोसेस में जावा इन डिक्लेअर पैरामीटर को आर्ग्यूमेंट्स को आटोमेटिक मैनेज या हैंडल कर सकता है।

Varargs syntax in Java.

जावा फंक्शन प्रोग्राम में पैरामीटर Varargs मेथड को सिग्नेचर में पैरामीटर के नाम से पहले तीन डॉट्स (…) का यूज़ करके डिक्लेयर या डिफाइन कर सकते है ।

<return_type> <method_name>(<parameter_type>… <parameter _name>) {

        // java function method argument body

    }

याद रहे, जावा फंक्शन प्रोग्राम में डिक्लेअर Varargs फंक्शन आर्गुमेंट ऑलवेज मेथड पैरामीटर लिस्ट में लास्ट पैरामीटर के रूप में डिफाइन होने चाहिए। जहा varargs प्रोग्राम में एक मेथड में सिर्फ़ एक varargs पैरामीटर डिक्लेअर हो सकता है।

Example of Java Parameter Varargs.

तो चलिए यहाँ हम एक ऐसा प्रोग्राम क्रिएट करते हैं, जिसमे यूजर डिक्लेअर एक फंक्शन प्रोग्राम मेथड अलग-अलग न्यूमेरिक इंटीजर वैल्यू को इनपुट करता है, और अंत में इन वैल्यूज को ऐड कर डिस्प्ले करता है.

Example of adding numbers to a method with Varargs in a Java function.

public class Main

{

    // here we implement varargs Method to total a number of given integers

    public static int totalInteger(int… integers) {

        int total = 0;

        for (int integer : integers) {

            total += integer;  // here it Add each integer value to the total variable

        }

        return total;

    }

    public static void main(String[] args) {

        // here it Calling the totalInteger method with different numbers of arguments

        System.out.println(“\n Here the total of 7, 10, 4 integers – ” + totalInteger(7, 10, 4));  //  Output is – 21

        System.out.println(“\n Here the total of 5, 6, 11, 33 integers – ” + totalInteger(5, 6, 11, 33));  // Output is – 55

        System.out.println(“\n Here the total of integers – ” + totalInteger());  // Output is – 0

    }

}

Explaining varargs in a Java function.

  • यहाँ इस प्रोग्राम में varargs totalInteger(int… integers) फंक्शन मेथड मल्टीप्ल इंडिविजुअल इंटीजर नंबर वैल्यू को इनपुट करता है।
  • जहा फंक्शन मेथड बॉडी में, integers वैल्यू को एक ऐरे (टाइप int[]) के रूप में ट्रीट किया जाता है, और यहाँ इसे एक बेहतर for लूप को यूज़ करके प्रोग्राम में लूप कर सकते हैं।
  • फंक्शन मेन मेथड में, प्रोग्रामर मल्टीप्ल इन्टिज नंबर के आर्गुमेंट के साथ totalInteger() यूजर डिफाइन varargs फंक्शन मेथड को कॉल करते हैं।

Some facts about the Java Varargs function method.

Varargs must be last – याद रहे, यदि जावा प्रोग्रामर के पास किसी दूसरे फंक्शन पैरामीटर (non-varargs) पैरामीटर एलिमेंट मौजूद हैं, तो फंक्शन मेथड सिग्नेचर में varargs पैरामीटर सबसे लास्ट में डिफाइन होने चाहिए।

public static void testMethod(int p, String… strings) {

        // here it valid varags methdod

    }

    // here This will cause a program compilation error

    // public static void testMethod(String… strings, int p) {

    //  invalid declaration define, here varargs function argument must be the last function parameter element

    // }

Varargs is considered an array – यहाँ यूजर डिफाइन फंक्शन मेथड के अंदर, varargs फंक्शन पैरामीटर को एक जावा प्रोग्राम ऐरे के रूप में ट्रीट किया जाता है। इसका अर्थ है कि जावा यूजर varargs एलिमेंट को इंडेक्सिंग और फॉर लूप इटरेशन जैसे ऐरे मेनूप्लेशन ऑपरेशन को सिंपल डिफाइन कर सकते हैं।

Multiple Varargs calls – यदि जावा यूजर प्रोग्राम में कोई आर्गुमेंट वैल्यू पास नहीं करते हैं, तो यहाँ varargs फंक्शन पैरामीटर को एक एम्प्टी array (new int[0]) के रूप में ट्रीट किया जाता है। जहा varargs फंक्शन पैरामीटर के लिए बिना आर्गुमेंट वाले फंक्शन मेथड को कॉल करना वैलिड होता है।

Varargs and Method Overloading – जावा फंक्शन प्रोगाम में Varargs आर्गुमेंट्स का यूज़ फंक्शन मेथड ओवरलोडिंग में किया जाता है, लेकिन ये ऑपरेशन परफॉर्म करते समय केयरफुल रहे। यदि किसी यूजर डिफाइन फंक्शन मेथड में varargs पैरामीटर वेरिएबल है, और किसी दूसरे फंक्शन मेथड में ऐसा पैरामीटर डिफाइन है. जो varargs आर्ग्यूमेंट्स से प्रॉपर मैच कर सकता है. जैसे, किसी ऐरे या पैरामीटर्स की एक स्पेशल नंबर्स), तो इससे varargs आर्गुमेंट में कन्फ्यूजन क्रिएट हो सकता है।

Example of Java varargs arguments with other data types.

जावा प्रोग्रामर varargs फंक्शन पैरामीटर आर्ग्यूमेंट्स को यूज़ करके दूसरे अन्य प्रकार के डेटा टाइप्स, जैसे String, double, char, आदि के साथ भी varargs आर्ग्यूमेंट्स को प्रोग्राम में यूज़ कर सकते हैं।

Varargs parameter arguments example of the Strings data type in Java programming.

public class Main

{

    // here we use displayStrings Method to print multiple individual strings information

    public static void displayStrings(String… strings) {

        for (String strng : strings) {

            System.out.println(strng);

        }

    }

    public static void main(String[] args) {

        displayStrings(“\n Vcanhelpsu”, “Java”, “Python”, “Funtion method”, “Varargs parameter”);  // here we defne Multiple individual strings

        displayStrings(“\n Simple string display”);  // here it display Single string text info

        displayStrings();  // here No strings method call like (empty string method call)

    }

}

Varargs function parameter example with doubles data type.

public class Main

{

// here we use countAverage Method to calculate the average of multiple double integers number values

    public static double countAverage(double… integers) {

        if (integers.length == 0) {

            return 0.0;  // here No integers values passed, and it return 0 values

        }

        double total = 0;

        for (double integer : integers) {

            total += integer;

        }

        return total / integers.length;

    }

    public static void main(String[] args) {

        System.out.println(“\n Here the Average of integer 7.3, 9.2, 3.8, 11.4 – ” + countAverage(7.3, 9.2, 3.8, 11.4));  // Output is – 7.9

        System.out.println(“\n Here the Average of integer 25.9, 37.4, 17.5, 22.7 – ” + countAverage(25.9, 37.4, 17.5, 22.7));  // Output is – 25.87

        System.out.println(“\n Here the Average of integer no integers – ” + countAverage());  // Output is – 0.0

    }

}

Leave a Reply