Identifiers in Java – Explore the Major Rules to Declare it!
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 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:
Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!
Let us take note of all the identifiers in this program.
- IdentifierBasics- This is an identifier for the class.
- methodexample()- This is an identifier for the method.
- 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:
- You should only use alphabets and numbers while naming anything in Java. The only special characters that are allowed are the dollar sign(‘$’) and the underscore(‘_’).
- Even if you can use numbers you cannot use them to start the name of the identifier. You cannot name your variable as 666devil. But you can use devil666 as a valid variable name.
- Since Java is case sensitive, the identifiers a and A are completely different. This means that you can have two different variables with the same name but different case.
- Java does not implement a restriction on the length of the identifier. However, it is advisable to keep the name of the identifier between 2 to 18.
- There are certain words that have a special meaning to the compiler such as int, class. You cannot use these as identifiers.
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:
- _helloworld
- hIghValue
- Special$value
Some invalid identifiers are:
- ^specialValue
- 00xTrick
- %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.
Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google
Nice teaching sense
Everything is explained in details.
WHY INT INVALID JAVA IDENTIFIERS BUT INTEGER NOT?
Best explanation
Such as Too much effective information that i found
give 15 examples of how to declare an identifier/variable in Java
15 examples of valid and invalid identifier in
Java
Best explanation