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

Java Keywords & Identifiers

Keywords are words Java has already reserved for its own jobs, like reserved parking spots. Identifiers are the names you give to your own things, as long as you follow the naming rules.

Keywords

Keywords like class, int, if, and return are reserved by the language itself and have fixed meaning to the compiler, so trying to name a variable int or class will fail to compile.

Example: 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

An identifier is any name you choose for a variable, method, class, or package -- picking clear, descriptive identifiers (like totalPrice instead of x) makes your code self-explanatory without extra comments.

Example: 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 requires identifiers to start with a letter, underscore, or dollar sign, and forbids spaces or symbols like # or - anywhere in the name -- numbers are allowed, but never as the very first character.

Example: 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

Because page and Page are treated as completely different identifiers, mixing up capitalization when referencing a variable is a very common source of 'cannot find symbol' compiler errors.

Example: 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

Favor full words over abbreviations for anything beyond a loop counter -- customerCount over cc -- since future readers (including you, months later) will spend far less time decoding what the variable holds.

Example: 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. #}
Common Mistakes
  1. Using a keyword such as class or int as a variable name, which is a compile error.
  2. Starting an identifier with a digit, as in 1name, which is not allowed.
  3. Assuming Name and name are the same identifier, when Java is 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.