๐ŸŽ‰ Special Offer !    Code: GET300OFF    Flat โ‚น300 OFF on every Java Course
Grab Deal ๐Ÿš€

Identifiers in Java  


Introduction

Person Talking Boy : How can i identify you ?
Girl : My name is "Priya", you can identify by my name.
  • Just as "Priya" is a name used to identify the girl; in programming, identifiers are names used to identify variables, classes, methods, interfaces or other elements in the code.
  • In simple words we can say, Ientifiers is any name, it can be variable, methods or class name in our program.
  • They are unique names that help us recognize elements like variables, methods, and classes in our program.
  • For example :
    • String name = "Deepak"; // here name is identifier
    • int rollno = 101; // here rollno is identifier
    • class Test {-} // here Test is identifier
Rules for Identifiers:
  1. Spaces cannot be used in an identifier.
    • Identifiers must not contain any whitespace.
    • Valid Examples:
      int myVariable;
      String userName;
    • Invalid Examples:
      int my Variable;  // Error: Spaces are not allowed
      String user Name; // Error: Spaces are not allowed
  2. Only two symbols (_ and $) can be used in an identifier.
    • Identifiers can include underscores (_) or dollar signs ($), but other special symbols like @, # or ! are not allowed.
    • Valid Examples:
      int _counter;
      double $price;
      String first_name;
    • Invalid Examples:
      int user#name;   // Error: '#' is not allowed
      double total@;   // Error: '@' is not allowed
  3. Integer values cannot be used at the first position.
    • Identifiers cannot begin with a digit (0-9) but may include digits after the first character.
    • Valid Examples:
      int variable1;
      double _123value;
    • Invalid Examples:
      int 1variable;   // Error: Cannot start with a digit
      double 3value;   // Error: Cannot start with a digit
  4. Reserved words cannot be used as an identifier.
    • Reserved keywords in Java (like class, public, if, etc.) cannot be used as identifiers.
    • Valid Examples:
      int myClass;
      String _if;
    • Invalid Examples:
      int class;   // Error: 'class' is a reserved keyword
      String public; // Error: 'public' is a reserved keyword
  5. Case Sensitivity.
    • Java identifiers are case-sensitive. For example, MyVariable and myVariable are treated as distinct identifiers.
    • Valid Examples:
      int MyVariable = 10;
      int myVariable = 20; // Different from 'MyVariable'
  6. No Length Limit.
    • There is no specific limit on the length of an identifier, but it is recommended to keep names concise and meaningful.
    • Valid Examples:
      int thisIsAnExtremelyLongVariableName = 100; // Valid but not recommended
  7. Must Not Conflict with Built-in Methods.
    • Avoid using names that conflict with commonly used built-in methods or library functions to prevent confusion.
    • Valid Examples:
      int println = 5; // Valid but confusing, as 'println' is used in System.out.println()
  8. Use Meaningful Names.
    • Choose names that describe the purpose of the variable or method to improve code readability.
    • Valid Examples:
      int x = 10; // Valid but not descriptive
      int age = 10; // Better: Descriptive and meaningful

Java Naming Conventions:
  • Naming convention refers to a set of standardized rules and guidelines for naming classes, methods, variables, and other identifiers to ensure code readability and maintainability.
  • Below table shows an example of naming convention in java :
    Naming Single Word Two Words Three Words
    Classes, Interfaces Example MyExample MyExampleDemo
    Methods example() myExample() myExampleDemo()
    Variables example my_example my_example_demo
    Constants EXAMPLE MY_EXAMPLE MY_EXAMPLE_DEMO
    Packages example my.example my.example.demo
  • Key Notes for Each Category:
    1. Classes and Interfaces:
      • Use PascalCase (capitalize the first letter of each word).
      • Example: MyClass, UserAccount.
    2. Methods:
      • Use camelCase (first word lowercase, subsequent words capitalized).
      • Always name methods as actions or verbs.
      • Example: getDetails(), calculateTotal().
    3. Variables:
      • Use camelCase for instance variables and local variables.
      • Separate words with an underscore (_) only when necessary.
      • Example: user_age, total_price.
    4. Constants:
      • Use UPPER_SNAKE_CASE (all uppercase, words separated by underscores).
      • Example: MAX_VALUE, DEFAULT_TIMEOUT.
    5. Packages:
      • Use lowercase letters with dots separating levels of hierarchy.
      • Reflect the project's domain or structure.
      • Example: com.example.project.