Learn Java Identifiers
In Java, an identifier is a name given to a variable, class, method, or another program element. Java identifiers must follow some rules:
- An identifier must start with a letter (a to z or A to Z), a dollar sign ($), or an underscore (_).
- After the initial character, identifiers can contain letters, numbers, dollar signs, and underscores.
- Java identifiers are case-sensitive, so a variable named “foo” is different from a variable named “Foo”.
- Java has reserved words that cannot be used as identifiers, such as “if”, “for”, and “while”.
- An identifier cannot have spaces or special characters, such as @, !, %, and *.
Here are some examples of valid Java identifiers:
int age;
double salary;
String firstName;
List<String> names;
MyClass myClass;
And here are some examples of invalid Java identifiers:
int 2years; // identifier cannot start with a number
double my$salary!; // identifier cannot have special characters
String first name; // identifier cannot have spaces
boolean if; // "if" is a reserved word
Note: It is advisable to use explanatory names. By using simple names, it will be very easy for you to understand and maintain your code in the future:
We need to use some conventions before declaring Java identifers. Learn from this simple Java code as shown below:
Code Example:
public class first {
public static void main(String[] args) {
int x, y, z;
x = y = z = 100;
// Print the value of x + y + z
System.out.println(x + y + z);
}
}

1st First is a Class Name.
2nd Main is a Method.
3rd String is a Predefined Class Name.
4th args is a String Variable.
5th System is a Predefined Class Name.
6th Out is a Variable Name.
7th println is a Method.
Please find the list of reserved keywords in Java Language:
abstract
continue
for
protected transient
Assert
Default
Goto
public
Try
Boolean
Do
If
Static
throws
break
double
implements
strictfp
Package
byte
else
import
super
Private
case
enum
Interface
Short
switch
Catch
Extends
instanceof
return
void
Char
Final
Int
synchronized
volatile
class
finally
long throw Date
const
float
Native
This
while
Code Example:
public class First{
public static void main(String[] args) {
// Good way to write the identifiers so you can understand later
double weight= 76.85;
// OK, it's very difficult to understand what w represnts
double w = 76.85;
System.out.println(weight);
System.out.println(w);
}
}
Output: