Programs

Java Identifiers: Definition, Syntax, and Examples

Have you ever wondered what the world would be like if people and things had no names? Think about it, every time you refer to a person or thing, you would have to describe their specific physical appearance because they have no name you can identify them with. Moreover, how do you think contacts on your phone would appear if your contacts had no name in the first place? Strange, right?

Naming is as vital in programming languages as it is in our everyday life, and that’s where identifiers in Java have a role to play. Just like naming people is a way to identify them, Java identifiers allow the programmer to refer to different items in a program. 

This article will explore what identifiers in Java are, the rules to write them, and examples of valid and invalid identifiers.

Definition of Java Identifiers

Identifiers in Java are a sequence of characters to identify something in a program. They are names given to a class, variable, package, method, or interface and allow the programmer to refer to the specific item from any place in the program.

In other words, a Java identifier is simply a word or a combination of characters used in a Java program. However, an identifier is considered valid only if it is named according to set rules. In this regard, it is essential to remember that the first character of an identifier is highly crucial and decides the identifier’s validity. 

Java programming language allows underscore (_), dollar sign ($), and letters (A-Z, a-z) as the first character of an identifier. Apart from these characters, you cannot use any other special symbol or character to start an identifier. After the first character, Java allows underscore (_), dollar sign ($), digits (0-9), and letters (A-Z, a-z) for declaration of identifiers. We will discuss more Java identifiers’ rules in the next section.

Explore Our Software Development Free Courses

Example of Java Identifier

Let us take a look at the following Java code snippet and take note of the identifiers.

public class LetsIdentify {

  public static void main(String[] args) {

    String name = “M. Scott”;

    int weight = 70;

    float height = 160.0;

  }

}

The identifiers in the above code snippet are as follows:

LetsIdentify: Identifier for the class

main: Identifier for the method

String: Class name reserved in Java programming language

args: Identifier for the arguments to the program

name: String type variable for holding a string type value

weight: Variable with int data type for holding an integer value

height: Variable with float data type for holding a floating value

Learn Software engineer programs online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.

Syntax For Naming Java Identifiers

Just like any language has a set of rules governing the formation of sentences from words and phrases, Java identifiers are written following definite rules to qualify them as valid. 

Given below are the rules you should keep in mind when defining identifiers in Java:

  • An identifier can consist of letters (A-Z, a-z), dollar sign ($), and underscore (_) as its first character.
  • The acceptable letters and digits that can define Java identifiers are Java letters and Java digits, respectively.
  • For example, “upgrad#” is an invalid Java identifier because it contains a special character (#) which is not allowed. 
  • Digits (0-9) can only be used as non-first characters in Java identifiers. In other words, an identifier can begin with a letter, underscore, or dollar sign but not with a digit. Digits are only allowed after the first character.
  • For example, “67upgrad” is not a valid identifier because it starts with a digit. However, “upgrad67” is a valid one.
  • Java letters and digits can be drawn from the Unicode character set, which supports most writing scripts used today, including the large sets for Japanese, Chinese, and Korean.
  • There should not be any white space in an identifier. Since Java does not accept any spaces in identifiers, an underscore (_) can be used instead.

For example, “up grad” is an invalid Java identifier.   

While the identifier can be of any length, it is advisable to keep it within an optimal range of 4-15 characters long.

The 49 Java reserved keywords cannot be used as identifiers. These include if, else, void, continue, class, import, public, new, switch, etc. Using the reserved keywords as identifiers will give a compile-time error.

Since Java is case-sensitive and treats upper and lower case letters differently, the declaration of identifiers is also case-sensitive.

For example, the identifiers “upGrad” and “upgrad” are entirely different from each other.

Explore our Popular Software Engineering Courses

Examples of Valid Identifiers in Java

A Java identifier is valid only if it is defined following the rules stated above. Keeping that in mind, here are some examples of valid identifiers in Java:

_myvariable

_5variablename

$variabletest

$test_variable

TestVariable

testvariable

A

a

a4

B5

this_is_my_test_variable_123

In-Demand Software Development Skills

Examples of Invalid Identifiers in Java

Invalid identifiers are those that do not follow the rules mentioned earlier. Given below are some examples of invalid Java identifiers.

123test (invalid because it starts with a digit)

Test-variable (invalid because it contains a special character ‘-’ other than _, $, A-Z, or a-z)

break (invalid because it is a reserved keyword)

Test.variable (invalid because it contains a special character ‘.’)

Test variable (invalid because it contains a white space)

this_is_my_test-variable (invalid because of the hyphen ‘-’)

upGrad’s Exclusive Software Development Webinar for you –

SAAS Business – What is So Different?

 

Naming Conventions for Java Identifiers (Optional)

Given below is a set of conventions for naming identifiers in Java. Although these are optional, the conventions increase the readability of the code to a significant degree.

  • Local and private variables should always be in lower case. 
  • The names of instance variables and public methods should be in lower case.
  • For identifiers with multiple words, the starting character of the second and subsequent words should be in upper case to enhance the readability. (For example, “TestVariable”)
  • Interface and class names should begin with an upper case letter.
  • Constants should be named using all upper case letters and underscores. (For example, “MAX_VALUE”).

Read our Popular Articles related to Software Development

Summary

Identifiers are a fundamental concept in Java programming. They are names for identifying class, variable, package, method, or interface and allow programmers to refer to a specific item from anywhere in the program. In other words, Java identifiers are symbolic names in the form of words or a combination of characters given to a class, variable, method, package, constant, etc.

 However, defining identifiers in Java calls for adhering to specific rules, without which the identifier would be invalid and cause a compile-time error. In addition, there are some naming conventions for defining Java identifiers. These are optional and need not be followed strictly. But they enhance the readability of the code and convey the meaning and intention of identifiers with more clarity.

All said and done, if you are looking to kickstart a career in software development, upGrad’s Job-linked PG Certification in Software Engineering is an excellent opportunity to upskill. The 5-month online program focuses on top skills like Java, JavaScript, CSS3, HTML5, SQL & NoSQL Databases, and more. Both final year and fresh graduates can enroll in this program.

Here’s a look into the highlights of  upGad’s Job-linked PG Certification in Software Engineering program:

  • Specialisation in MERN/Cloud-Native
  • 500+ hours of online learning
  • 50+ live sessions
  • 350+ hours of hands-on training
  • Five industry projects
  • Industry mentorship and student support
  • Networking with industry experts

Sign up today and prepare for a promising career in software development!

What is Java identifier and variable?

An identifier and variable are names given by users to particular entities in a program. While an identifier is used to identify a unique entity of a program at the time of execution, a variable is a name given to a memory location that holds a value.

What is a variable identifier?

A variable identifier is a unique string that represents the name of a variable. The name, in turn, serves as a placeholder to specify the variable itself. For example, ‘num1’ can be an identifier for a variable that holds the value of an integer.

What are identifiers and literals in Java?

Identifiers are names assigned to different constructs of a Java program, such as classes, methods, interfaces, variables, etc. On the other hand, literals are the values allotted to identifiers.

Want to share this article?

Prepare for a Career of the Future

Leave a comment

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

Our Popular Software Engineering Courses

Get Free Consultation

Leave a comment

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

×
Get Free career counselling from upGrad experts!
Book a session with an industry professional today!
No Thanks
Let's do it
Get Free career counselling from upGrad experts!
Book a Session with an industry professional today!
Let's do it
No Thanks