What are valid identifiers in java

Identifiers in Java with Examples

Java Course - Mastering the Fundamentals

Identifiers in Java are names that are helpful in uniquely recognizing a class, a method, a package name, a constant, or a variable. Some words in Java are reserved and cannot be used as identifiers. Certain rules must be followed in Java while we are defining an identifier, or the compiler will throw an error.

Introduction to Identifiers in Java

In general terms, an identifier is a name given to an entity for identification. Java identifiers are any attribute/property of an entity that is utilized for identification. For example, a person’s name, an employee’s employee number, or an individual’s social security number.

Identifiers in Java are names that distinguish between different Java entities, such as classes, methods, variables, and packages. Identifiers include the names of classes, methods, variables, packages, constants, etc. These identifiers are each specified using a specific syntax and naming scheme.

The syntax would depend on the type of identifier. For example, an integer variable in Java can be declared as below,

The variable name is the name given by the programmer to uniquely identify the int variable. Later the programmer can use that variable in his program. Hence, here the variable name is the identifier. These Identifiers follow a certain naming convention/rules when they are named, which we shall look at in the next section of this article.

Читайте также:  Php create from format

Example Java Code Snippet

Identifiers: Below is the list of identifiers that are present in the above sample code.

  • MainClass (Class name)
  • main (Method name)
  • String (Predefined Class name)
  • args (String variable name)
  • var1 (integer variable name)
  • var2 (double variable name)
  • System (Predefined Class name)
  • out (Variable name)
  • println (Method name)

Rules for Identifiers in Java

Below are the rules that need to be followed when defining an identifier in Java. A compile-time error will be produced if any of the following rules are broken.

Rule 1:

Identifiers can only contain alphanumeric characters [a-z] [A-Z] 5 , dollar sign ($) and underscore ( _ ). No other character is allowed. Valid Examples:

Identifier Explanation
NomansLand90 This is valid as it contains only alphanumerics
$Wink1NTomBoy This is valid as characters are alphanumerics, and $

Invalid Examples:

Rule 2:

Identifiers cannot start with a numeric value 8. The starting character should be alphabet [A-Z] [a-z], dollar ($) or underscore ( _ ).

Valid Example:

Invalid Example:

Rule 3:

Identifiers should not contain spaces in their name.

Valid Example:

Invalid Example:

Rule 4:

Java identifiers are case-sensitive.

Eg: ‘Vendetta’ and ‘vendetta’ are considered as two different identifiers.

Rule 5:

The standard convention sets the size of the Java identifiers between 4 – 15, and it is advised to follow that length when defining an identifier. However, there is no upper limit on the length of an identifier.

Rule 6:

Reserve Keywords cannot be used as Identifiers. Java defines a total of 53 reserved keywords that are predefined. In the following part, we’ll examine what a reserved keyword is and a list of predefined terms.

Example: int, float, public, static, etc.

Java Reserved Keywords

Reserved keywords are keywords that are used by java syntax for a particular functionality. Since each of these reserved keywords’ functionality is predefined, these keywords cannot be used for any other purpose.

Java predefines a set of 53 reserved keywords that cannot to used as Identifiers. So these keywords cannot be used as Class names, Method names, Package names, or Variable names. We have some of these keywords in our sample program in the introduction. int, double, public, static, etc., are all examples of reserved keywords.

Example 1: int Var1; → Here, int is a reserved keyword that is used to define an integer variable. The variable name is Var1 , which is an identifier for an int variable.

Example 2: int break; → Here, int is a reserved keyword, and the variable name is break , which is invalid as break itself is a reserved keyword with its predefined functionality in java. Hence this statement will throw an error.

Below is the table of references for all the 53 keywords in Java:

Keywords
abstract default goto package this
assert do if private throw
boolean double implements protected throws
break else import public transient
byte enum int return true
catch extends interface short try
char false instanceof static void
class final long strictfp volatile
const finally native super while
continue float new switch
case for null synchronized

Valid Identifiers in Java

Below is an example list of valid Identifiers that can be used in java

Identifier Explanation
Employee alphabets
EMP12 alphanumerics
$Manager1 alphanumerics and $
_AngryMonk404 alphanumerics and _
Student36Pro9 alphanumerics
A alphabet uppercase A
i alphabet lowercase i
$ Symbol $
final_result_value alphabets and _
SevenUp___7 alphanumerics and _

Invalid Identifiers in Java

Below is an example list of Identifiers that are invalid in java.

Identifier Explanation
@Employee contains invalid character @
12EMP Starts with numerics
&Manager1 contains invalid character &
^AngryMonk404 contains invalid character ^
36-StudentPro9 Starts with numerics and contains invalid character –
5 Is a numeric
final result value Contains spaces

Examples

Let’s discuss few examples in detail related to Java identifiers to get a better understandin:

Valid identifier example:

Explanation: In this example, myVariable is a valid identifier for an integer variable. It is assigned the value 1 0 10 1 0 , and then its value is printed, resulting in the output of 1 0 10 1 0 .

Invalid identifier example

Output/Error

Explanation: In this example, the identifier 1 2 3 a b c 123abc 1 2 3 a b c violates the naming rule as it starts with a digit. Identifiers must start with a letter, underscore, or dollar sign, not a digit.

Using the reserved word as an identifier

Output/Error

Explanation: In this example, the identifier class is a reserved word in Java and cannot be used as an identifier. Attempting to use a reserved word as an identifier will cause a compilation error.

Valid constant identifier example

Explanation: In this example, P I PI P I is a valid constant identifier representing the mathematical constant pi. It is declared as final to indicate that its value cannot be changed. The value of P I PI P I is then printed, resulting in the output of 3.14159.

Valid method identifier example

Explanation: In this example, «printMessage» is a valid identifier for a method. It takes a String parameter «message» and prints it. The method is called with the argument «Hello, world!», resulting in the output of Hello, world!.

FAQs

Q. What are identifiers in Java?

A Identifiers in Java are names used to uniquely identify classes, methods, package names, constants, and variables.

Q. Can reserved words be used as identifiers in Java?

A No, reserved words in Java cannot be used as identifiers. Attempting to do so will result in a compilation error.

Q. What happens if an identifier violates the naming rules in Java?

A If an identifier violates the naming rules in Java, the compiler will throw an error, and the code will not compile. It is important to follow the naming rules to avoid compilation issues.

Q. Why is it important to choose meaningful identifiers in Java?

A Choosing meaningful identifiers improves code readability and maintainability. It makes the code easier to understand for developers and helps in avoiding naming conflicts.

Conclusion

  • Below are some of the main points to keep my mind when declaring identifiers in java
    • Only alphanumeric, $, and _ are valid characters for defining an identifier.
    • Do not start an identifier with a number.
    • Identifier should not contain white spaces in between.
    • Keywords should not be used as identifiers.

    Read Also

    Источник

    Identifiers in Java | Rules for Identifiers

    Scientech Easy

    Identifiers in Java are names that identify elements, such as classes, variables, and methods in a program.

    In other words, an identifier is one that is for naming variables, user-defined methods, classes, objects, parameters, labels, packages, and interfaces in a program.

    In Java, an identifier is a sequence of letters, digits, and underscore characters.

    For example, Student, main, length, breadth, num1, num2, number, area, and so on are names of things that appear in the program.

    In Java programming terminology, such names are called identifiers. They are case sensitive, which means that area, Area, and AREA are not the same. All are different identifiers.

    Rules for naming Identifiers in Java

    Java identifiers obey the following rules that are as follows:

    1. Java identifier is a sequence of characters that consists of letters, digits, underscores (_), and dollar signs ($).

    2. It must start with a letter, an underscore (_), or a dollar sign ($). It cannot begin with a digit.

    3. It cannot be a reserved word.

    4. It cannot be true, false, or null.

    5. It can be of any length.

    Identifiers in Java should be meaningful and can be easily read. There are some naming conventions that are followed by Java programmers. They are as:

    1. The names of all public instance variables and methods begin with a leading lowercase (small) letter. For example, average, sum, etc.

    2. If more than one word is used in a name, the second and subsequent words must start with a leading uppercase letter. For example, dayTemperature, totalMarks, schoolId, etc.

    3. All private and local variables start with only lowercase letters combined with underscores. For example, length, student_id, etc.

    4. All classes and interfaces must begin with a leading uppercase letter and each subsequent word with a leading uppercase letter. For example, Student, HelloJava, Vehicle, etc.

    5. Variables that are initialized with constant values, must start with a leading uppercase letter and can place underscores between words. For example, TOTAL, PRINCIPAL_AMOUNT, etc.

    Note that all the above points are conventions, not rules.

    Q. Which of the following identifiers are valid? Which are Java keywords?

    id, Test, Student, main, a++, ––a, 4#R, $4, #44, apps, class, public, abstract, int, x, y, radius, boolean

    Ans: id, Test, Student, main, $4, apps, x, y, radius are all valid identifiers. Whereas, class, public, boolean, abstract, and int are all reserved keywords.

    FAQs on Identifiers:

    Q1: What is an identifier in Java programming?

    A: An identifier refers to a name used to identify various elements in a Java program, such as variables, methods, classes, interfaces, and packages.

    Q2: Why are meaningful identifiers important in Java?

    A: Meaningful identifiers enhance code understanding as well as make it easier to identify the purpose of variables, methods, or classes.

    Q3: What are the common mistakes to avoid with identifiers?

    A: Common mistakes include using ambiguous names, choosing non-descriptive identifiers, and using single-letter names excessively.

    Q4: How do Java keywords differ from identifiers?

    A: In Java, keywords are reserved words with predefined meanings and we cannot use them as identifiers in a Java program.

    Hope that this tutorial has covered all the important points related to identifiers in Java with suitable examples. I hope that you will have understood the rules of identifiers. In the next tutorial, we will understand comments in Java with the help of various examples.
    Thanks for reading.

    Источник

Оцените статью