Identifiers


What is identifier.

  1. A Name in Java program is called identifier, which can be used for identification purpose.
  2. It can be a class name, interface name, method name, label name, variable name etc.

Example








  • In the above example words marked with red underline are identifiers.
  • As you can see there are 5 identifiers
    • Test (Class Name)
    • main (Method Name)
    • String (Pre-defined Class Name)
    • args (Variable Name)
    • println(Method Name)

Rules / Convention for Identifier Naming

While defining Java identifier name, we have to follow some rules as listed below.
  1. Allowed characters in Java identifier are listed below. 
    1. A to Z (Uppercase letters)
    2. a to z (Lowercase letters)
    3. 0 to 9 (Digits)
    4. _ (Underscore)
    5. $ (Dollar sign)
      1. Example: 
        1. contact_number ()
        2. contact# (✕)
  2. Identifier can't starts with digit.
    1. Example
      1. abc123 ()
      2. 123abc (✕)
  3. Java is a case sensitive language all variable listed below are different, but it is not a good programming practice.
    1. int total = 10; ()
    2. int TOTAL = 10; ()
    3. int Total = 10; ()
  4. In Java, there is not limit on the length of identifier name, but it is a good programming practice to keep your variable short, readable and meaningful.
  5. Keywords / Reserved words are not allowed as identifiers.
    1. Example
      1. int a = 10; ()
      2. int if = 10; (✕)
  6. You can use predefined class names and interface names as identifier name, but it is not recommended to use predefined names, it creates conflicts and confusion.
    1. Example
      1. int String = 111; ()
      2. int Runnable = 555; ()

Example of Valid / Invalid Java Identifiers

  1. father_name  ()
  2. my_number  ()
  3. my# (✕)
  4. 123abc (✕)
  5. abc123  ()
  6. ca$h ()
  7. _$_$_$  ()
  8. contact@me (✕)
  9. it2utor ()
  10. w3schools ()
  11. Integer  ()
  12. int (✕)
  13. Int  ()
  14. my-address (✕)

Comments

Popular posts from this blog

Program to calculate telephone bill

Arrays in C Language

Program to check a year is leap year or not.

Sum of two numbers in go lang

String in golang

Pointers in C Language

Data types in Java

Decision Making in Go lang

Installing go lang