Java के Variable Arguments (varargs)
In this page:
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
// 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));
}
}
Login to try C/C++/Java/PHP code in the editor
Varargs Syntax Rules
एक method ज़्यादा से ज़्यादा एक varargs parameter declare कर सकता है, और यह signature में आखिरी parameter होना चाहिए, क्योंकि compiler को unambiguously जानना है कि fixed parameters कहां खत्म होते हैं और variable-length group कहां शुरू होता है।
उदाहरण: Varargs Syntax Rules
// 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);
}
}
Login to try C/C++/Java/PHP code in the editor
Varargs as Arrays
चूंकि varargs parameter असल में hood के नीचे बस एक array है, आप individual values list करने के बजाय एक मौजूदा array सीधे pass कर सकते हैं, और method दोनों तरीकों से identically behave करता है।
यह तब handy है जब values पहले से किसी दूसरे array या एक collection से convert की गई एक से आती हैं।
उदाहरण: Varargs as Arrays
// 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));
}
}
Login to try C/C++/Java/PHP code in the editor
Overloading Varargs Methods
आप उसी नाम के fixed-arity versions के साथ एक varargs method को overload कर सकते हैं, लेकिन compiler सबसे specific matching signature पसंद करता है, इसलिए ambiguous overloads (जैसे दो versions जो दोनों एक दिए call से match कर सकते हैं) चुपचाप एक चुनने के बजाय compile होने में fail होंगे।
उदाहरण: Overloading Varargs Methods
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));
}
}
Login to try C/C++/Java/PHP code in the editor
Empty Varargs
बिल्कुल कोई arguments न देकर एक varargs method call करना legal है; parameter method के अंदर अब भी एक array के रूप में मौजूद है, बस 0 की length के साथ।
Code जो मान लेता है कि array में हमेशा कम से कम एक element है उस case में ArrayIndexOutOfBoundsException throw करेगा, इसलिए explicitly एक empty array के खिलाफ guard करें।
उदाहरण: Empty Varargs
// 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());
}
}
Login to try C/C++/Java/PHP code in the editor
- Varargs parameter को पहले रखना, जब इसे आखिरी parameter होना चाहिए।
- एक method में दो varargs parameters declare करना, जो एक compile error है।
f(null)call करना और यह देखकर आश्चर्यचकित होना कि arraynullहै, इसलिए यहNullPointerExceptionthrow कर सकता है।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: