← Back to Advanced Java Course | Chapter 4: Modern Java Features | Lesson 2 of 12

Java का var Keyword

var Java को आपके दिए गए से variable का type पता लगाने देता है, जैसे एक label जो खुद को भर लेता है। आप कम लिखते हैं, लेकिन type फिर भी fixed रहता है।
Syntax
java
var variableName = value;  // type inferred by the compiler

Introduction to var

Java 10 में introduced var keyword, local variable type inference को सक्षम करता है, आपको एक explicit type declaration छोड़ने देता है जब भी compiler variable के initializer से type तय कर सके। Variable नीचे अब भी statically typed है — var Java को dynamically typed नहीं बनाता, यह सिर्फ आपको वह type लिखने से बचाता है जो compiler पहले से जानता है।

उदाहरण: Introduction to var

java
public class Main {
	public static void main(String[] args) {
		var message = "Inferred as String"; // compiler determines the type, still statically typed
		System.out.println(message);
	}
}

Var with Primitive Types

आप var का उपयोग standard primitive types के साथ भी कर सकते हैं, सिर्फ objects के साथ नहीं; Java आपके assign किए literal value से सीधे सही primitive type infer करता है, जैसे var x = 5 से int infer करना।

उदाहरण: Var with Primitive Types

java
public class Main {
	public static void main(String[] args) {
		var x = 5; // inferred as int
		var pi = 3.14; // inferred as double
		System.out.println(x + " " + pi);
	}
}

Var in Loops

var keyword loops के अंदर खासतौर पर सुविधाजनक है, जहां यह index counters और enhanced for-loop variables में verbose type declarations को सरल बना देता है, अन्यथा आसानी से पढ़े जाने वाले code में visual clutter घटाते हुए।

उदाहरण: Var in Loops

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Loop: repeat while the condition holds
		for (var i = 0; i < 3; i++) {
			// Print a line to the console
			System.out.println(i);
		}
	}
}

Invalid Uses of var

var keyword सिर्फ किसी method body या block के अंदर declared local variables के लिए उपयोग किया जा सकता है। इसे class fields, method parameters, या method return types के लिए उपयोग नहीं किया जा सकता, इन सभी को अब भी एक explicit, declared type चाहिए।

उदाहरण: Invalid Uses of var

java
public class Main {
	// var field; // invalid -- fields need an explicit type
	static int compute(int n) { // invalid to use var here as a parameter/return type too
		var result = n * 2; // valid: local variable inside a method body
		return result;
	}
	public static void main(String[] args) {
		System.out.println(compute(5));
	}
}

Best Practices with var

var का उपयोग तब करें जब initialized value पहले से ही variable के type को एक नज़र में स्पष्ट कर दे, जैसे var list = new ArrayList<String>()। इससे तब बचें जब ऐसा करना असली type को छुपा दे और code को किसी पढ़ने वाले के लिए समझना मुश्किल बना दे।

उदाहरण: Best Practices with var

java
import java.util.ArrayList;
public class Main {
	public static void main(String[] args) {
		var list = new ArrayList<String>(); // type is obvious at a glance -- good use
		list.add("clear");
		System.out.println(list);
	}
}
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. var x; या var x = null; लिखना, जो compile नहीं होता क्योंकि compiler के पास type infer करने के लिए कुछ नहीं है।
  2. var का उपयोग fields, method parameters या return types के लिए करना, जबकि इसकी अनुमति सिर्फ local variables के लिए है।
  3. var list = new ArrayList<>(); लिखना और ArrayList<Object> पाना, क्योंकि diamond के पास infer करने के लिए कोई type नहीं है।

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.