Literals
A literal in programming is a fixed value written directly in the code. It represents the actual data that a program uses.
- Literals are the actual values.
- Literals will be assigned to variables or constants.
- Literals are also used to perform any operations.
- Constant values in programs are called as Literals.
Types of Literalsβ
- Boolean Literals.
- Character Literals.
- String Literals.
- Integral Literals.
- Floating Literals.
- null Literal.
1.Boolean Literalsβ
- Boolean Literal can be assigned to a boolean type variable.
- There are two boolean literals
- true
- false
Example1.javaβ
class Example1 {
public static void main(String as[]) {
boolean b1 = true;
boolean b2 = false;
System.out.println(b1);
System.out.println(b2);
}
}
Output:
true
false
Example2.javaβ
class Example2 {
public static void main(String as[]) {
boolean b = 1; // boolean will be true or false
System.out.println(b);
}
}
Output:
Compilation error
Example3.javaβ
class Example3 {
public static void main(String as[]) {
boolean b = True; // boolean will be true/false (case sensitive)
System.out.println(b);
}
}
Output: Compilation error
Example4.javaβ
class Example4 {
public static void main(String as[]) {
boolean Tru e = false;
boolean b = True;
System.out.println(b);
}
}
Output:
false
2.Character Literalsβ
- Character Literal is a single character enclosed within single quotation marks.
- Character Literal can be assigned to char type variable.
Example5.javaβ
class Example5 {
public static void main(String as[]) {
char ch1 = 'a';
char ch2 = 'J';
char ch3 = '5';
char ch4 = '*';
System.out.println(ch1);
System.out.println(ch2);
System.out.println(ch3);
System.out.println(ch4);
}
}
Output:
a
J
5
*
Example6.javaβ
class Example6{
public static void main(String as[]){
char ch1 = ''; // Empty
char ch2 = ' '; // Two Space
char ch3 = 'AB'; // error
char ch4 = '\'; // error
char ch5 = ''; // error
}
}
Output: Compilation error
2.1 Escape Sequenceβ
- ESCAPE SEQUENCE is a special notation which is used to represent some special characters which canβt be represented as it is.
Escape Sequence Tableβ
| Escape Sequence | Description |
|---|---|
\t | Tab Space. |
\b | Backspace. |
\n | Newline. |
\r | Carriage return. |
\f | Formfeed. |
\' | Single quote character. |
\" | Double quote character. |
\\ | Backslash character. |
Example8.javaβ
class Example8 {
public static void main(String as[]) {
char ch1 = '\"';
char ch2 = '\\';
System.out.println(ch1);
System.out.println(ch2);
}
}
Example9.javaβ
class Example9{
public static void main(String str[]){
char ch1 = '\t';
char ch2 = '\p';
char ch = '\a';
System.out.println(ch);
}
}
Example10.javaβ
class Example10 {
public static void main(String str[]) {
char ch1 = '\"'; // DOUBLE QUOTES in Single Quotes
char ch2 = '\"'; // slash followed by Double Quotes
System.out.println(ch1);
System.out.println(ch2);
}
}
Example11.javaβ
class Example11{
public static void main(String str[]){
String str = "Welcome to "JavaWorld"";
System.out.println(str);
}
}
Example12.javaβ
class Example12 {
public static void main(String as[]) {
String str = "Welcome to \"JavaWorld\"";
System.out.println(str);
}
}
Example13.javaβ
class Example13{
public static void main(String str[]){
char ch='\u';
System.out.println(ch);
}
}
Output: error
2.2 ASCII Character Setsβ
- ASCII stands for American Standard Code for Information Interchange.
- Every character enclosed in single quotation marks will have an integer equivalent value called as ASCII value.
- ASCII Value Range is 0 β 255.
- ASCII Value can be assigned to a char type variable.
Example14.javaβ
class Example14 {
public static void main(String str[]) {
char ch = '1';
System.out.println(ch);
}
}
Output:
1
Example14.javaβ
class Example14 {
public static void main(String str[]) {
char ch = 1;
System.out.println(ch);
}
}
Output:
Find by yourself
Example15.javaβ
class Example15 {
public static void main(String str[]) {
char ch1 = 65;
char ch2 = 48;
char ch3 = 97;
System.out.println(ch1);
System.out.println(ch2);
System.out.println(ch3);
}
}
Output:
A
0
a
Example16.javaβ
class Example16**{
public static void main(String str[]){
char ch1=0;
char ch2=65535;
System.out.println(ch1);
System.out.println(ch2);
}
}
Output:
Find by yourself
Example17.javaβ
class Example17 {
public static void main(String str[]) {
char ch1 = -1; // error
char ch2 = 65536;
System.out.println(ch1);
System.out.println(ch2);
}
}
Output: error
2.3 UNICODE Charactersβ
- UNICODE stands for UNIversal CODE.
- Every character will have UNICODE value.
UNICODE Notationβ
Syntax:
\uXXXX - X will be hexadecimal digit
Starts with \u followed by four hexadecimal digits.
UNICODE Rangeβ
\u0000 (0) to \uFFFF (65535)
Example18.javaβ
class Example18 {
public static void main(String str[]) {
char ch1 = '\u0061';
char ch2 = '\u0062';
System.out.println(ch1);
System.out.println(ch2);
char ch3 = '\u0041';
char ch4 = '\u0042';
System.out.println(ch3);
System.out.println(ch4);
}
}
Example19.javaβ
class Example19 {
public static void main(String str[]) {
int a = '\u0061';
int b = '\u0062';
System.out.println(a);
System.out.println(b);
int c = '\u0041';
int d = '\u0042';
System.out.println(c);
System.out.println(d);
}
}
Example20.javaβ
class Example20{
public static void main(String str[]){
int a = \u0037;
int b = '\u0037';
System.out.println(a);
System.out.println(b);
}
}
2.4 Octal Value as char typeβ
- Octal value can be assigned to char type.
OCTAL Notationβ
Syntax:
\DDD - D will be octal digit
Starts with \ followed by three octal digits.
OCTAL Rangeβ
- Range in Decimal: 0 β 255
- Range in Octal:
\0 β \377
Character β ASCII β OCTAL β UNICODEβ
Digits (0β9)β
| Character | ASCII | OCTAL | UNICODE |
|---|---|---|---|
| 0 | 48 | 060 | \u0030 |
| 1 | 49 | 061 | \u0031 |
| 2 | 50 | 062 | \u0032 |
| 3 | 51 | 063 | \u0033 |
| 4 | 52 | 064 | \u0034 |
| 5 | 53 | 065 | \u0035 |
| 6 | 54 | 066 | \u0036 |
| 7 | 55 | 067 | \u0037 |
| 8 | 56 | 070 | \u0038 |
| 9 | 57 | 071 | \u0039 |
Uppercase Letters (AβZ)β
| Character | ASCII | OCTAL | UNICODE |
|---|---|---|---|
| A | 65 | 101 | \u0041 |
| B | 66 | 102 | \u0042 |
| C | 67 | 103 | \u0043 |
| D | 68 | 104 | \u0044 |
| E | 69 | 105 | \u0045 |
| F | 70 | 106 | \u0046 |
| G | 71 | 107 | \u0047 |
| H | 72 | 110 | \u0048 |
| I | 73 | 111 | \u0049 |
| J | 74 | 112 | \u004A |
| K | 75 | 113 | \u004B |
| L | 76 | 114 | \u004C |
| M | 77 | 115 | \u004D |
| N | 78 | 116 | \u004E |
| O | 79 | 117 | \u004F |
| P | 80 | 120 | \u0050 |
| Q | 81 | 121 | \u0051 |
| R | 82 | 122 | \u0052 |
| S | 83 | 123 | \u0053 |
| T | 84 | 124 | \u0054 |
| U | 85 | 125 | \u0055 |
| V | 86 | 126 | \u0056 |
| W | 87 | 127 | \u0057 |
| X | 88 | 130 | \u0058 |
| Y | 89 | 131 | \u0059 |
| Z | 90 | 132 | \u005A |
Lowercase Letters (aβz)β
| Character | ASCII | OCTAL | UNICODE |
|---|---|---|---|
| a | 97 | 141 | \u0061 |
| b | 98 | 142 | \u0062 |
| c | 99 | 143 | \u0063 |
| d | 100 | 144 | \u0064 |
| e | 101 | 145 | \u0065 |
| f | 102 | 146 | \u0066 |
| g | 103 | 147 | \u0067 |
| h | 104 | 150 | \u0068 |
| i | 105 | 151 | \u0069 |
| j | 106 | 152 | \u006A |
| k | 107 | 153 | \u006B |
| l | 108 | 154 | \u006C |
| m | 109 | 155 | \u006D |
| n | 110 | 156 | \u006E |
| o | 111 | 157 | \u006F |
| p | 112 | 160 | \u0070 |
| q | 113 | 161 | \u0071 |
| r | 114 | 162 | \u0072 |
| s | 115 | 163 | \u0073 |
| t | 116 | 164 | \u0074 |
| u | 117 | 165 | \u0075 |
| v | 118 | 166 | \u0076 |
| w | 119 | 167 | \u0077 |
| x | 120 | 170 | \u0078 |
| y | 121 | 171 | \u0079 |
| z | 122 | 172 | \u007A |
Example21.javaβ
class Example21 {
public static void main(String str[]) {
char ch1 = '\101';
char ch2 = '\103';
int a = '\101';
System.out.println(ch1);
System.out.println(ch2);
System.out.println(a);
}
}
Output:
A
C
65
Example22.javaβ
class Example22 {
public static void main(String str[]) {
char ch1 = '\101';
char ch2 = '\420'; // invalid range (octal > 377)
}
}
Output:
Compilation error
3 String Literalsβ
- String Literal is a collection of zero or more characters enclosed between double quotation marks.
- String Literals can be assigned to reference variable of type String.
- String Literal will be represented as char array in memory.
Example23.javaβ
class Example23 {
public static void main(String str[]) {
String str1 = null;
System.out.println(str1);
int x = str1.length(); // runtime error
System.out.println(x);
}
}
Output:
Runtime Exception
Null Pointer Exception
Example24.javaβ
class Example24 {
public static void main(String str[]) {
String str1 = "";
System.out.println(str1);
int x = str1.length();
System.out.println(x);
String str2 = "JavaWorldINDIA";
System.out.println(str2);
int y = str2.length();
System.out.println(y);
String str3 = "JavaWorld@india";
System.out.println(str3);
int z = str3.length();
System.out.println(z);
}
}
Output:
(empty line)
0
JavaWorldINDIA
8
JavaWorld@india
11
Example25.javaβ
class Example25 {
public static void main(String str[]) {
String dir1 = "D:\new\test\batch";
System.out.println(dir1);
String dir2 = "D:\\new\\test\\batch";
System.out.println(dir2);
}
}
Notesβ
- Difference between null string and empty string
nullβ no memory allocated β accessing methods causes exception""(empty string) β memory allocated β valid object
4.Integer Literalsβ
- Integer Literals can be assigned to one of the integral data types: byte, short, int, long.
- Integer Literal size depends on the integral data type you are using.
- Default type of Integer Literal is int.
There are four types of Integer Literals:β
- Decimal Literals
- Octal Literals
- Hexadecimal Literals
- Binary Literals (From Java 7)
4.1 Decimal Literalsβ
- Decimal Literal is a valid number from the decimal number system.
- The base (radix) of decimal number system is 10 (digits 0 to 9).
- Decimal literals must not start with 0.
Examples:β
12345
98787898
Example26.javaβ
class Example26 {
public static void main(String str[]) {
byte b = 65;
System.out.println(b);
short s = 1234;
System.out.println(s);
int i = 123431;
System.out.println(i);
}
}
Output:
65
1234
123431
Example27.javaβ
class Example27 {
public static void main(String str[]) {
byte b = 128; // Error (out of range)
short s = 32768; // Error (out of range)
int i = 2147483648; // Error (out of range)
long a = 2147483648; // Error (L is required)
}
}
Reason: Values exceed the allowed range of respective data types.
Example28.javaβ
class Example28 {
public static void main(String str[]) {
long a = 2147483648L;
System.out.println(a);
}
}
Output:
2147483648
Example29.javaβ
class Example29 {
public static void main(String str[]) {
System.out.println(9880979999);
}
}
Output:
Compilation Error (value out of range for int)
Important Pointsβ
- Integer literals are int by default
- Use L or l for long values
Example:
2147483648L - If value exceeds int range β must use long
- Each data type has a fixed range, exceeding it causes compile-time error
Example30.javaβ
class Example30 {
public static void main(String str[]) {
System.out.println(9880979999L);
}
}
Output:
9880979999
Example31.javaβ
class Example31 {
public static void main(String str[]) {
int a = 099; // Error
System.out.println(a);
}
}
Reason:
Numbers starting with 0 are treated as octal, and 9 is not allowed β compilation error
4.2 Octal Literalsβ
- Octal Literal is a valid number from the octal number system
- Base (radix) = 8 β digits allowed: 0 to 7
- Octal literal must start with 0 (zero)
Examples:β
0101
0377
0345245
Example32.javaβ
class Example32 {
public static void main(String str[]) {
int a = 1010; // Decimal
int b = 0101; // Octal
System.out.println(a);
System.out.println(b);
}
}
Output:
1010
65
Explanation:
1010β decimal β 10100101β octal β (1Γ64 + 0Γ8 + 1 = 65)
4.3 Hexadecimal Literalsβ
- Hexadecimal Literal is a valid number from hexadecimal number system
- Base (radix) = 16
- Allowed digits:
0β9 and AβF (or aβf)
- Must start with:
0x or 0X
Example:β
0x123af
Example33.javaβ
class Example33 {
public static void main(String str[]) {
int a = 0xA;
int b = 0x61;
int c = 0xface;
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
Output:
10
97
64206
Explanation:
0xAβ 100x61β 970xfaceβ 64206
Key Takeawaysβ
0prefix β Octal0x / 0Xβ Hexadecimal- No prefix β Decimal
- Invalid digits in octal β compile-time error
- Always use
Lfor large numbers (long type)
4.4 Binary Literals (From Java 7)β
- Binary Literal is a valid number from the binary number system.
- Base (radix) = 2 β only digits 0 and 1 are allowed.
- Binary literal must start with 0B or 0b
Examples:β
0b1010
0B111001101
Example34.javaβ
class Example34 {
public static void main(String str[]) {
int a = 101; // Decimal
int b = 0b101; // Binary
System.out.println(a);
System.out.println(b);
}
}
Output:
101
5
Example35.javaβ
class Example35 {
public static void main(String str[]) {
int a = 0b102; // Error (invalid binary digit '2')
System.out.println(a);
}
}
Reason: Binary literals can contain only 0 and 1.
Example36.javaβ
class Example36 {
public static void main(String str[]) {
int a = 101; // Decimal
int b = 0101; // Octal
int c = 0x101; // Hexadecimal
int d = 0b101; // Binary
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
}
}
Output:
101
65
257
5
5. Floating Point Literalsβ
- Floating Point Literal can be assigned to:
floatdouble
- Size depends on the data type used
- Default type is double
Rules:β
- You can optionally use D/d suffix for double
- You must use F/f suffix for float
Floating Point Representationsβ
- Standard Floating Point Notation
- Exponent / Scientific Notation
- Hexadecimal Notation (From Java 5)
Examples:β
12987.63
129.8763
Key Pointsβ
0b / 0Bβ Binary- Only
0and1allowed in binary - Default floating type β
double - Use
fβ float literal - Supports scientific notation like:
1.2e3// 1200
5.1 Standard Floating Point Notationβ
A floating point number consists of:
- Whole number part
- Decimal point
- Fractional part
Example:β
12.876
- 12 β integer part
- .876 β fractional part
Example37.javaβ
class Example37 {
public static void main(String str[]) {
double d1 = 1234.567;
double d2 = 123.4567;
System.out.println(d1);
System.out.println(d2);
}
}
Output:
1234.567
123.4567
Example38.javaβ
class Example38 {
public static void main(String str[]) {
float f1 = 12987.63; // Error
System.out.println(f1);
}
}
Reason:
- Default type of floating literal is double
- Float must use F or f suffix
Example39.javaβ
class Example39 {
public static void main(String str[]) {
float f1 = 12987.63F;
System.out.println(f1);
}
}
Output:
12987.63
5.2 Scientific / Exponent Notation**β
- Exponent is represented using E or e
- Format:
number Γ 10^power
Examples:β
123.4567e2 -> 123.4567 Γ 10^2
123.4567E+2 -> 123.4567 Γ 10^2
12345.67e-2 -> 12345.67 Γ 10^-2
Example40.javaβ
class Example40 {
public static void main(String str[]) {
float f = 129.8763e2F; // 12987.63
double d1 = 129.8763e+2; // 12987.63
double d2 = 12987.63e-2; // 129.8763
System.out.println(f);
System.out.println(d1);
System.out.println(d2);
}
}
Output:
12987.63
12987.63
129.8763
Key Pointsβ
- Default floating type β double
- Use
Fβ float - Scientific notation uses
eorE - Positive exponent β multiply
- Negative exponent β divide
5.3 Hexadecimal Floating-Point Literals (From Java 5)β
- It allows floating-point literals (float / double) to be written in base 16 (hexadecimal) instead of base 10.
Example:β
double x=0xAp0;
A(hex) = 10 (decimal)p0means Γ 2β°
π Result:
10 * 2^0 = 10.0
Example41.javaβ
class Example41 {
public static void main(String str[]) {
double d1 = 0xAp0;
System.out.println(d1);
double d2 = 0xAp1;
System.out.println(d2);
double d3 = 0xAp2;
System.out.println(d3);
double d4 = 0xA9.0P0;
System.out.println(d4);
double d5 = 0xA9.P0;
System.out.println(d5);
}
}
Output:
10.0
20.0
40.0
169.0
169.0
Example42.javaβ
class Example42 {
public static void main(String str[]) {
double d = 0B1011; // Binary literal
System.out.println(d);
}
}
Output:
11.0
Example43.javaβ
class Example43 {
public static void main(String str[]) {
double d = 0B1011.1; // Invalid
System.out.println(d);
}
}
Compilation Error
- Binary literals cannot be used with floating-point numbers
6. Null Literalβ
nullis a special literal value- It is the default value for reference variables
- If a variable is
null, it means:- It does not refer to any object
- No memory/reference is assigned
Example44.javaβ
class Example44 {
static String str1;
public static void main(String str[]) {
String str2 = null;
System.out.println(str1);
System.out.println(str2);
}
}
Output:
null
null
Key Pointsβ
Hexadecimal Floating:β
- Starts with
0xor0X - Uses
porPfor exponent (base 2) - Example:
0xAp1 = 10 Γ 2ΒΉ = 20
Binary:β
- Works only for integer literals
- Not valid for floating-point
Null:β
- Default value of objects / reference types
- Does not point to any memory location
2.7.7 Underscore in Numeric Literals
- To represent large numbers, we usually use commas (,) for readability.
Example:β
2,45,12,452
- From Java 7 onwards, you can use underscore (_) instead of commas.
Example:β
2_45_12_452
Example44.javaβ
class Example44 {
public static void main(String str[]) {
int a = 2_45_12_452;
System.out.println(a);
}
}
Output:
24512452
Example45.javaβ
class Example45 {
public static void main(String str[]) {
int _12 = 9898;
System.out.println(_12);
int a = _12;
System.out.println(a);
}
}
Output:
9898
9898
Example46.javaβ
class Example46 {
public static void main(String str[]) {
int a = _234; // Invalid
System.out.println(a);
}
}
Compilation Error
Example47.javaβ
class Example47 {
public static void main(String str[]) {
int a = 0_77;
System.out.println(a);
int b = 0b1_01_01;
System.out.println(b);
int c = 0x1_A_F;
System.out.println(c);
double d = 1_2_3.4_5_6;
System.out.println(d);
}
}
Output:
63
21
431
123.456
Example48.javaβ
class Example48 {
public static void main(String str[]) {
int a = 234_; // Invalid
int b = 0b_101; // Invalid
int c = 0x_1_A_F; // Invalid
double d1 = 123_.45; // Invalid
double d2 = 123._45; // Invalid
}
}
Compilation Errors
Rules for Using Underscore (_)β
-
You can use
_between digits to improve readability1_000_000 -
Cannot use
_at the beginning of a numberint a=_123;// Invalid -
Cannot use
_at the end of a numberint a=123_;// Invalid -
Cannot use
_just after prefix- Binary (
0b) - Hex (
0x)
int a=0b_101;// Invalidint b=0x_FF;// Invalid - Binary (
-
Cannot use
_near decimal pointdouble d=123_.45;// Invaliddouble d=123._45;// Invalid -
Can be used in:
- Decimal numbers β
1_000 - Binary β
0b1010_0101 - Hex β
0xFF_AA - Floating β
123.45_67
- Decimal numbers β
Key Ideaβ
- Underscore is only for readability
- It does not affect the actual value