Java Keywords और Identifiers
Keywords
class, int, if, और return जैसे Keywords language से खुद reserved हैं और compiler के लिए fixed meaning रखते हैं, इसलिए किसी variable को int या class नाम देने की कोशिश compile होने में fail होगी।
उदाहरण: Keywords
public class Main {
public static void main(String[] args) {
// int int = 5; // ILLEGAL - 'int' is a reserved keyword, not a valid identifier
int total = 5;
System.out.println(total);
}
}
Login to try C/C++/Java/PHP code in the editor
Identifiers
एक identifier कोई भी नाम है जो आप एक variable, method, class, या package के लिए चुनते हैं -- clear, descriptive identifiers चुनना (जैसे x के बजाय totalPrice) आपके code को बिना extra comments के self-explanatory बनाता है।
उदाहरण: Identifiers
public class Main {
public static void main(String[] args) {
int totalPrice = 250; // clear identifier instead of 'x'
System.out.println(totalPrice);
}
}
Login to try C/C++/Java/PHP code in the editor
Naming Rules
Java को identifiers एक letter, underscore, या dollar sign से शुरू होने चाहिए, और नाम में कहीं भी spaces या # या - जैसे symbols मना करता है -- numbers allowed हैं, लेकिन कभी बिल्कुल पहले character के रूप में नहीं।
उदाहरण: Naming Rules
public class Main {
public static void main(String[] args) {
int _count = 1;
int $price = 2;
int total2 = 3;
// int 2total = 4; // ILLEGAL - cannot start with a digit
System.out.println(_count + $price + total2);
}
}
Login to try C/C++/Java/PHP code in the editor
Case Sensitivity in Names
चूंकि page और Page को पूरी तरह अलग identifiers माना जाता है, किसी variable को reference करते समय capitalization mix up करना 'cannot find symbol' compiler errors का एक बहुत आम source है।
उदाहरण: Case Sensitivity in Names
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare page and set it to "lowercase"
String page = "lowercase";
// Declare Page and set it to "uppercase"
String Page = "uppercase";
// Print a line to the console
System.out.println(page + " is not the same identifier as " + Page);
}
}
Login to try C/C++/Java/PHP code in the editor
Best Practices for Names
एक loop counter से आगे किसी भी चीज़ के लिए abbreviations से ज़्यादा पूरे words पसंद करें -- cc से ज़्यादा customerCount -- क्योंकि भविष्य के readers (महीनों बाद आप सहित) यह decode करने में कहीं कम समय बिताएंगे कि variable क्या रखता है।
उदाहरण: Best Practices for Names
public class Main {
public static void main(String[] args) {
int customerCount = 42; // clearer than 'cc'
System.out.println(customerCount);
}
}
Login to try C/C++/Java/PHP code in the editor
classयाintजैसा एक keyword variable नाम के रूप में उपयोग करना, जो एक compile error है।- किसी identifier को एक digit से शुरू करना, जैसे
1name, जो allowed नहीं। - यह मान लेना कि
Nameऔरnameवही identifier हैं, जब Java case-sensitive है।
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: