Literals in Java
A literal is the source code representation of a fixed value.
Java language specifies five major types of literals:
Integer literals
Floating literals
Character literals
String literals
Boolean literals
Literals can be any number, text, or other information that represents a value.
Integer literals:
Integer data types consist of the following primitive data types: int,long, byte, and short. byte, int, long, and short can be expressed in decimal(base10), hexadecimal(base 16) or octal(base 8) number systems as well.Prefix 0 is used to indicate octal and prefix 0x indicates hexadecimal when using these number systems for literals.
Examples:
int decimal = 100;
int octal = 0144;
int hexa = 0x64;
Floating-point literals:
Floating-point
numbers are like real numbers in mathematics, for example, 4.13179,
-0.000001. Java has two kinds of floating-point numbers: float and
double.The default
type when you write a floating-point literal is double, but you can
designate it explicitly by appending the D (or d) suffix. However, the
suffix F (or f) is appended to designate the data type of a
floating-point literal as float.
0.0314 *10² (i.e 3.14).
6.5E+32 (or 6.5E32) Double-precision floating-point literal
7D Double-precision floating-point literal
.01f Floating-point literal
Character literals:
char data type
is a single 16-bit Unicode character. We can specify a character literal
as a single printable character in a pair of single quote characters
such as 'a', '#', and '3'. You must know about the ASCII character set.
The ASCII character set includes 128 characters including letters,
numerals, punctuation etc. Below table shows a set of these special
characters.
Escape
Meaning
\n
New line
\t
Tab
\b
Backspace
\r
Carriage return
\f
Formfeed
\\
Backslash
\'
Single quotation mark
\"
Double quotation mark
\d
Octal
\xd
Hexadecimal
\ud
Unicode character
String Literals:
The set of
characters in represented as String literals in Java. Always use "double
quotes" for String literals. There are few methods provided in Java to
combine strings, modify strings and to know whether to strings have the
same values.
""
The empty string
"\""
A string containing
"This is a string"
A string containing 16 characters
"This is a " + "two-line string"
actually a string-valued constant expression, formed from two string literals
Boolean Literals:
The values true
and false are treated as literals in Java programming. When we assign a
value to a boolean variable, we can only use these two values. we can't presume that the value of 1 is equivalent to true and 0 is
equivalent to false in Java. We have to use the values true and false to
represent a Boolean value.
Example:
boolean chosen = true;
* Remember that
the literal true is not represented by the quotation marks around it.
The Java compiler will take it as a string of characters, if its in
quotation marks.
Floating-point literals:
Floating-point
numbers are like real numbers in mathematics, for example, 4.13179,
-0.000001. Java has two kinds of floating-point numbers: float and
double.The default
type when you write a floating-point literal is double, but you can
designate it explicitly by appending the D (or d) suffix. However, the
suffix F (or f) is appended to designate the data type of a
floating-point literal as float.
0.0314 *10² (i.e 3.14).
6.5E+32 (or 6.5E32) Double-precision floating-point literal
7D Double-precision floating-point literal
.01f Floating-point literal
6.5E+32 (or 6.5E32) Double-precision floating-point literal
7D Double-precision floating-point literal
.01f Floating-point literal
Character literals:
char data type
is a single 16-bit Unicode character. We can specify a character literal
as a single printable character in a pair of single quote characters
such as 'a', '#', and '3'. You must know about the ASCII character set.
The ASCII character set includes 128 characters including letters,
numerals, punctuation etc. Below table shows a set of these special
characters.
Escape | Meaning |
\n | New line |
\t | Tab |
\b | Backspace |
\r | Carriage return |
\f | Formfeed |
\\ | Backslash |
\' | Single quotation mark |
\" | Double quotation mark |
\d | Octal |
\xd | Hexadecimal |
\ud | Unicode character |
String Literals:
The set of
characters in represented as String literals in Java. Always use "double
quotes" for String literals. There are few methods provided in Java to
combine strings, modify strings and to know whether to strings have the
same values.
"" | The empty string |
"\"" | A string containing |
"This is a string" | A string containing 16 characters |
"This is a " + "two-line string" | actually a string-valued constant expression, formed from two string literals |
Boolean Literals:
The values true
and false are treated as literals in Java programming. When we assign a
value to a boolean variable, we can only use these two values. we can't presume that the value of 1 is equivalent to true and 0 is
equivalent to false in Java. We have to use the values true and false to
represent a Boolean value.
Example:
boolean chosen = true;
* Remember that
the literal true is not represented by the quotation marks around it.
The Java compiler will take it as a string of characters, if its in
quotation marks.
0 comments:
Post a Comment