Saturday, December 13, 2008

What is an identifier in Java

Welcome back to Java Code Online. Java is a very powerful language. The power in it comes from the Principle of OOPS that it follows. Identifiers in Java are used for naming a variable, method, class or label. I will cover each of these topics in detail, in my later posts. For the time being just concentrate on the word identifier.

Identifiers are required by Java, so that you may give your variable a name. Though as seen above it is also used for naming method, class and label. There are some rules that need to be followed, while naming an identifier.

The rules for naming an identifier are simple and must be practiced thoroughly, if you don't want your JVM to shout that it cant compile the class. The rules for naming an identifier are:-

1. An identifier in Java can start with a letter.
2. An identifier in Java can start with a dollar sign($)
3. An identifier in Java can start with an underscore(_)
4. Subsequent characters in the identifier after the first character can be letters, dollar sign, underscore, or a digit.

Any other rule for identifiers is not there, so just stick to these basic four rules and you won't face any problem in compilation of your code.

I will take a few examples so as to make the situation more clear to you.

companyName //legal
BigClass //legal: you may embed the keywords in between
$money //legal
8_node //illegal(Cannot start with a digit)
!node //illegal(special characters not allowed in starting except "$" and "_")

Now you would be able to clearly distinguish, which one is a valid identifier and which one is not. Having good understating of these basic concepts of Java help make the life of the developer easy, as he faces less errors. The life of a developer is already very hard, faced with coding requirements, meeting deadline, and debugging bugs.

So it would be good to avoid small pitfalls like naming an identifier. I hope the article was handy for all those looking for information on Identifiers in Java. If you like the article then do leave a comment. For more info on Java, keep buzzing Java Code Online.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.