Identifiers in Java. A Java identifier is the symbolic name that is used for identification purpose. In Java, an identifier can be a variable name, constant name, method name, class name, array name, packages name or an interface. Few authors term variables as an identifier. For example : int score = 100;
Here, the score is a variable (an identifier)
We’ll be covering the following topics in this tutorial:
Rules when defining Java Identifiers
The rules for naming identifiers are the same as the rules for naming variables & class.
• An identifier is a sequence of characters that may consist of letters (A-Z, a-z) digits (0-9), an underscore character ( _ ) or the dollar sign ($).
• An identifier can be one to several characters long.
• An identifier that cannot be used as a variable name is a reserved word.
• An identifier must start with a letter, an underscore or a dollar sign.
• An identifier cannot start with a digit and must not contain any spaces or tabs.
• An identifier cannot be any of the Java keywords.
• Identifiers are case sensitive, so count, and Count is distinct identifiers.
• An identifier cannot be a true, false or null.
• It is good programming practice to use the identifier names that reflect the meaning of the programming element being used.
• Java is case sensitive. Not using the proper uppercase and lowercase letters for an identifier causes a compilation error typically.
Examples of valid identifiers :
a, area, sum, Welcome, ProcessExamResult, roll_number, Total, GetData, GradeBook, _ab, $amount, INT are all valid identifiers.
Examples of invalid identifiers :
• 5th // Begins with an illegal character.
• max-height //Special symbol such as a hyphen (-) not allowed.
• “a” //Special symbol such as double quotes (” “) not allowed.
• file manager //blank spaces not allowed.
• float //keywords cannot be used as an identifier.
• what? //special symbol (?) not allowed.
Keywords also knew as reserved words are the pre-defined identifiers reserved by Java for a specific purpose and used only in a limited, specific manner. They cannot be used as identifiers because they have special meaning within the language. The keywords are always written in lowercase.
In addition to the rules that governs identifiers, Java programmers follow certain style conventions to make up names for classes, methods, constants, variables, interfaces and packages.
1. Class names in Java begins with a capital letter. Class names should be descriptive names or noun phrases but not very long. If class name contains multiple words then each subsequent word in the class name begins with a capital letter. For example: Employee, GradeBook, CommissionEmployee, ProcessExamResult etc.
2. Names of the fields that are not final and method’s name should begin with a lowercase letter. Method names should be verbs or verb phrases. However, if they contain multiple words then each subsequent word in the name begins with a capital letter. For example: firstName, salary, getName, getMaximum etc.
3. Names of the packages intended only for local use should have a first identifier that begins with a lowercase letters.
4. Constant represent fixed values that cannot be altered. For example, PI is constant with a fixed value 3.14159. Such constants should be written in uppercase.
These conventions are followed so that programmers can distinguish whether an identifier corresponds to a class name, variable, method, package in a program.
All the data type has it’s own capacity to keep the maximum value. Which have been mentioned below :
|Byte-length integer||8-bit two’s complement|
|Short integer||16-bit two’s complement|
|Integer||32-bit two’s complement|
|Long integer||64-bit two’s complement|
|Single-precision floating point||32-bit IEEE 754|
|Double-precision floating point||64-bit IEEE 754|
|A single character||16-bit Unicode character|
|A boolean value (||true or false|