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

Java Method के Parameters

Parameters किसी method में खाली slots हैं जिन्हें आप इसे हर बार उपयोग करते समय भरते हैं, एक recipe में कितने eggs चाहिए इस blank जैसा। Java method को आपकी दी value की एक copy देता है।
Syntax
java
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

java
// 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");
	}
}

Multiple Parameters

Multiple parameters method signature में commas से अलग होते हैं, और call site पर आप जो arguments pass करते हैं वे strictly position से match होते हैं, नाम से नहीं।

उदाहरण: Multiple Parameters

java
// 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));
	}
}

Pass by Value

चूंकि primitives value से pass होते हैं, method एक independent copy के साथ काम करता है -- method के अंदर उस copy को reassign या modify करने का caller के original variable पर zero effect है।

उदाहरण: Pass by Value

java
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
	}
}

Object Parameters

Object references (arrays सहित) भी value से pass होते हैं, लेकिन वह value reference खुद है -- इसलिए जबकि आप caller के variable को कहीं और point नहीं करा सकते, आप अब भी उस object को mutate कर सकते हैं जिसकी ओर यह point करता है, और caller उन changes को देखेगा।

उदाहरण: Object Parameters

java
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]);
	}
}

Final Parameters

एक parameter को final मार्क करना method body को इसे एक अलग value reassign करने से रोकता है, जो accidental overwrites पकड़ सकता है और readers को signal करता है कि parameter की original value पूरे method में untouched रहनी है।

उदाहरण: Final Parameters

java
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);
	}
}
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. यह उम्मीद करना कि एक method caller का primitive बदलेगा, जब Java value की एक copy pass करता है।
  2. Arguments को गलत order में या गलत type के pass करना, जो एक compile error है।
  3. यह उम्मीद करना कि एक reassigned object parameter caller का variable बदलेगा, जब सिर्फ reference copy बदली जाती है।

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.