What is identifier
A name in java program is called identifier. It can be a class name, method name, package name, variable name, constant name.
A shown in the above example, words marked with red underline are identifiers. As you can see there are 5 identifiers as listed below...
- Test (class name)
- main (method name)
- String (Library class name)
- args(variable name)
- println(method name)
Identifier naming rules
While creating identifiers, you have to remember some rules as listed below...
1. Identifier name can contains alphabets (A-Z / a-z), digits (0-9), underscore (_), dollar sign($).
contact_number (✓) contact# (✕)
2. An identifier can't start with a digit.
abc123 (✓) 123abc (✕)
3. Spaces and special symbols are not allowed. User underscore(_) to join two or more words or use the camel-case variable name.
father name (✕) father-name (✕) father_name (✓) fatherName (✓)
4. Java is a case sensitive language, so total, Total, and TOTAL are valid but different variable names.
5. Keywords and Reserved words are not allowed.
int (✕) integer (✓) int_number (✓)
6. Predefined class names and interface names are valid identifier names but it is not a recommended way to use predefined names.
int String = 111; (✓) int Runnable = 555; (✓)
Valid / Invalid Identifier names
father_name (✓) my_number (✓) my# (✕) 123abc (✕) abc123 (✓) ca$h (✓) _$_$_$ (✓) contact@me (✕) it2utor (✓) w3schools (✓) Integer (✓) int (✕) Int (✓) my-address (✕)