English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Literals are the source code representation of fixed values. They can be represented directly in the code without any computation.
Literals can be assigned to any primitive type variable. For example.
byte a = 68; char a = 'A';
byte, int, long and short can also be used in decimal (base10), hexadecimal (base16) or octal (base8) representation of the number.
When these number systems are used for literals, the prefix 0 indicates octal, and the prefix 0x indicates hexadecimal. For example-
int decimal = 100; int octal = 0144; int hexa = 0x64;
In Java, string literals are specified by enclosing a sequence of characters in a pair of double quotes, just like in most other languages. An example of a string literal is.
"Hello World" "two\nlines" "\"This is in quotes\""
The string and character types can contain any Unicode character. For example.
char a = '\u0001; String a = "\u0001";
The Java language also supports some special escape sequences for String and char literals. They are-
Number | Notation and Character Representation |
---|---|
1Each | \n Line Feed (0x0a) |
2 | \r Carriage Return (0x0d) |
3 | \F Form Feed (0x0c) |
4 | \b Backspace Key (0x08) |
5 | \s Space (0x20) |
6 | \t Tag |
7 | \“ Double Quote |
8 | \' Single Quote |
9 | \\\ Backslash |
10 | \ ddd Octal Character (ddd) |
11 | \ uxxxx Hexadecimal Unicode Character (xxxx) |