Java Identifiers And Keywords With Example

What are Java identifiers ?

An identifier can be viewed as a sequence of valid characters such as letters, numbers, dollar-sign, and underscore characters that are used to name classes, variables, and methods for uniquely identifying them.

Further, Java is case-sensitive, meaning, “TEST” and “test” are not considered as same. An identifier must not start with a number as the resultant identifier would be treated as an invalid.

Below are the examples of valid identifiers in Java :

test, Test, te1st, $test, te_st

Below are the invalid identifiers in Java :

2test, te-st, te/st

A legal identifier always starts with a letter or dollar sign or underscore, and its succeeding characters can be the letter, digits, dollar, and underscore.

How many identifier can you see ?

public class MyClass {

    public static void main(String[] args) {
        int test = 0;
    }
}
There are 5 identifiers  in above class.
keywords java 2

What are the rules for identifiers in Java ?

A Java compiler follows certain rules to validate a valid identifier, if it is considered invalid, then a compiler would throw a compile-time exception.

Therefore, one needs to follow certain rules to define a valid identifier.

  • A valid or legal identifier is made of a combination of alphabets, numbers, dollar signs, and underscores. Using any other characters to create an identifier would trigger a compile-time error.
  • It should start with any letter, $ sign, and underscore. For example, _abc. Further, digits are not allowed to use as the very first character of an identifier i.e 12abc.
  • In Java, identifiers are case-sensitive. For example, ‘name’ and ‘Name’ are not considered as same, therefore, can be differentiated based on the case.
  • In Java, there is no restriction on the length of an identifier. This means an identifier can have any range of sequence of characters. Using a lengthy name for a class, method or field can reduce the readability of code.
  • reserved keyword cannot be used as an identifier. For example, int long=10.
  • White space is not allowed in between an identifier.

Basic Naming conventions in Java

An identifier should have meaningful names as it increases code readability. In Java, there are certain naming conventions that one should follow while naming a class, variable, or method. However, these are not required to follow strictly but are considered as a good coding habit.

These are the following conventions :

1) An identifier should be in camel-case if the name is constructed by combining two or more words. For example, “userName”, “getSum()”

2) A method or variable name should begin with a lowercase letter. for example, “result”,”getSum()”.

3) A class or interface name should start with a capital letter. for example, “User”, “String”

4) A variable defined as a constant should be in capital letters and if it is made from two or more words then “_” should be used in between. For example, “PI”, “USER_NAME”

 

What is reserved keywords in Java?

A reserved keyword depicts a predefined meaning and functionality to the compiler which acts as one of the basic building blocks to write a program.

A reserved keyword cannot be used as a variable name or identifier. There are 50 Keywords in JAVA and 3 literals. Using any of these in a program as an identifier would trigger a compile-time error.

abstractcontinue fornewswitch
assert defaultgotopackage synchronized
boolean do ifprivate this
break double implementsprotectedthrow
byte else importpublicthrows
case enuminstanceof returntransient
catch extends int shorttry
char finalinterface static void
classfinally long strictfpvolatile
constfloat nativesuper while

Leave a Reply