Site icon DataFlair

Identifiers in Java – Explore the Major Rules to Declare it!

Java Identifiers

Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java

When you were born, you were given a name. Imagine what would happen if they didn’t give you one? Imagine if there were no concept of names in this world? It would have been tough!

You would have to specifically describe each person by their specific appearance every time you want to talk about someone.

Imagine the contact names you would use then.

Long story short, it would be weird and redundant.

This is why the concept of naming someone has been brought to identify people.

In the programming world, you also use the same techniques. These are called identifiers.

Identifiers in Java

Any name that you see in a Java program, be it the class name, the function name or the variable name, each of them is an identifier.

They identify or point to a certain thing in memory, be it a variable or a class. Let us take a very basic example of a Java program and find all the identifiers in it.

package com.dataflair.javaidentifiers;
public class IdentifierBasics
{
    public void methodexample()
    {
        System.out.println("Hey there. I am learning Java at DataFlair");
    }
    public static void main(String[] args) {
        new IdentifierBasics().methodexample();
    }
}

Output:

Hey there. I am learning Java at DataFlair

Let us take note of all the identifiers in this program.

  1. IdentifierBasics- This is an identifier for the class.
  2. methodexample()- This is an identifier for the method.
  3. args- This is an identifier for the arguments to the program.

Rules for Using Java Identifiers

As you might have expected, there are some rules for using identifiers in Java. Some of them are:

Best Practices for Naming Identifiers in Java

1. Try to use a short name for the variable.

2. If representing a value by more than one word, make sure to separate it by an underscore.

3. Avoid the use of a lengthy name, as it reduces the readability of code.

4. Name the variable such that it represents the data for easy understanding.

5. Using the uppercase and lowercase letters of the same character creates confusion. Instead, use other characters.

Reserved Keywords in Java

As we discussed, some words in Java cannot be used as identifiers. Some of them are words such as goto, const, class, void, public and so on…

This means that there is a set of words that have a special meaning to the compiler. You can not use the words as your variable names or class names.

There are also literals that have a special value. For example, true, false and null.

You can try to make use of these words as normal identifiers, but the compiler does not recognise them and will throw an error.

However, if you use an IDE, you will immediately notice the colour change in the syntax when you try to use a reserved keyword as an identifier for something else, such as a class or a variable.

Examples of Valid and Invalid Identifiers in Java

Now that we know quite a bit about valid and invalid identifiers, let us see some examples of what identifiers should look like and what they shouldn’t look like.

Some valid identifiers in Java are:

  1. _helloworld
  2. hIghValue
  3. Special$value

Some invalid identifiers in Java are:

  1. ^specialValue
  2. 00xTrick
  3. %trest

These identifiers are not correct.

Summary

In this article, we learned about identifiers, which are a core concept in Java. We learnt about them, how they should be named and much more.

Identifiers are often a favourite topic to be asked by interviewers, so if you are applying for a job and you have Java in your resume, be prepared with these concepts, as they are the basics, and many tricky questions can be asked about this.

Exit mobile version