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

As the name suggests, Identifiers are symbolic names, used for the identification. Identifiers in Java can be a class name, variable name, constant name, array name, methods name, package names. There are some reserved words in Java, which we can’t use as identifiers. In addition, you have to follow some rules before declaring an identifier. So, let’s discuss everything in detail.

Identifiers in Java

Java Identifiers are used to identify a class name, method or variable name and also a label. Let’s discuss with an example-

public class Student
{
    public static void main(String[] args)
    {
          int number = 5;
    }
}

Identifiers in Java with Examples

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

  1. Student is the name of the class.
  2. main is the main method.
  3. String is a predefined class name.
  4. args are the string variables.
  5. number is the name of the variable.

Do you know how variables in Java are declared?

Examples of valid identifiers in Java

  • MyVariable
  • myvariable
  • x
  • i
  • my_Variable
  • _myvariable
  • $myvariable
  • sum_of_array
  • MYVARIABLE
  • dataflair123

Examples of invalid identifiers in Java

  • My Variable (it contains a space)
  • 123gkk (it begins with numbers)
  • a+c (plus sign is not an alphanumeric character)
  • variable-2 (the hyphen is not allowed)
  • sum_&_difference (ampersand is not an alphanumeric character)
  • O’Reilly (the apostrophe is not an alphanumeric character)

Rules for Identifiers in Java

There are certain rules for declaring an identifier. We need to follow these rules otherwise we will get a compile-time error.

  1. A valid identifier has characters [A-Z],[a-z] or numbers [0-9], $ (dollar sign) and _ (underscore). For example, @dataflair is not a valid identifier, because it contains @ which is a special character.
  2. We can’t declare a variable with space. For example, data flair is invalid.
  3. We can’t start an identifier with a number. For example, 222dataflair is not a valid identifier.
  4. As there is no limit on the length of an identifier but it is 4 – 15 letters only appropriate to use.
  5. It is not recommended to use Reserved words as an identifier. For example, int float=5; is not a valid statement.

Wait, don’t forget to learn Interfaces in Java

Java Reserved Words

Java reserved words are the keywords, which are reserved functions. We cannot use these keywords as an identifier because they are already used by the syntax of Java.  Reserved word in Java can’t be declared as a variable (it will throw an error).

Java Reserved words are used to represent functionality in programs. Every keyword has its own meaning. Literals are used to define values.

Java Reserved Words

Note: The keywords const and goto are reserved, despite the fact that they are not at present utilized, const is replaced by a final keyword.

Must check – Literals in Java (integer, floating, char, string, boolean)

Java Quiz

Summary

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

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.