Java Method के Parameters
static void methodName(dataType param1, dataType param2) {
// body
}
methodName(arg1, arg2);
Single Parameter
एक parameter method तक scoped एक local variable जैसा काम करता है, method invoke करते समय caller जो भी value argument के रूप में supply करता है उससे अपने आप initialized।
उदाहरण: Single Parameter
// Define the class Main
public class Main {
static void greet(String name) {
// Print a line to the console
System.out.println("Hello, " + name);
}
// Program entry point: the JVM starts running here
public static void main(String[] args) {
greet("Alice");
}
}
Login to try C/C++/Java/PHP code in the editor
Multiple Parameters
Multiple parameters method signature में commas से अलग होते हैं, और call site पर आप जो arguments pass करते हैं वे strictly position से match होते हैं, नाम से नहीं।
उदाहरण: Multiple Parameters
// Define the class Main
public class Main {
static int add(int a, int b) {
// Send a + b back to the caller
return a + b;
}
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Print a line to the console
System.out.println(add(3, 5));
}
}
Login to try C/C++/Java/PHP code in the editor
Pass by Value
चूंकि primitives value से pass होते हैं, method एक independent copy के साथ काम करता है -- method के अंदर उस copy को reassign या modify करने का caller के original variable पर zero effect है।
उदाहरण: Pass by Value
public class Main {
static void modify(int x) {
x = 100;
}
public static void main(String[] args) {
int num = 5;
modify(num);
System.out.println(num); // still 5
}
}
Login to try C/C++/Java/PHP code in the editor
Object Parameters
Object references (arrays सहित) भी value से pass होते हैं, लेकिन वह value reference खुद है -- इसलिए जबकि आप caller के variable को कहीं और point नहीं करा सकते, आप अब भी उस object को mutate कर सकते हैं जिसकी ओर यह point करता है, और caller उन changes को देखेगा।
उदाहरण: Object Parameters
public class Main {
static void changeFirst(int[] arr) {
arr[0] = 99; // mutates caller's array
}
public static void main(String[] args) {
int[] numbers = {1, 2, 3};
changeFirst(numbers);
System.out.println(numbers[0]);
}
}
Login to try C/C++/Java/PHP code in the editor
Final Parameters
एक parameter को final मार्क करना method body को इसे एक अलग value reassign करने से रोकता है, जो accidental overwrites पकड़ सकता है और readers को signal करता है कि parameter की original value पूरे method में untouched रहनी है।
उदाहरण: Final Parameters
public class Main {
static void printValue(final int x) {
System.out.println(x);
// x = 10; // would not compile
}
public static void main(String[] args) {
printValue(42);
}
}
Login to try C/C++/Java/PHP code in the editor
- यह उम्मीद करना कि एक method caller का primitive बदलेगा, जब Java value की एक copy pass करता है।
- Arguments को गलत order में या गलत type के pass करना, जो एक compile error है।
- यह उम्मीद करना कि एक reassigned object parameter caller का variable बदलेगा, जब सिर्फ reference copy बदली जाती है।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: