Identifiers in Java – how to create and use them
Identifiers in Java are names that a programmer creates to represent something in his program. So we normally say that identifiers are programmer-defined sequence of characters.
nanadwumor
- An identifier is a name that a programmer creates to represent something in his program.
- Examples of identifiers are names you give to your variable, Class, method, package, constants
- Although not compulsory, always choose names for your identifiers based on what they do.
RECOMMENDED ARTICLES
Variables and Literals in Java
Variables are used in many situations. For Instance, game applications use variables to collect your name when you sign in; to store and keep track of your number of trials, successes and failures. In Java, a variable is a container used to store...
double data type in Java
The double data type is also a floating point number. It is called double because it has a double-precision decimal number. The double data type is a floating point number. It is double precision data type The range is 4.9406564584124654 x...
The print and println methods in Java
The print() and println() methods are Java's predefined methods used to display text output on the console. These methods are part of the Java API. The print and println methods are used to print string of characters onto the console The print...
To identify something is to recognize it. In Java, an identifier is a name that a programmer creates to represent something in his program. So we normally say that identifiers are programmer-defined sequence of characters.
Examples of identifiers are names you give to your variable, Class, method, package, constants etc.
A basic Java program starts with a class name. A class name is an identifier and this underscores how important identifiers are in your program.
Join Other Subscribers on Our YouTube Channel and Don’t Miss a thing!
Program demonstrates Identifiers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
//Program demonstrates programmer-defined and Java identifiers public class SemesterExam { //SemesterExam is name of the class. It is programmer-defined. public static void main(String[] args) { //main is name of the Main method. It is an //identifier. It is one of Java's keywords double testScore=35.0; //testScore is an identifier. It is a programmer-defined name //for a variable double examScore=74.2; //examScore is an identifier. It is a programmer-defined variable double average_score=(testScore+examScore)/2; System.out.print(average_score); } } |
Output
54.6
In the above program, SemesterExam is the name of the class. It is an identifier. It is a name we chose for our class. Note that first character is capitalized and first letter of second word is also capitalized. This is a recommended practice for Java class names. Such naming style is called Pascal Case syntax.
testScore is a variable name. It is a name we chose to identify or hold a student’s score or mark. Note that first letter is in lowercase and first letter of second word is capitalized. This style is called the camelCase.
average_score is another variable we created on our own. This holds the average score of a student. Note that this variable comprises two words. Both words begin with lowercase and are separated by underscore. This is called the snake_case syntax.
args is short for argument. It is another identifier. It is a String array. It can be changed to any other name.
main is a name of a method. It’s a special identifier. It’s a Java keyword and cannot be changed. It is where the Java program starts to execute. It is part of the Java API including methods such as print() and println().
General rules for naming identifiers in Java
- Your identifier should comprise characters from [A-Z], that’s uppercase alphabets, or [a-z], that’s lowercase alphabets, or numbers [0-9], and underscore(_) or a dollar sign ($).
- There cannot be a space character in an identifier. That’s, we cannot have something like Square Number. It should be SquareNumber instead.
- An identifier cannot start with a number. Example : 5Star is an invalid identifier because it starts with a number.
- You cannot use Java’s keywords as identifiers in Java. Example: You cannot use Scanner, System as identifiers because these are pre-defined classes in Java.
- The plus(+) sign cannot be used in identifiers. Example: math+score is an invalid identifier name.
- Hyphen cannot be used in an identifier. Example : price-count is an invalid identifier name.
- The ampersand (&) symbol cannot be used in identifiers. Example: man&woman is an invalid identifier.
- An apostrophe cannot be used in identifiers. Example: Country’s GDP is invalid identifier
- Identifiers are case-sensitive. That’s, uppercase letters are different from lowercase letters. For example, man is different from Man.
- An identifier should not be any query language keywords such as SELECT, FROM, COUNT, DELETE, etc.
Conventions for naming Identifiers
These conventions are not compulsory but recommended practices. They are standard practices followed by the Java community.
- Although not compulsory, always choose names for your identifiers based on what they do. For Instance, if your identifier will hold students’ marks, it is more appropriate to use a word like score or mark but not x or y. If you name your identifiers per what they do, it makes it makes it easy to understand the code even if someone else reads it. This is called Self-documentation or we say, it creates self-documenting code or program.T hats, by reading the code, you even get a quick and fair idea of what the program is intended to do. For instance, a program which has identifiers such as Student, marks, pass etc will suggest that the program calculates marks of students and grades them.
- An identifier should be of length 4-15 characters only. Note that there is no limit on the length of an identifier.
- Althought the dollar sign ($) is a legitimate character to name identifiers, it’s not a recommended practice. That’s, an identifier like $mySchool is correct although it begins with the dollar sigh. This is however not a recommended practice.
Style of writing Identifiers
Java programmers have different ways of writing their identifiers. Some use underscore (_) to separate two or more lowercase words or group of characters. This is called snake_case syntax. This style is recommended for variable and constant names. Examples are: my_index_number, school_fees, id_card, MAX-VALUE etc
Others also put the two words together without an underscore. However, in order to enhance readability of code, the second word is capitalized. This is called the camelCase syntax. This style is recommended for method and variable names. Examples are: myIndexNumber, schoolFees, idCard.
Class names
In Java, a class is given a name. A class name is a programmer-defined word. In other words, it’s the programmer who chooses how he wants to name his class. That’s , he chooses how to identify it. Hence, an identifier.
Aside, the general rules and conventions for naming identifiers, there are recommended conventions to follow when naming classes.
Recommended conventions when naming a Class
- The class should be a noun. The first letter should be uppercase. If two or more words are put together, capitalize the first letter of succeeding words. This is called Pascal casing syntax. Example, don’t write Myindexnumber but write MyIndexNumber.
- Use whole words. Avoid using abbreviations or acronyms. Example: instead of writing SFN, write StudentFileNumber.
However, in situations where the abbreviated form is more common or used that the full name, use the abbreviated form. Example, URL and HTML
The table below summarizes the naming styles recommended for the different identifiers.
IDENTIFIER | STYLE | EXAMPLE |
class | PascalCase | MyClass |
interface | PascalCase | MyInterface |
Method | camelCase | myMethod |
Variable. | camelCase. | firstVariable |
Package | snake_case (all lower case) | original_package |
Constant | snake_case | (ALL UPPER CASE) GLOBAL_CONSTANT |
Program demonstrates Java naming conventions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
package com.villagecoder.example; public class CollegeStudent{ //class name is in PascalCase public static final int PROGRAM_YEARS=4; //maximum years for a program. Constant is in snake_case (ALL UPPERCASE) private String studentName; // to hold Student name. Variable is in camelCase. private double testScore; // to hold Student score. This is in camelCase public CollegeStudent(String name, double score){ studentName=name; testScore=score; } /** * * @param name accepts name of student */ public void setName(String name){ studentName=name; } /** * * @param score accepts score of student */ public void setScore(double score){ testScore=score; } /** * * @return name of student */ public String getName(){ return studentName; } /** * * @return return score */ public double getScore(){ return testScore; } /** * * @param args accepts arguments */ public static void main (String[ ] args){ CollegeStudent std=new CollegeStudent("John", 63.3); System.out.println("Student Name: " + std.getName()); System.out.println("Test Score: " + std.getScore()); }} |
Ouput
Student Name: John
Test Score: 63.3
You May Also Like…
Variables and Literals in Java
Variables are used in many situations. For Instance, game applications use variables to collect...
double data type in Java
The double data type is also a floating point number. It is called double because it has a...
The print and println methods in Java
The print() and println() methods are Java's predefined methods used to display text output on the...
0 Comments