← Back to Core Java Course | Chapter 1: Introduction & Basics | Lesson 10 of 12

Java के Variables

एक variable एक labeled box जैसा है जहां आप information का एक टुकड़ा रखते हैं। आप Java को बताते हैं box में किस तरह की चीज़ जाती है, इसे एक नाम देते हैं, और बाद में इसके अंदर क्या है यह बदल सकते हैं।
Syntax
java
dataType variableName = value;
dataType a, b, c;
variableName = newValue;

Variable Declaration

एक variable declare करना (जैसे int age;) इसके type के अनुसार sized memory में एक slot reserve करता है, लेकिन वह slot तब तक garbage या एक default value रखता है जब तक आप असल में इसे कुछ assign न करें।

उदाहरण: Variable Declaration

java
public class Main {
	public static void main(String[] args) {
		int age; // declared - not yet assigned a meaningful value
		age = 25; // now assigned
		System.out.println(age);
	}
}

Variable Initialization

Declaration और assignment को एक line में combine करना, जैसे int age = 25;, सबसे आम pattern है और किसी variable को meaningful value दिए जाने से पहले गलती से उपयोग करने के risk से बचाता है।

उदाहरण: Variable Initialization

java
public class Main {
	public static void main(String[] args) {
		int age = 25; // declaration and assignment combined
		System.out.println(age);
	}
}

Modifying Variables

Reassignment बस उसी memory slot में एक नई value लिखता है, जब तक नई value का type match करे (या variable के declared type में automatically convert हो सके)।

उदाहरण: Modifying Variables

java
public class Main {
	public static void main(String[] args) {
		int age = 25;
		age = 26; // reassigned - new value must match the declared type
		System.out.println(age);
	}
}

Multiple Variables

int x = 1, y = 2, z = 3; लिखना एक statement में तीन अलग int variables declare करता है -- closely related values के लिए convenient, हालांकि clarity अक्सर unrelated वालों के लिए अलग lines पसंद करती है।

उदाहरण: Multiple Variables

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		int x = 1, y = 2, z = 3;
		// Print a line to the console
		System.out.println(x + " " + y + " " + z);
	}
}

Variable Scope

Local variables सिर्फ उस method या block के अंदर रहते हैं जहां वे declared हैं और इसके return होने पर गायब हो जाते हैं, जबकि instance variables (class level पर declared) object की lifetime के लिए persist करते हैं और उस class में हर method को visible हैं।

उदाहरण: Variable Scope

java
public class Main {
	int instanceVar = 100; // lives as long as the object exists

	void showLocal() {
		int localVar = 5; // exists only inside this method
		System.out.println(localVar);
	}

	public static void main(String[] args) {
		new Main().showLocal();
	}
}
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. एक local variable को initialize किए बिना उपयोग करना, जो might not have been initialized compile error है।
  2. उसी scope में उसी variable को दो बार declare करना, जैसे int x = 1; int x = 2;।
  3. किसी variable को इसके declared block के बाहर उपयोग करना, जैसे loop के बाद एक loop variable।

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.