Java Identifiers and Keywords

Short Answer:
Identifiers are names given to classes, variables, and methods. Keywords are reserved words in Java with predefined meaning.

Detailed Explanation:
Identifiers are user-defined names, while keywords are reserved by Java and cannot be used as identifiers.


1. Java Identifiers

Short: Names used to identify variables, methods, classes, etc.

Rules for Identifiers:

  • Must start with a letter, underscore (_), or dollar sign ($).
  • Cannot start with a number.
  • Cannot use Java keywords.
  • Are case-sensitive.
  • No special characters like @, #, %, etc.

Valid Examples:

int age;
String studentName;
double _salary;
int $count;

Invalid Examples:

int 1age;      // Cannot start with number
int class;     // Keyword cannot be used
int total-amt; // Special character not allowed

2. Java Keywords

Short: Reserved words with special meaning in Java.

Java has 50+ keywords. They define structure, data types, access control, flow control, etc.

Commonly Used Keywords:

Access Class Data Types Flow Control
public class int if
private interface double else
protected extends boolean switch
static implements char for
final abstract void while

Important Interview Points

  • Keywords cannot be used as identifiers.
  • Java is case-sensitive (Age and age are different).
  • main is not a keyword.
  • String is not a keyword (It is a class).
  • const and goto are reserved but not used.

0 comments

Leave a comment