An identifier is simply the name given to a class, method, variable, etc. in a Java program.
An identifier in Java is a sequence of characters of unlimited length.
The sequence of characters includes all Java letters and Java digits, the first of which must be a Java letter.
For example, A-Z, a-z, _ (underscore), and $ are considered Java letters from the ASCII character set range of Unicode.
Examples of valid identifiers are as follows:
Name | Description |
---|---|
num1 | Can use a-z and 0-9 and start with a letter |
letter | Only letters |
aLetter | Can mix uppercase and lowercase |
a1Letter | Can mix letter and digit as long as the first letter is not digit |
_aaa | Can start with an underscore |
_ | Can have only one letter, which is an underscore |
sum_of_two_numbers | Can have letters and underscores |
Outer$Inner$Level | Can have a-z, A-Z and $ |
$var | Can start with $ |
Examples of invalid identifiers are as follows:
Name | Description |
---|---|
2num | Cannot start with a number |
my name | Cannot have a space |
num1+num2 | Cannot have + sign |
num1-num2 | Cannot have minus sign |