Core Java

Java Identifier Explained

In this article, we will see examples of the identifier in the java programming language, which is a very important aspect of Programming.

1. Introduction

In C, C++, C#, Java, and other programming languages, an identifier is a name that is assigned by the user for a program element such as variable type, template, class, function, or namespace. It is usually limited to letters, digits, and underscores.

Java Identifier

Certain words, such as “new,” “int” and “break,” are reserved keywords and cannot be used as identifiers. Identifiers are used to identify a program element in the code. 

2. What is a Java Identifier?

In programming languages, identifiers are used for identification purpose.

In Java, an identifier can be a class name, method name, variable name, or a label. It allows a programmer to refer to the item from other places in the program.

To make the most out of the identifiers you choose, make them meaningful, and follow the standard Java naming conventions.

3. Java Identifier Examples

Identifiers must be composed of letters, numbers, the underscore and the dollar sign. Identifiers may only begin with a letter, the underscore or a dollar sign.

int score;

You cannot use keywords as variable names. It’s because keywords have predefined meanings, For example

int float;

Here, score is a variable (an identifier). It’s because float is a keyword and cannot be used as a variable name.

public class Test
{
    public static void main(String[] args)
    {
        int a = 20;
    }
}

In the above java code, we have 5 identifiers namely:

  • Test : class name.
  • main : method name.
  • String : predefined class name.
  • args : variable name.
  • a : variable name.

4. Java Identifiers Rules/Conventions

In Java, there are several points to remember about identifiers. They are as follows:

  • Identifiers cannot be a keyword.
  • Identifiers are case-sensitive.
  • It can have a sequence of letters and digits. However, it must begin with a letter, $ or _ .
  • The first letter of an identifier cannot be a digit.
  • It’s a convention to start an identifier with a letter rather and $ or _.
  • White-spaces are not allowed.
  • Similarly, you cannot use symbols such as @, #, and so on.

Some valid identifiers:

  • score
  • level
  • highestScore
  • number1
  • convertToString

Some invalid identifiers:

  • float
  • 1number
  • highest Score
  • @pple

5. Java Identifier – Summary

Identifiers in Java is a very important aspect of Programming. They are mainly used for Identification Purposes. Identifiers are a necessity as they make the program readable and efficient.

This article is contributed to enhancing your understanding of the Identifiers’ rules/conventions providing examples of valid identifiers.

Simranjit Singh

Simranjit Singh has graduated from Computer Science Department of the Guru Nanak Dev University of Amritsar, Punjab, India. He also holds a Master degree in Software Engineering from the Birla Institute of Technology & Science of Pilani, Rajasthan, India. He works as a Senior Consultant in the e-commerce sector where he is mainly involved with projects based on Java and Big Data technologies.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button