← Back to Core Java Course | Chapter 5: Methods & Arrays | Lesson 6 of 10

Java के Variable Arguments (varargs)

Varargs एक method को एक जैसे तरह के किसी भी संख्या के inputs accept करने देते हैं, एक stretch होने वाला bag जैसा। Method के अंदर वे एक array की तरह behave करते हैं।
Syntax
java
static returnType methodName(dataType... values) {
    // values behaves like an array
}

methodName(a, b, c);

Introduction to Varargs

Varargs (variable arguments) एक method को उसी type के किसी भी संख्या में arguments accept करने देते हैं बिना caller को पहले एक array बनाने की ज़रूरत के, Type... name syntax उपयोग करते हुए।

Internally, Java method body चलने से पहले आपके pass किए किसी भी arguments को एक regular array में collect करता है।

उदाहरण: Introduction to Varargs

java
// Define the class Main
public class Main {
	static int sum(int... numbers) {
		// Declare total and set it to 0
		int total = 0;
		for (int n : numbers) {
			total += n;
		}
		// Send total back to the caller
		return total;
	}
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Print a line to the console
		System.out.println(sum(1, 2, 3));
	}
}

Varargs Syntax Rules

एक method ज़्यादा से ज़्यादा एक varargs parameter declare कर सकता है, और यह signature में आखिरी parameter होना चाहिए, क्योंकि compiler को unambiguously जानना है कि fixed parameters कहां खत्म होते हैं और variable-length group कहां शुरू होता है।

उदाहरण: Varargs Syntax Rules

java
// Define the class Main
public class Main {
	static void printAll(String label, int... numbers) {
		// Print to the console without a newline
		System.out.print(label + ": ");
		for (int n : numbers) {
			// Print to the console without a newline
			System.out.print(n + " ");
		}
	}
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		printAll("Scores", 90, 85, 77);
	}
}

Varargs as Arrays

चूंकि varargs parameter असल में hood के नीचे बस एक array है, आप individual values list करने के बजाय एक मौजूदा array सीधे pass कर सकते हैं, और method दोनों तरीकों से identically behave करता है।

यह तब handy है जब values पहले से किसी दूसरे array या एक collection से convert की गई एक से आती हैं।

उदाहरण: Varargs as Arrays

java
// Define the class Main
public class Main {
	static int sum(int... numbers) {
		// Declare total and set it to 0
		int total = 0;
		for (int n : numbers) {
			total += n;
		}
		// Send total back to the caller
		return total;
	}
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Declare values and set it to {4, 5, 6}
		int[] values = {4, 5, 6};
		// Print a line to the console
		System.out.println(sum(values));
	}
}

Overloading Varargs Methods

आप उसी नाम के fixed-arity versions के साथ एक varargs method को overload कर सकते हैं, लेकिन compiler सबसे specific matching signature पसंद करता है, इसलिए ambiguous overloads (जैसे दो versions जो दोनों एक दिए call से match कर सकते हैं) चुपचाप एक चुनने के बजाय compile होने में fail होंगे।

उदाहरण: Overloading Varargs Methods

java
public class Main {
	static int sum(int a, int b) {
		return a + b;
	}
	static int sum(int... numbers) {
		int total = 0;
		for (int n : numbers) {
			total += n;
		}
		return total;
	}
	public static void main(String[] args) {
		System.out.println(sum(1, 2)); // matches the fixed-arity version
		System.out.println(sum(1, 2, 3));
	}
}

Empty Varargs

बिल्कुल कोई arguments न देकर एक varargs method call करना legal है; parameter method के अंदर अब भी एक array के रूप में मौजूद है, बस 0 की length के साथ।

Code जो मान लेता है कि array में हमेशा कम से कम एक element है उस case में ArrayIndexOutOfBoundsException throw करेगा, इसलिए explicitly एक empty array के खिलाफ guard करें।

उदाहरण: Empty Varargs

java
// Define the class Main
public class Main {
	static int sum(int... numbers) {
		// Print a line to the console
		System.out.println("Length: " + numbers.length);
		// Declare total and set it to 0
		int total = 0;
		for (int n : numbers) {
			total += n;
		}
		// Send total back to the caller
		return total;
	}
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Print a line to the console
		System.out.println(sum());
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Varargs parameter को पहले रखना, जब इसे आखिरी parameter होना चाहिए।
  2. एक method में दो varargs parameters declare करना, जो एक compile error है।
  3. f(null) call करना और यह देखकर आश्चर्यचकित होना कि array null है, इसलिए यह NullPointerException throw कर सकता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.