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

Java Keywords और Identifiers

Keywords वे words हैं जिन्हें Java ने पहले से अपने खुद के jobs के लिए reserve किया है, reserved parking spots जैसे। Identifiers वे names हैं जो आप अपनी खुद की चीज़ों को देते हैं, जब तक आप naming rules follow करते हैं।

Keywords

class, int, if, और return जैसे Keywords language से खुद reserved हैं और compiler के लिए fixed meaning रखते हैं, इसलिए किसी variable को int या class नाम देने की कोशिश compile होने में fail होगी।

उदाहरण: Keywords

java
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);
	}
}

Identifiers

एक identifier कोई भी नाम है जो आप एक variable, method, class, या package के लिए चुनते हैं -- clear, descriptive identifiers चुनना (जैसे x के बजाय totalPrice) आपके code को बिना extra comments के self-explanatory बनाता है।

उदाहरण: Identifiers

java
public class Main {
	public static void main(String[] args) {
		int totalPrice = 250; // clear identifier instead of 'x'
		System.out.println(totalPrice);
	}
}

Naming Rules

Java को identifiers एक letter, underscore, या dollar sign से शुरू होने चाहिए, और नाम में कहीं भी spaces या # या - जैसे symbols मना करता है -- numbers allowed हैं, लेकिन कभी बिल्कुल पहले character के रूप में नहीं।

उदाहरण: Naming Rules

java
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);
	}
}

Case Sensitivity in Names

चूंकि page और Page को पूरी तरह अलग identifiers माना जाता है, किसी variable को reference करते समय capitalization mix up करना 'cannot find symbol' compiler errors का एक बहुत आम source है।

उदाहरण: Case Sensitivity in Names

java
// 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);
	}
}

Best Practices for Names

एक loop counter से आगे किसी भी चीज़ के लिए abbreviations से ज़्यादा पूरे words पसंद करें -- cc से ज़्यादा customerCount -- क्योंकि भविष्य के readers (महीनों बाद आप सहित) यह decode करने में कहीं कम समय बिताएंगे कि variable क्या रखता है।

उदाहरण: Best Practices for Names

java
public class Main {
	public static void main(String[] args) {
		int customerCount = 42; // clearer than 'cc'
		System.out.println(customerCount);
	}
}
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. class या int जैसा एक keyword variable नाम के रूप में उपयोग करना, जो एक compile error है।
  2. किसी identifier को एक digit से शुरू करना, जैसे 1name, जो allowed नहीं।
  3. यह मान लेना कि Name और name वही identifier हैं, जब Java case-sensitive है।

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.