Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconSoftware Developmentbreadcumb forward arrow iconJava Identifiers: Definition, Syntax, and Examples

Java Identifiers: Definition, Syntax, and Examples

Last updated:
29th Apr, 2022
Views
Read Time
7 Mins
share image icon
In this article
Chevron in toc
View All
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.

Ads of upGrad blog

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”).

Learn Java Tutorials

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.

Check out all trending Java Tutorials in 2024

Ads of upGrad blog

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!

Profile

Pavan Vadapalli

Blog Author
Director of Engineering @ upGrad. Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working on solving problems of scale and long term technology strategy.

Frequently Asked Questions (FAQs)

1What 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.

2 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.

3What 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.

Explore Free Courses

Suggested Tutorials

View All

Suggested Blogs

Best Jobs in IT without coding
134966
If you are someone who dreams of getting into the IT industry but doesn’t have a passion for learning programming, then it’s OKAY! Let me
Read More

by Sriram

12 Apr 2024

Scrum Master Salary in India: For Freshers & Experienced [2023]
900403
Wondering what is the range of Scrum Master salary in India? Have you ever watched a game of rugby? Whether your answer is a yes or a no, you might h
Read More

by Rohan Vats

05 Mar 2024

SDE Developer Salary in India: For Freshers & Experienced [2024]
905374
A Software Development Engineer (SDE) is responsible for creating cross-platform applications and software systems, applying the principles of compute
Read More

by Rohan Vats

05 Mar 2024

System Calls in OS: Different types explained
5044
Ever wondered how your computer knows to save a file or display a webpage when you click a button? All thanks to system calls – the secret messengers
Read More

by Prateek Singh

29 Feb 2024

Marquee Tag & Attributes in HTML: Features, Uses, Examples
5202
In my journey as a web developer, one HTML element that has consistently sparked both curiosity and creativity is the venerable Marquee tag. As I delv
Read More

by venkatesh Rajanala

29 Feb 2024

What is Coding? Uses of Coding for Software Engineer in 2024
5082
Introduction  The word “coding” has moved beyond its technical definition in today’s digital age and is now considered an essential ability in
Read More

by Harish K

29 Feb 2024

Functions of Operating System: Features, Uses, Types
5228
The operating system (OS) stands as a crucial component that facilitates the interaction between software and hardware in computer systems. It serves
Read More

by Geetika Mathur

29 Feb 2024

What is Information Technology? Definition and Examples
5114
Information technology includes every digital action that happens within an organization. Everything from running software on your system and organizi
Read More

by spandita hati

29 Feb 2024

50 Networking Interview Questions & Answers (Freshers & Experienced)
5297
In the vast landscape of technology, computer networks serve as the vital infrastructure that underpins modern connectivity.  Understanding the core p
Read More

by Harish K

29 Feb 2024

Schedule 1:1 free counsellingTalk to Career Expert
icon
footer sticky close icon