Select Page

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

May 10, 2023

Identifiers in Java - java for beginners


  • 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 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
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 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

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

Ouput

Student Name: John
Test Score: 63.3


You May Also Like…


double data type in Java
double data type in Java

The double data type is also a floating point number. It is called double because it has a...



0 Comments

Submit a Comment

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