Java supports several types of literals including integer, real, character, string, and backslash literals. Integer literals can be decimal, octal, or hexadecimal and consist of digits with an optional sign. Real literals contain fractional parts. Character literals are single characters within single quotes. String literals are sequences of characters within double quotes. Backslash literals use escape sequences to represent special characters like newlines and tabs.
Java Literals
• Literalin Java refer to fixed values that do not
change during the execution of the program
• Java supports several types of constants
• Integer Literal
• Real Literal
• Character Literal
• String Literal
• Backslash Literal
1
2.
Integer Literals
• Aninteger literal refers to sequence of digits
• Integers are of three types
• They are
• Decimal Integer
• Octal Integer
• Hexa Decimal Integer
2
3.
Decimal Integer
• DecimalIntegers consists of a set of digits from 0
to 9,preceeded by an optional minus(-) sign
eg. : +999,-999
• Embedded spaces ,commas and non-digit
characters are not permitted in between digits
eg. 99 999, 99,999 , $999 are illegal
numbers.
3
4.
Octal Integer
• Anoctal integer consists of any combination of
digits from 0 to 7 with a leading zero(0)
eg. 045,063,026 etc.,
4
5.
Hexa Decimal Integer
•A sequence of digits preceded by 0x or 0X is
considered as hexa decimal integers
• They may also include alphabets from A to F or a
to f
• Letters from a to f represents the numbers from 10
to 15
eg. 0xd,0XF etc.,
5
6.
Real Literals
• Numberscontaining fractional parts are called real
literals
eg. 9.999,0.999 etc.,
• The numbers are also expressed in exponential
notation
eg. 999.9 may be written as 9999E-3.E-3 mean
multiply by 10-3
6
7.
Single Character Literals
•A single character constant contains a single
character enclosed with a pair of single quote
marks „ „
eg. „m‟
7
8.
String Literals
• AString Literal is a sequence of characters
enclosed between double quote marks “ “
• The characters may be alphabets, digits, special
characters and blank spaces
eg. “neelu”
8
9.
Backslash Literals
• Java supports some special back slash character
literals
• They are used in output formatting methods
eg. n stands for new line
t stands for horizontal tab
b stands for blank space
v stands for vertical tb
a stands for a small beep
9