Friday, 11 January 2013

Java Identifiers

Identifiers in Java

Basics in using Identifiers in Java Programming


Identifiers are programmer-designed tokens. They are used for naming classes, methods, variables, objects, labels, packages and interfaces in a program. Java identifier’s basic rules are as follows:
  1. They can have alphabets, digits, and the underscore and dollar sign characters.
  2. They must not begin with a digit.
  3. Uppercase and lowercase letters are distinct.
  4. They can be of any length.
Identifier must be meaningful, short enough to be quickly and easily typed and long enough to be descriptive and easily read. Java developers have followed some naming conventions.
  • Names of all public methods and instance variables start with a leading lowercase letter. 
              Example: 
                       average
                       sum
  • When more than one word are used in a name, the second and subsequent words are marked with a leading uppercase letters. 
              Example: 
                       dayTemperature
                       firstDayOfMonth
                       totalMarks
  • All private and local variables use only lowercase letters combined with underscores.
              Example: 
                       length
                       Batch_strength
  • All classes and interfaces start with a leading uppercase letter(and each subsequent word with a leading uppercase letter).
Example: 
                       Student
                       HelloJava
                       Vehicle
                       MotorCycle
  • Variables that represent constant values use all uppercase letters and underscores between words.
Example: 
                       TOTAL
                       F_MAX
                       PRINCIPAL_AMOUNT
We may follow our own conventions as long as we do not break the basic rules of naming identifiers. 
The following table shows some valid and invalid identifiers:

Valid
Invalid
HelloWorld
Hello World (uses a space)
Hi_JAVA
Hi JAVA! (uses a space and punctuation mark)
value3
3value(begins with a number)
Tall
short (this is a Java keyword)
$age
#age (does not begin with any other symbol except _ $ )

It is standard Java practice to name multiple-word identifiers in lowercase except for the beginning letter of words in the middle of the name.

No comments :

Post a comment