Java का var Keyword
In this page:
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
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);
}
}
Login to try C/C++/Java/PHP code in the editor
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
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);
}
}
Login to try C/C++/Java/PHP code in the editor
Var in Loops
var keyword loops के अंदर खासतौर पर सुविधाजनक है, जहां यह index counters और enhanced for-loop variables में verbose type declarations को सरल बना देता है, अन्यथा आसानी से पढ़े जाने वाले code में visual clutter घटाते हुए।
उदाहरण: Var in Loops
// 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);
}
}
}
Login to try C/C++/Java/PHP code in the editor
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
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));
}
}
Login to try C/C++/Java/PHP code in the editor
Best Practices with var
var का उपयोग तब करें जब initialized value पहले से ही variable के type को एक नज़र में स्पष्ट कर दे, जैसे var list = new ArrayList<String>()। इससे तब बचें जब ऐसा करना असली type को छुपा दे और code को किसी पढ़ने वाले के लिए समझना मुश्किल बना दे।
उदाहरण: Best Practices with var
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);
}
}
Login to try C/C++/Java/PHP code in the editor
var x;याvar x = null;लिखना, जो compile नहीं होता क्योंकि compiler के पास type infer करने के लिए कुछ नहीं है।varका उपयोग fields, method parameters या return types के लिए करना, जबकि इसकी अनुमति सिर्फ local variables के लिए है।var list = new ArrayList<>();लिखना औरArrayList<Object>पाना, क्योंकि diamond के पास infer करने के लिए कोई type नहीं है।
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: