Java Identifiers

Java Tutorial

Java Identifiers – Complete Guide

In Java Identifiers is the name given to:

 Identifiers help Java identify program elements.


 1. Examples of Java Identifiers


 

Here:

  • age, salary, name → variable identifiers

  • calculateTotal → method identifier

  • Student → class identifier


 2. Rules for Java Identifier (Very Important)

 Allowed

  1. Can contain letters (a–z, A–Z)

  2. Can contain digits (0–9)

  3. Can contain underscore (_)

  4. Can contain dollar sign ($)

  5. Must start with a letter, _, or $


 Not Allowed

  1. Cannot start with a digit

  2. Cannot contain spaces

  3. Cannot use special characters (@, #, %, etc.)

  4. Cannot use Java keywords


 3. Java Keywords (Cannot Be Identifier)

Examples:

  •  These words are reserved by Java.

 4. Case Sensitivity in Identifiers

Java is case-sensitive:

  • age and Age are different identifiers

 5. Naming Conventions (Best Practice)

Variables & Methods → camelCase


Classes → PascalCase


Constants → UPPER_CASE


 6. Valid vs Invalid Identifiers (Quick Table)

ValidInvalid
age2age
studentNamestudent name
_totaltotal@
$salaryclass
count1int

 7. Meaningful Identifiers (Recommended)

 Bad:

Good:


 8. Interview Questions (Must Know)

Q1: Can an identifier start with $?
 Yes

Q2: Can Java keywords be identifier?
 No

Q3: Is Java case-sensitive?
Yes

Q4: Is _ allowed as an identifier?
 Yes (but avoid alone _ in modern Java)


 Quick Cheat Sheet


 

You may also like...