vishmi tharuka
3 min readNov 18, 2023

Java Literals are numbers, text, or anything that represents a value. In other words, Literals in Jasva are the constant values assignesd to the variables.It is also called a constant .

Types of Literals :

01. Integer

02. Floating Point

03. Character

04. Boolean

05. String

01. Integer Literals

For integer data types(byte, short, int, long) we can specify literals in 4 ways,

01.) Decimal (Base10 ) Literals

In this form the allowed digits are 0–9.

02.) Octal (Base 8 ) Literals

In this form the allowed digits are 0–7.

special : The octal numbers should be prefix with 0.

octal value of 100

03.) Binary (Base 2 ) Literals

From 1.7 onwards we can specify literals value even in the binary form. Also allowed digits are 0 and 1.

special : The binary number should be prefix “ob” or “OB”.

binary value of 100

04.) Hexadecimal(Base 16) Literals

In this file format the allowed digits 0–9 ans the characters are A — F(A=10, B=11, C=12, D=13, E=14, F=15 ).we can use both uppercase and lowercase characters.

As we know that Java is a case sensitive programming Language.But here Java isn’t case sensitive.

special : The hexadecimal number should be prefix with “ox” or “OX”.

hexadecimal value of 100

02. Floating point Literals

For floating point data types, we can specify literals in only decimal form and we can’t specify in octa and hexa-decimal forms.

03. Character Literals

For character data type we can specify literals in 05 ways,

01.Single quote :
we can specify literals to character data type as single character within single quote.

example :- ‘a’ , ‘b’ , ‘c’ , ‘d’

02.Unicode Representation :

we can specify character literals in unicode presentation ‘\uxxxx’. Here xxxx represent 4 hexa-decimal numbers.\

‘\u0061’ represent a

03.Escape Sequence :

every escape character can be specify as a char literals.

‘\n’ ; — new line

‘\t’ ; — tab

‘\o’ — octal

‘\u’ — unicode

‘\\’ — backslash

‘\’ ’ — single quote

‘\” ’ — double quote

‘\b’ — backspace

‘\f’ — form feed

‘\r’ — carriage return