Java Identifiers
In java , Name of variables , methods , classes , packages , interfaces are known as the identifiers. java identifiers are used for identification purpose.
Identifiers must be composed of letters, numbers, the underscore (_) and the dollar sign ($). Identifiers may only starts with a letter, the underscore(_) or a dollar sign($).
Example
public static void main(String args[])
Identifiers in above java code
main : Method name
String : Predefined class name
args : Variable name
a : Variable name
Rules to declare Java Identifiers
There are some predefined rules to declare identifiers which user have to followed otherwise compile throw compile-time-error.
Rule-1
int xyz=200; Valid
int _ab=500; Valid
int $ab=800; Valid
int a-bd=900; Invalid
int a jk=980; Invalid
Rules-2
Identifiers in java should not starts with number [0-9].
Example
int 2ab=100; Invalid
Rule-3
Identifier in java are cases sensitive just because java programming language is a cases sensitive language .
Example
int a=10;
int A=10;
Here java consider both A and a as different variables .
Rules-4
In java identifiers can not be duplicate ,it must be unique .
Rules-5
Length limit not define for declaring the identifiers .
Rules-6
In java it is not possible to use java keywords as identifiers.
Example
int if=20; Invalid
int while=30;Invalid
Valid identifiers in java
The list of legal identifiers in java given below
- MyVariable
- myvariable
- MYVARIABLE
- x
- i
- _myvariable
- $myvariable
- _9id
- ABc
- This_is_Method
- abc123
- sv23
Invalid Identifiers in java
List of illegal identifiers in java given below
- My Variable // Contains space
- 9tv // Starts with a digit
- a+d // Contains + sign
- test1-2-3 // Contains hyphen
- A's // Contain Apostrophe
- A_&_b // Contains ampersand
Hope !!! The above lesson java identifiers helpful for you...
Tags : java identifiers , identifiers in java , declaration of identifiers , rules for defining identifiers , legal identifiers in java , illegal java identifiers ,rule for identifiers
Java Naming Convention with Example |