Skip to main content

Literals

A literal in programming is a fixed value written directly in the code. It represents the actual data that a program uses.

info
  • 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​

  1. Boolean Literals.
  2. Character Literals.
  3. String Literals.
  4. Integral Literals.
  5. Floating Literals.
  6. null Literal.

1.Boolean Literals​

  • Boolean Literal can be assigned to a boolean type variable.
  • There are two boolean literals
    1. true
    2. 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 SequenceDescription
\tTab Space.
\bBackspace.
\nNewline.
\rCarriage return.
\fFormfeed.
\'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)​

CharacterASCIIOCTALUNICODE
048060\u0030
149061\u0031
250062\u0032
351063\u0033
452064\u0034
553065\u0035
654066\u0036
755067\u0037
856070\u0038
957071\u0039

Uppercase Letters (A–Z)​

CharacterASCIIOCTALUNICODE
A65101\u0041
B66102\u0042
C67103\u0043
D68104\u0044
E69105\u0045
F70106\u0046
G71107\u0047
H72110\u0048
I73111\u0049
J74112\u004A
K75113\u004B
L76114\u004C
M77115\u004D
N78116\u004E
O79117\u004F
P80120\u0050
Q81121\u0051
R82122\u0052
S83123\u0053
T84124\u0054
U85125\u0055
V86126\u0056
W87127\u0057
X88130\u0058
Y89131\u0059
Z90132\u005A

Lowercase Letters (a–z)​

CharacterASCIIOCTALUNICODE
a97141\u0061
b98142\u0062
c99143\u0063
d100144\u0064
e101145\u0065
f102146\u0066
g103147\u0067
h104150\u0068
i105151\u0069
j106152\u006A
k107153\u006B
l108154\u006C
m109155\u006D
n110156\u006E
o111157\u006F
p112160\u0070
q113161\u0071
r114162\u0072
s115163\u0073
t116164\u0074
u117165\u0075
v118166\u0076
w119167\u0077
x120170\u0078
y121171\u0079
z122172\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:​

  1. Decimal Literals
  2. Octal Literals
  3. Hexadecimal Literals
  4. 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 β†’ 1010
  • 0101 β†’ 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 β†’ 10
  • 0x61 β†’ 97
  • 0xface β†’ 64206

Key Takeaways​

  • 0 prefix β†’ Octal
  • 0x / 0X β†’ Hexadecimal
  • No prefix β†’ Decimal
  • Invalid digits in octal β†’ compile-time error
  • Always use L for 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:
    • float
    • double
  • 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​

  1. Standard Floating Point Notation
  2. Exponent / Scientific Notation
  3. Hexadecimal Notation (From Java 5)

Examples:​

12987.63
129.8763

Key Points​

  • 0b / 0B β†’ Binary
  • Only 0 and 1 allowed 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 e or E
  • 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)
  • p0 means Γ— 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​

  • null is 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 0x or 0X
  • Uses p or P for 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 (_)​

  1. You can use _ between digits to improve readability 1_000_000

  2. Cannot use _ at the beginning of a number

    int a=_123;// Invalid
  3. Cannot use _ at the end of a number

    int a=123_;// Invalid
  4. Cannot use _ just after prefix

    • Binary (0b)
    • Hex (0x)
    int a=0b_101;// Invalid
    int b=0x_FF;// Invalid
  5. Cannot use _ near decimal point

    double d=123_.45;// Invalid
    double d=123._45;// Invalid
  6. Can be used in:

    • Decimal numbers β†’ 1_000
    • Binary β†’ 0b1010_0101
    • Hex β†’ 0xFF_AA
    • Floating β†’ 123.45_67

Key Idea​

  • Underscore is only for readability
  • It does not affect the actual value