Identifiers in Java – Rules & Example

1. Objective

In this Java tutorial, we are going to study about the Identifiers in Java. Moreover, we will discuss rules for identifiers in Java. In addition, we saw how Java Identifiers are case sensitive and invalid & valid identifiers in Java Programming language. At last, we will discuss Java Identifier example and reserved words in Java.

So, let’s start with Identifiers in Java.

Identifiers in Java

Identifiers in Java – Rules & Example

2. Identifiers in Java

Java Identifiers use to identify a class name, method or variable name and also a label.

Let’s discuss Variables in Java Programming language

public class Test
                public static void main(String[] args)
                                int a = 20;

Above Java Identifier example, we have 5 identifiers, they are –

  • Test: class name.
  • main: method name.
  • String: predefined class name.
  • args: variable name.
  • a: variable name

3. Rules for Identifiers in Java

There are sure guidelines for characterizing legitimate java identifiers. These principles must be taken after, else we get compile time error. These standards are additionally substantial for different languages like C, C++.

Read about Access Modifiers in Java – Types and Examples

  • The main permitted characters for Java identifiers are for the most part alphanumeric characters ([A-Z],[a-z],[0-9]), ‘$'(dollar sign) and ‘_’ (underscore).For case “ghhh@” isn’t a legitimate java identifier as it contain ‘@’ – special character.
  • Identifiers in Java ought not to begin with digits([0-9]). For instance “123hhhs” is a not a substantial java identifier.
  • Java identifiers are case-sensitive/delicate.
  • There is no restriction on the length of the Java identifier. However, it is prudent to utilize an ideal length of 4 – 15 letters as it were.
  • Reserved words can’t utilize as an identifier in Java. For instance “int while = 20;” is an invalid explanation as while is a reserved word. There are 53 reserved words in Java.

Example –

  • MyVariable
  • Myvariable
  • x
  • i
  • x1
  • i1
  • _myvariable
  • $myvariable
  • sum_of_array
  • hhh123

Let’s Explore Serialization and Deserialization in Java

Examples of invalid identifiers in Java –

  • My Variable // contains a space
  • 123gkk // Begins with a digit
  • a+c // plus sign is not an alphanumeric character
  • variable-2 // hyphen is not an alphanumeric character
  • sum_&_difference // ampersand is not an alphanumeric character

4. Java Reserved Words

Java Reserved words use for represent functionality in programs, they are held in every language.

Literals in Java use for define values.

Identifiers in Java

Java Identifiers – Java Reserved Words

Identifiers in Java utilize by symbol tables in different examining phases(like lexical, syntax, semantic ) of a compiler engineering.

Do You Know 7 Unknown Facts about Null in Java 

Note: The keywords const and goto are reserved, despite the fact that they are not at present utilized. Set up of const, final keyword utilizes. A few keywords like strictfp are incorporated into later forms of Java.

This was all about Identifiers in Java Tutorial. Hope you like our explanation.

5. Conclusion

Hence, we study what are identifiers in Java and how Java Identifiers are case sensitive. We also discussed invalid & valid identifiers in Java. In conclusion, we look at Java Identifier example and Java reserved word. Furthermore, if you have any query, feel free to ask in a comment section.

Related Topic- Pair Class in Java With Techniques

For reference