Literals :
The Literals are the constants in Java. There are
following types of Literals . Constants are one
whose value is fixed.
Difference Between Constants and Variables :
(a) The value of the constant is fixed or remains
the same , while the value of the variable can
vary or change.
(b) The variable occupies memory and the
amount of memory it occupies will depends on
its datatype.While , the constant doesnot
occupies memory.
(c) The value of the constant is accessed
directly, while the value of the variable is to be
lookedup in the primary memory.
e.g. In C,
#define MAX 50
In our program ,
MAX=60
So, it will give a compile time error,
LVALUE required.
As , MAX will get automatically replaced
by 50, so the statement MAX=60 , will become ,
50=60
As we cannot assign one constant into
another it will generate an error.Error , LVALUE
means Left value , as Left side of the
assignment statement is always a variable.
Types of Literals :
·0 Numeric Literals
·1 Character Literals
·2 String Literals
·3 Boolean Literals
1. Numeric Literals
The Numberic literals are the fractional and
non-fractional constants. e.g. 5.6,-7.8,45,-67
etc...
The Numeric literals also associate with then the
base of the number system to which they belong.
By base we means
(a) Decimal Number System
(b) Octal Number System
(c) Hexadecimal Number System
All the Numeric literals by default belongs to the
decimal number system.
If we want to indicate that the numeric literal
belongs to the Octal number system , then we
just prefix the numeric literal with the leading 0.
e.g. 056 is regared as a octal value.
If we want to indicate that the numeric literal
belongs to the Hexadecimal number system ,
then we just prefix the numeric literal with the
0x or 0X.
e.g. 0x12f or 0X12f are the hexadecimal
numbers.
The Numeric literals also give the information
about the datatype to which they belongs.This is
done by adding a character at the end of the
numeric literal , which indicate its datatype.
==================================
==================================
===
Trailing Character
Datatype
==================================
==================================
==
(i) l or L
Long
e.g. 12l or 12L
(ii) f or F
Float
e.g 3.14f or 3.14F
(iii) d or D
Double
e.g. 3.14d or 3.14D
==================================
==================================
===
Note that ,
(a) All the non-fractional numeric literals
are by default considered as integer .
e.g 13 , -45
(b) All the fracional numeric literals are
by defualt considered as double.
e.g.
float radius=3.14f;
2. Character Literals
The character literals are the character constants
enclosed in the single quotes.
e.g. 'a','2' etc...
Escape Sequences :
'\t' , '\n' are considered as Escape
Squences.
As we know that in the single quotes only
the single character can be enclosed, but they are
two characters and still they are enclosed in
single quotes.This is because they are not treated
merely as two different character , they have a
special purpose reserved for them.
==================================
===========================
Escape Sequence
Meaning
==================================
===========================
(a) \n
Carriage Return and Line Feed
(b) \r
Carriage Return
(c) \a
Beep
(d) \t
Tab Space
(e) \\
Back Slash
(f) \"
Double Quotes
etc.....
==================================
===========================
Output : "Hello World"
System.out.println("\"HelloWorld\"");
3. String Literals
The String is a sequence or a group of characters
. The string literals is the value enclosed in
double quotes.
e.g.
"ajay"
"a"
4. Boolean Literals
The boolean literal is the logical constant
value , that is, true or false.
e.g.
boolean found;
found=false;
===================*==============
===============*================*==
========