What Are Identifiers?

In Java, an identifier is a name used to identify a variable, class, method, or other program element. Identifiers are used to provide names to various elements in your Java code so that you can refer to and manipulate them. Here are some key rules and characteristics of identifiers in Java:

Naming Rules

  • Identifiers can consist of letters (uppercase and lowercase), digits, and underscores (_).
  • They must begin with a letter (a-z or A-Z) or an underscore (_). Although they can start with an underscore, it is not recommended because it is often reserved for special cases.
  • Identifiers are case-sensitive, which means that myVariable and myvariable are considered different identifiers.
  • You cannot use Java keywords (reserved words) as identifiers. For example, you cannot name a variable "class" or "int" because these are reserved keywords.

Naming Conventions

  • It is a common convention to use camelCase for variable and method names in Java. For example, myVariable, calculateTotalAmount().
  • Class names should follow the PascalCase convention, where each word in the name begins with an uppercase letter. For example, Person, CustomerOrder.
  • Constants (final variables) are often named using uppercase letters with underscores to separate words, known as SCREAMING_SNAKE_CASE. For example, MAX_VALUE, PI.
  • Name of idenfier should be meaningful. The name of identifier should reveal the purpose of using it. For example to store marks of a student, the name of variable should be 'marks' instead of 'abc'.

Length

  • Identifiers can be of any length, but it's a good practice to keep them reasonably short and meaningful. Extremely long names can make your code less readable.

Reserved Words

  • You cannot use Java reserved words (keywords) as identifiers. For example, if, else, class, int, for, etc. Attempting to use these reserved words will result in a compilation error.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Choosing meaningful and descriptive identifiers is important for writing clear and maintainable code. Good naming practices make your code more understandable to you and other programmers who may read or work on it in the future.




Comments

Popular posts from this blog

Lecture 2 - How to Take Input From User in Java

Programming Conventions in Java

Why Java is Called Java?