Java - Data Types Java Identifier

What Is an Identifier?

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.

Features of Java Identifier?

  • There is no limit on the number of characters used in an identifier.
  • Characters used in an identifier are drawn from the Unicode character set, not only from the ASCII character set. A Java letter is a letter from any language that is represented by Unicode character set.
  • Java Identifiers are case sensitive. For example, num and Num are two different identifiers.
  • Spaces are not allowed in an identifier.

For example, A-Z, a-z, _ (underscore), and $ are considered Java letters from the ASCII character set range of Unicode.

Valid Java Identifier

Examples of valid identifiers are as follows:

num1Can use a-z and 0-9 and start with a letter
letter Only letters
aLetter Can mix uppercase and lowercase
a1LetterCan 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 $

Invalid Java Identifier

Examples of invalid identifiers are as follows:

2num Cannot start with a number
my name Cannot have a space
num1+num2 Cannot have + sign
num1-num2 Cannot have minus sign