Java की var Keyword
In this page:
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?
public class Main {
public static void main(String[] args) {
var age = 25; // compiler infers 'int' - still strongly typed
System.out.println(age);
}
}
Login to try C/C++/Java/PHP code in the editor
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
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);
}
}
Login to try C/C++/Java/PHP code in the editor
var with Collections
var ArrayList<String> या HashMap<String, Integer> जैसे verbose generic collection types के साथ विशेष रूप से उपयोगी है, क्योंकि यह declaration के दोनों sides पर वही लंबा type name दोहराने की ज़रूरत हटाता है।
उदाहरण: var with Collections
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);
}
}
Login to try C/C++/Java/PHP code in the editor
Restrictions on var
var सिर्फ उसी line पर एक initializer वाले local variables के लिए काम करता है, fields, method parameters, या return types के लिए उपयोग नहीं हो सकता, और इसका type infer होने के बाद एक अलग type की value को reassign नहीं किया जा सकता।
उदाहरण: Restrictions on var
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);
}
}
Login to try C/C++/Java/PHP code in the editor
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
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);
}
}
Login to try C/C++/Java/PHP code in the editor
- बिना initializer के
varउपयोग करना, जैसेvar x;, जो एक compile error है क्योंकि type infer नहीं किया जा सकता। var x = null;लिखना, जो fail होता है क्योंकिnullका कोई infer करने लायक type नहीं।- Fields या method parameters के लिए
varउपयोग करना, जो allowed नहीं; यह सिर्फ local variables के लिए काम करता है।
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: