Loading...
Java

Identifiers And Rules To Define Java Identifiers

A name in the java program is called identifiers. It may be class name, method name, variable name and label name.
Example:
class Example
{
public static void main(String[] arg)
{
int a=5;
}
}
The identifiers in the above example are:
1. Example
2. main
3. String[]
4. arg
5. a

Rules to define identifiers:

Rule 1: The allowed characters in java identifiers are:
1. A to Z
2. a to z
3. 0 to 9
4. _ (underscore)
5. $

Rule 2: If we use any other characters or symbols we get compiler time error.
Example:
1. number_sum ———- valid identifier
2. number# —————- invalid identifier

Rule 3: Java identifiers are case sensitive because java language is case sensitive language.
Example:
class Example
{
int sum=5;
int Sum=10;
int SUM=15;
int SuM=20;
int sUM =25;
}
we can differentiate with case

Rule 4: Identifiers cannot be started with digits.
Example:
1. A1——— valid identifier
2. 1A——— Invalid identifier

Rule 5: There is no limit or length for java identifiers but it is recommended to take length less than 15.

Rule 6: Reserved words cannot be used as identifiers.
Example:
int else=10; ———- invalid

Rule 7: All predefined java class names and interface names can be used as identifiers.
Example:
class Tst
{
public static void main (String[] arg)
{
int String=20;
System.out.println(String);
}
}
OUTPUT:
20

Leave a Reply

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