Java Identifiers

Identifiers in Java

In Java programming, identifiers are names used to identify variables, methods, classes, and other elements in your code. They act as labels to help you refer to different components within your program.

Rules for Naming Identifiers

  • Start with a letter, underscore, or dollar sign: Identifiers must begin with a letter (a-z, A-Z), an underscore (_), or a dollar sign ($). However, the dollar sign is not recommended unless generated by tools.
  • Can include letters, digits, and underscores: After the first character, you can use letters, digits (0-9), underscores, or dollar signs.
  • Case-sensitive: Identifiers are case-sensitive, meaning myVariable and myvariable are treated as different identifiers.
  • Cannot be a reserved keyword: Identifiers cannot use Java reserved keywords like int, class, public, etc.

Examples of Valid Identifiers
int myVariable;
float _totalAmount;
String userName1;

Examples of Invalid Identifiers
int 1stVariable; // Starts with a digit
float total-Amount; // Contains a hyphen
String @class; // Uses a reserved keyword

Importance of Descriptive Identifiers
Using meaningful names for identifiers improves code readability and maintainability. While shorter names are quicker to type, descriptive names give context about the purpose of the variable, making the code more understandable.

Good Practice Example
In this example, the variable minutesPerHour clearly indicates that it represents the number of minutes in an hour, making the code easier to follow.
int minutesPerHour = 60; // Descriptive and clear

Less Descriptive Example
The variable m is less descriptive and can make the code confusing, especially to others or when revisiting the code later.
int m = 60; // Not clear what ‘m’ stands for

Task : Create your bio using the proper data type and proper identifier with values like below example as you can see in image .

Less-Descriptive-Example

Course Video

YouTube Reference :