Identifiers in Java

What are Identifiers in Java?

In literal meaning of identifiers is the thing which identifies something. In real world we have names for each thing just to identify them from other things. Let’s take an example camera, what comes into your mind when someone says a camera? Obvious a device which takes beautiful pictures ( by the way it depends on the skills of photographer 😛 ) and saves memories hence this name “camera” identifies a device which is used for taking pictures.

Therefore in Java identifiers means the name which you give to class, method, variables, interfaces etc which identifies them by a specific sequence of Java letters and digits.

But wait!

Java has certain rules for writing identifiers just to maintain a standard for naming as well as to avoid name conflicts and confusion, these are

  • An identifier is a sequence of Java letters and Java digits, the first of which must be a Java letter.

  • Letters and digits may be drawn from the entire Unicode character set, which supports most writing scripts in use in the world today, including the large sets for Chinese, Japanese, and Korean. This allows programmers to use identifiers in their programs that are written in their native languages.
    In following example I’ve used Devnagari script to name the variables as well as storing their respective values. To achieve this we need to use UTF-8.But regarding number literals Java supports only ASCII characters  0-9 for digits according to Java Language Specification 3.10.1. hence in that case you have to type Devnagari script or your native language in double quote and use Integer.parseInt(” “) method to convert it to Java’s supported ASCII digits 0-9;

  • An identifier cannot have the same spelling (Unicode character sequence) as a keyword, boolean literal, or the null literal, or a compile-time error occurs.

  • Two identifiers are same only if they are identical which means both have same Unicode characters representing each letter or digit.
    For an example:
    Unicode of Latin capital letter ‘A’ is \u0041 and Greek capital letter A is \u0391

If we look at the output we can clearly see the Latin and Greek character capital A i.e. ‘A’ apparently looks akin to each other but their Unicodes are different. Therefore two identifiers who appear similar need not be same, they could be different hence two identifiers are considered identical only if their Unicodes are similar.

  • an identifier can be sequence of uppercase and lowercase letters, numbers, or the underscore and dollar-sign characters.
  • Identifiers should not begin with a number.
  • Identifier can have $ sign but not intended for general use so better avoid or use rarely.
  • Identifier can begin with an alphabet, underscore ( _ ), and ¤, $, £, ¢, ¥ but any other symbol and digits at beginning will give compile time error.


Share and Enjoy !

0 0

Leave a Reply

Your email address will not be published. Required fields are marked *