Introduction to Literals in Java
In the overall architecture of a Java program, literals in Java are the smallest unit of data that a program will use. While a variable is a container for data, a literal is the data that is placed in that container. A literal is a data construct that is used for boolean, integer, character, or string data types.
In essence, if you have a line of code that says int count = 50;, the literal is the number 50, and the count is the variable identifier. Explore more with our Java tutorial for beginners.
1. Integer Literals
An integer literal is a series of characters that are all digits, without any fractional data. In Java, integer literals are supported in four different number systems.
| System | Prefix | Allowed Digits | Example |
| Decimal | None | 0-9 | 100 |
| Binary | 0b or 0B | 0 and 1 | 0b11001 |
| Octal | 0 | 0-7 | 144 |
| Hexadecimal | 0x or 0X | 0-9 and A-F | 0x64 |
The long Suffix
By default, all integer literals will be assumed to be 32-bit integers. However, if you want to hold a number larger than 231-1, you will need to append an L or as a long (64-bit).
int hexVal = 0x1a; // 26 in decimal
int binVal = 0b11010; // 26 in decimal
long bigVal = 99999999L; // Long literal
2. Floating Point Literals
These are used for representing numbers with decimal points. They can be represented in standard decimal form or scientific notation using “e” or “E”.
- Double Literals: The default literal type. It can be optionally written using “D” or “d”.
- Float Literals: The “f” or “F” suffix is required. Failure to do so will result in a compilation error because of “lossy conversion.”
Code Example:
double pi = 3.14159;
double scientific = 1.23e4; // 12300.0
float price = 19.99f; // Required ‘f’
Learn with complete hands-on exposure in our software training institute in Chennai.
3. Character Literals
A character literal is represented as a single character enclosed in single quotes. The single quotes can contain any character. Java supports Unicode character sets, so we can use almost any language’s character set.
Special Representations
- Single Quote: ‘a’, ‘$’, ‘7’
- Unicode: ‘\u0061’ (the letter ‘a’)
- Escape Sequences: Used for characters that cannot be typed directly.
| Escape Sequence | Description |
| \n | New line |
| \t | Tab space |
| \b | Backspace |
| \’ | Single quote |
| \” | Double quote |
| \\ | Backslash |
4. String Literals
A string literal is a series of characters enclosed between two double quote marks (” “). Unlike other programming languages, Java does not allow strings and characters to be used interchangeably. It does not allow single quote marks for strings.
String Pool Concept
Java uses a special technique to store string literals. This technique is known as the String Constant Pool. Suppose we write a program: String s = “Hello”;. Then Java checks if “Hello” is present in a special memory location.
Code Example:
String greeting = “Hello World!”;
String path = “C:\\Users\\Desktop”; // Using escape characters
Practice more with our Java project ideas.
5. Boolean and Null Literals
This is the simplest form of literals used in Java.
- Boolean Literals: Only two boolean literals exist: true and false. They don’t relate to any numbers (in contrast to C/C++, where 1 and 0 are used).
- Null Literals: This is indicated by the keyword null. This keyword is used for reference variables that don’t point to any object.
6. Underscores in Numeric Literals
Java provides a feature that allows you to make your code easier to read by putting an underscore between any two digits in a numeric literal. This feature is not used by the compiler.
Rules for Using Underscores:
- It can’t be at the start or end of a number.
- Not allowed immediately after a decimal point.
- Not allowed immediately before an L or F.
Code Example:
long creditCardNumber = 1234_5678_9012_3456L;
float bytes = 0b1101_0101_1100_0010f;
double socialSecurity = 999_00_1234;
7. Class Level View: Literals and Constants
A constant is a variable declared with final and holding a literal.
// ’10’ is the literal
// ‘MAX_USERS’ is the constant
final int MAX_USERS = 10;
| Literal Category | Examples | Default Type |
| Integer | 10, 0x7B, 0b101 | int |
| Floating Point | 10.5, 12.3f, 4.5e2 | double |
| Character | A’, ‘\u0041’, ‘\n’ | char |
| String | “Java”, “123”, “” | String |
| Boolean | true, false | boolean |
Conclusion
The literals need to be understood for memory management and to avoid precision errors, especially for float and double. By using underscores and prefixes, you make your code easier to maintain and avoid confusion with magic numbers. Learn further with our comprehensive Java training in Chennai.
