Identifiers in java


Identifiers are the names of variables, methods, classes, packages and interfaces. Unlike literals they are not the things themselves, just ways of referring to them. In the HelloWorld program, here HelloWorld, String, args, main and println are identifiers.

  • Rules of identifiers:

Identifiers must not begin with a digit. They can have alphabets, digit, underscores and dollar sign. Java is a case sensitive language therefore upper case and lower case are treated as different. They can be of any length. Keywords are not use as an identifier. No any symbol other than underscore and dollar sign are allowed even blank space (white space) are also not allowed. We can start /begin an identifier with letter, underscores or a dollar sign.

    • Valid Identifiers :

↪ $myvariable
↪ _variable
↪ variable
↪ deepak_identifier_name
↪ deepak2024
↪ x
↪ empId  ...

    • Invalid Identifiers :
↪ Deepak variable
↪ Deepak_identifiere
↪ &variable
↪ 123idntifiers
↪ Switch
↪ Var/deepak
↪ Variable’s  
↪ employ Id, etc...

  • Naming Convention :

↪Identifiers must be meaningful, short enough to be quickly and easily typed and long enough to be descriptive and easily read.

    • Class and interface :

↪All classes and interfaces start with a leading uppercase letter and each subsequent word with a leading uppercase letter (proper case).  

 
Example: Student, SumAvgN, MyJava etc.

    • Variables and Methods :

↪Names of all public method and instance variables starts with a leading lower-case letter , when more than one words are used in a name , the second and subsequent words are marked with a leading uppercase letter. For example, totalMarks, compareTo() etc. All private and local variables use only lowercase letter combined with underscore as like in C.

      • Public variables and methods : Lower case: age, x, y, add, addition(), intput(), etc...
      • Private and local variable and methods :  Proper case and Lower case: empId, empSalary, getData(), setData(), etc...

    • Constants :

↪Variable that represents constant value, uses all uppercase letter and underscores between words.

 
Example: FULL_MARKS, PI, PASS_MARKS etc.

    • Packages:

↪These consists of all lowercase letter. 

 
Example : mypackage.subpakage.subpackag

Comments