Site icon DataFlair

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

Java Identifiers

Free Java courses with 37 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 are identifiers.

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

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

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 Identifiers

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

Reserved Keywords

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

This means that there are 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 recognize 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

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 are:

  1. _helloworld
  2. hIghValue
  3. Special$value

Some invalid identifiers are:

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

These identifiers are not correct.

Summary

In this article, we learnt 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 from this.

Exit mobile version