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
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.
An identifier is a name given to elements in a program, such as classes, methods, variables, and interfaces. Identifiers must follow certain rules:
Examples of valid identifiers:
Examples of invalid identifiers:
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.
Java has established naming conventions that help make code more readable and maintainable. Following these conventions is considered good practice:
MyClass
, User
, InputStream
.Comparable
, Serializable
, ActionListener
.doSomething
, calculateSum
.counter
, firstName
, userList
.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.
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
Reserved keywords in Java programming
Java has reserved keywords that cannot be used as identifiers. Examples include if
, while
, class
, etc.
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
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; }
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.
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.