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

Java की var Keyword

var keyword compiler को किसी local variable का type उसके initializer से infer करने देता है, boilerplate घटाते हुए जबकि variable नीचे से पूरी तरह strictly typed रहता है।
Syntax
java
var variableName = value;  // type inferred from the value

What is var?

Java 10 में introduced var keyword compiler को किसी local variable का type इसे assign की गई value से infer करने देता है, इसलिए आप type explicitly लिखना skip कर सकते हैं जबकि variable नीचे strongly typed रहता है।

उदाहरण: What is var?

java
public class Main {
	public static void main(String[] args) {
		var age = 25; // compiler infers 'int' - still strongly typed
		System.out.println(age);
	}
}

Type Inference with var

var के साथ Type inference compile time पर assignment के right-hand side पर expression examine करता है और permanently वही type variable को assign करता है, चाहे यह एक int, double, boolean, या कोई और type हो।

उदाहरण: Type Inference with var

java
public class Main {
	public static void main(String[] args) {
		var price = 19.99;   // inferred as double, permanently
		var name = "Java";   // inferred as String, permanently
		System.out.println(price + " " + name);
	}
}

var with Collections

var ArrayList<String> या HashMap<String, Integer> जैसे verbose generic collection types के साथ विशेष रूप से उपयोगी है, क्योंकि यह declaration के दोनों sides पर वही लंबा type name दोहराने की ज़रूरत हटाता है।

उदाहरण: var with Collections

java
import java.util.ArrayList;
public class Main {
	public static void main(String[] args) {
		var names = new ArrayList<String>(); // instead of ArrayList<String> names = new ArrayList<String>();
		names.add("Alice");
		System.out.println(names);
	}
}

Restrictions on var

var सिर्फ उसी line पर एक initializer वाले local variables के लिए काम करता है, fields, method parameters, या return types के लिए उपयोग नहीं हो सकता, और इसका type infer होने के बाद एक अलग type की value को reassign नहीं किया जा सकता।

उदाहरण: Restrictions on var

java
public class Main {
	// var field; // ILLEGAL - cannot use var for a class field
	public static void main(String[] args) {
		var count = 1;
		count = 2;      // OK - still an int
		// count = "two"; // ILLEGAL - cannot reassign to a different type
		System.out.println(count);
	}
}

When to Use var

var सबसे उपयोगी है जब type पहले से assignment के right-hand side से obvious हो, जैसे new ArrayList<String>(), लेकिन एक explicit type अक्सर clearer होता है जब एक bare literal या method call यह reveal नहीं करती कि value क्या represent करती है।

उदाहरण: When to Use var

java
import java.util.ArrayList;
public class Main {
	public static void main(String[] args) {
		var list = new ArrayList<String>(); // clear from the right-hand side
		int total = 5; // explicit type clearer than a bare literal with var
		System.out.println(list.size() + total);
	}
}
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. बिना initializer के var उपयोग करना, जैसे var x;, जो एक compile error है क्योंकि type infer नहीं किया जा सकता।
  2. var x = null; लिखना, जो fail होता है क्योंकि null का कोई infer करने लायक type नहीं।
  3. Fields या method parameters के लिए var उपयोग करना, जो allowed नहीं; यह सिर्फ local variables के लिए काम करता है।

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.