Basics of Java Programming: Keywords, Literals, and Identifiers
Basics of Java Program
The basics of a Java program include keywords, literals, and identifiers. These are the fundamental building blocks used while writing Java code.
1. Keywords
Keywords are reserved words in Java that have predefined meanings and functionality for the Java compiler.
Key points:
Keywords must be written in lowercase.Java has 50 reserved keywords.Out of these, 48 keywords have predefined meaning and functionality.The remaining 2 reserved words (goto and const) are not used and have no functionality.Examples of Java keywords:
- class
- static
- final
- public
- void
Keywords cannot be used as variable names, method names, or identifiers.
2. Literals
Literals are fixed values that are directly stored in a Java program.
Java supports five types of literals:
Integer Literals:
Eg:- 1, 25, 100
Floating-Point Literals:
Eg:- 1.5, 2.75
Character Literals:
Eg:- 'a', 'Z'
String Literals:
Eg:- "Java", "Programming"
Boolean Literals:
Eg:- true, false
Literals are used to assign values to variables in a Java program.
3. Identifiers
Identifiers are names given to variables, methods, classes, and objects in Java.
Rules for identifiers:
- The first character must be an alphabet (A–Z or a–z), $, or _.
- Identifiers cannot start with a number.
- After the first character, digits can be used.
- Spaces are not allowed.
- Keywords cannot be used as identifiers.
- Operators cannot be used as identifiers.
🔹 Note on underscore (_):
- In JDK 1.7 and earlier, _ can be used as an identifier.
- From JDK 8 onwards, _ alone is not allowed as an identifier.
Examples of valid identifiers:
- totalAmount
- $count
- student1
Examples of invalid identifiers:
- 1value
- total amount
- class
Comments
Post a Comment