Java Tutorial

Operators

Flow Control

String

Number and Date

Built-in Classes

Array

Class and Object

Inheritance and Polymorphism

Exception Handling

Collections, Generics and Enumerations

Reflection

Input/Output Stream

Annotation

Java Identifiers And Keywords

In Java, identifiers and keywords are used to give names to various elements in the code, such as classes, methods, and variables. Understanding the rules and conventions for naming these elements is essential for writing clean and maintainable code. In this tutorial, we'll discuss the rules for creating identifiers, the list of reserved keywords, and some naming conventions.

  • Java Identifiers

An identifier is a name given to elements in a program, such as classes, methods, variables, and interfaces. Identifiers must follow certain rules:

  • Identifiers can contain letters (a-z, A-Z), digits (0-9), underscores (_), and dollar signs ($).
  • Identifiers cannot start with a digit.
  • Java identifiers are case-sensitive, meaning "myVariable" and "MyVariable" are considered different identifiers.
  • There is no limit on the number of characters an identifier can have, but it's best to keep them concise and descriptive.
  • Identifiers cannot be any of the reserved keywords in Java (explained below).

Examples of valid identifiers:

  • myVariable
  • MyClass
  • _temp
  • $value

Examples of invalid identifiers:

  • 123variable (starts with a digit)
  • for (reserved keyword)
  • my-variable (contains an invalid character, '-')
  • Java Keywords

Keywords are reserved words in Java that have special meanings and cannot be used as identifiers. They are used to define the structure and syntax of a Java program.

List of Java keywords:

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

Note: const and goto are reserved but not used in Java.

  • Naming Conventions

Java has established naming conventions that help make code more readable and maintainable. Following these conventions is considered good practice:

  • Class names: Use PascalCase (the first letter of each word is capitalized) and should be nouns, e.g., MyClass, User, InputStream.
  • Interface names: Use PascalCase and should be adjectives or noun phrases, e.g., Comparable, Serializable, ActionListener.
  • Method names: Use camelCase (the first letter of each word after the first word is capitalized) and should be verbs, e.g., doSomething, calculateSum.
  • Variable names: Use camelCase and should be descriptive, e.g., counter, firstName, userList.
  • Constant names: Use uppercase letters with words separated by underscores (_) and should be descriptive, e.g., MAX_VALUE, MINIMUM_SIZE.

In conclusion, Java identifiers and keywords are essential elements in writing clean and maintainable code. Identifiers give names to various elements in the code, such as classes, methods, and variables, while keywords define the structure and syntax of a Java program. Following the rules for creating identifiers, understanding the list of reserved keywords, and adhering to established naming conventions will help you write better Java code.

  1. Legal and illegal identifiers in Java

    Legal identifiers follow naming conventions, while illegal identifiers violate these rules. For example:

    // Legal identifiers
    int myVariable;
    String _name;
    double $value;
    
    // Illegal identifiers
    int 2count;  // starts with a digit
    float my-variable;  // contains hyphen
    
  2. Reserved keywords in Java programming

    Java has reserved keywords that cannot be used as identifiers. Examples include if, while, class, etc.

  3. Using reserved words as identifiers in Java

    Attempting to use reserved words as identifiers will result in compilation errors.

    int class = 10;  // Compilation error, 'class' is a reserved keyword
    
  4. Handling naming conflicts with Java identifiers

    When naming conflicts arise, use fully qualified names or aliases to disambiguate.

    import java.util.List;
    
    public class MyClass {
        List<String> myList;
    }
    
  5. CamelCase and snake_case in Java identifiers

    CamelCase: myVariableName Snake_case: my_variable_name

    Choose one style and stick to it for consistency in your codebase.

  6. Keyword usage in Java and its impact on code readability

    Using keywords as identifiers can lead to confusion and reduced code readability.

    int class = 5;  // Confusing, 'class' is a keyword
    

    Choose descriptive alternatives to enhance code readability.