Computer values and data type
Computer values and data type
ata Types
rHE CHARACTER SET
.,ike any other human ·
~ d ( readable I
or s tokens) and Senten anguage, program .
ces (statements) ming _languages also use Ch
. · aracters
.The character set . ,
characters that are ~a:ds~t of alphabets, letters and .
m Java langu age. some special
rhe smallest unit of Java Ian . .
rhese character set are defin::ge~s _the set of characters needed to ·t
n J p . y n1code character set - wn e Java tokens.
ava rogratnming, each d .
3asic Lexical Element. Each : ;ery character is considered as a single 1 .
>f different Java progr~""' ...... ~n lva ogram is set of statements and each stat exeme: i.e.,
......... u.1..u g exemes. ement 1s set
However, we all know that a computer c~ only understand binary language that is just
sequence of Os and ls. So how is it going to understand these alphabets, digits and
symbols? Here comes the concept of encoding. . _
Character Encoding
A character encoding tells the computer how to interpret raw zeroes and ones into real
characters by pairing numbers with characters. It's just like saying let's give a number
to capital 'A' that is 65 and 'B' 66 and so on upto Z. There can be similar corresponding
numbers to different l~tters, digits and symbols. Now these numbers can easily be converted
to binary number.
Words and sentences in text are created from characters and these characters are grouped
into a character set. There are many different types of character encodings, but the ones
We deal most frequently with are ASCII, 8_ - bit encodings, and Unicode-based encodings.
cti ve of the
Sta nd ard pro vid es a un iqu e nu mb er for eve ry cha rac ter , irre spe
Th e Un ico de mo der n software
ice , app lic ati on or lan gua ge. It ha s bee n ado pte d by all
pla tfo rm , dev
tra nsp ort ed thr ou gh ma ny dif fer ent pla tfo rm s, dev ice s and
pro vid ers all ow ing to be
'
app lic ati on s wi tho ut cor rup tio n. major
fou nd ati on for the rep res ent ati on ofl ang uag es an d sym bo ls in all
Un ico de for ms the , lap top s, an d sm art ph on es plu s
the Int ern et
tem s, sea rch eng ine s, bro ws ers .
op era tin g sys CS S, JSO N, etc .).
Ls, HT ML , XM L,
an d Wo rld Wi de We b (UR
Th e Un ico de Character Se t . .
/
al'l
bit (2 byt e) ch ara cte r set wh ich ca n rep res en t alm ost all hum
Th e Un ico de is a 16- rld . Th is ch ara cte r set is use d
in Javs
itin g sys tem s aro un d the wo
alp hab ets _and wr .
pr og r~ g lan gua ge. . "'"'d
• ·ta1 cu•
rti f · · cro sof t, HP , D1g i
the co Ap ple , Mi
In the yea r 199
IBM cre ate d the
3
U. nso . um o co mp an ies su ch
set > usi
as
ng the ISO -10 64 6 sta nd ard . Th
eir aim was to
dar de ch ara cte r
pro du ce a sin gle s:o
d.
> All the cha rac ter s usedin th ~ 16-bit Un ieo de ch ara cte t eet occupy the sam e space•
V a lu es a n d D a ta
T yp es
I
r se t sh a r s It . . te r se t .
► T h is c h~ a ra c te e s fi rs t 2 5 6 al u e s With th e ISO-Latin ch ar ac and .
v .J
hi h
n s th e b a si s o f th e e . m g sy st em s su c h a s Windows 3
w c on e r o p er at
a rh
W in d o w s 9 5 . f
n to th e c h a ra c t ch t de ch ar ac te r se t
► In a d d it io e rs O th e A SC II se t,
ar ac e r rs . the U ni co
fin io n a l 6 5 2 8 0 d . . co d ed ch ar ac te
de e s a n a d d it P a rt ic u, la r U Is. ti n ct ,
► You c a n re fe r
to a rb · h
n ic o d. e c h a ra c te Y u s m g t elees ca p e se q u en ce i.e.
:
• o w ed b y a fo u r d ig it h x ad ec1mal n u m b er ; for examp
\ u fo ll e
.
\u00BD v2 The fraction 1/2
\u00AE mbol
® The copyright sy
\u0022 " The double quote
\u00BC V4 The fraction 1/4
✓ bol
\u221A Square Root sym
\u0041 A Capital A
ENC E .
ESCAPE S E Q U
n
g n al a n al te rn at iv e in te rp re ta ti o
e c h a r a c te r s a re u se d to si so called es ca p e se q u en ce s o r
E sc a p h a ra c te rs . T h ey
ar e al
a se ri e s
o f p e c o d e s.o f c
e sc a ial
es ca p e se q u en ce a n d h a s spec
is a n
r p re c e d e d b y a b a c k sl a sh (\ )
h a ra c te c o m p il e r.
In Ja v a , atoc th
m e a n in g e Java
em en t, th e co m piler in te rp re ts it
n t st at
u e n c e is e n c o u n te re d in .a p ri
W h en a n eto sc a p e se q g iv en b el o w:
ac co rd in g th e ta b le
u e n c e s a v a il a b le in J a v a are:
E sc a p e se q in t. ,er
rt s a ta b in th e te x t a t th is p o
\t In se in t. :7
a c k sp a c e in th e te x t a t th is p o
\b In se rt s a b p o
J
in t.
✓
x t a t th is
n e in th e te
In se rt s a n ew li o in_t.
\n
ia g e re tu rn in th e te x t a t th is p
\r In se rt s a c a rr t.
rm fe ed in th e te x t a t th is p o in
\f In se rt s a fo th e te x t a t th is.r-p- oin. t.
h a ra c te r in
. gle q u o te c
\' In se rt s a sm
c te r in th e te x t a t tl µ s p o m t.
. b le q u o te c h a ra
\,, In se rt s a d o u in th e te x t a t th is p o in t.
c te r
k sl a sh c h a ra
In se rt s a b a c ce,
\\ e m u st u se th e es ca p e sequen
s w
t q u o te s w1't h in---- q u o te
we want to p u
F o r e x a m p le , if
o r q u o te s:
\ " , o n th e in te ri llo!\" to me .");
("She said \"He
System.ou
O u tp u
t.
t: S
p
h
n.
e
n tln
sa id
~ ~-----~ ~~
"Hello!" to m e .
~==
Toted Computer Applications - IX
TOKENS
In eve?' h~~ language (like English), there are words and symbols which have a predefineq
mearung. Similarly, all programming languages have a set of predefined words and symbol
In Java programming language we call them tokens. s.
Ja.\>'l. toke.na are the smallest units of a Java program that are used
by the Java compiler for constructing expressions and statements.
Java program is just collection of different types of tokens, comments, and white spaces.
There are five types of tokens in Java:
► Reserved Keywords
► Identifiers
► Literals
► Operators
► Separators
Reserved Keywords
An Ex am ple
public class Test
{
public static void main(String □ args)
{
int a= 20;
}
}
tifie rs nam ely :
In the abo ve Jav a cod e, we hav e 5 iden
► Tes t: cla ss nam e.
► ma in: me tho d nam e.
► arg s: var iab le nam e.
► a: var iab le nam e.
id",
wor ds like "pu blic ", "cla ss", "sta tic" , "vo
And all oth er pre def ine d wor ds are key
"Str ing" .
Nam ing Ru les for Jav a Ide nti fier s foll owe d,
d Jav a iden tifie rs. The se rule s mu st be
The re are cer tain rule s for def inin g a vali gua ges like
The se rule s are also vali d for oth er lan
othe rwi se we get com pile -tim e erro r.
C,C++. l... 'L
> J:.t r\CWe S('O Ce cha rac ters ([A-
ters for iden tifie rs are all alp han um eric
COY' l'nO"l:
► The onl y allo wed cha rac
der sco re). .
Z],[a-~J,[0-9)), '$'( dol lar sign ) and '_' (un er.
iden tifie r as it con tain s @' - spe cial cha ract
For exa mpl e "gee k@ " is not a vali d Jav a a
[0-91) . For exa mpl e, "12 3ge eks " is a not
► Ide ntif iers sho u ld n ot sta rt wit h dig itsf
vali d Jav a iden tifie r.
► Jav a ide ntif iers are cas e-s ens
itiv e.
le to use an
► The re is no lim it on .the
len gth of the iden tifie r but it is adv isab
opt imu m len gth of 4 - 15 lett ers only . is an
Wo rds can 't be use d as an iden tifie r. For exa mp le "int whi le= 20;"
► Res erv ed
d wor d.
inv alid stat em ent as whi le is a rese rve
Literals or Constants
A literal or constant, in Java, .refers to a fixed value that does not change during th
-execution of the program. Java supports several types of constants.
1. ·integer constant
Integer constant refers to sequence of numbers without decimal point. There are thn
types of integer constants - decimal, octal and hexadecimal.
- -
----=::;;;;;;,:;;:;;:= =:::;;;;;---;:/
Value s and Data Types
F with leadin g (zero x) Ox or (zero X) OX.
Exam ple:
OX7A4 OXa 0xA4
)
C mant issa e/E expo nent
-----=-----------
intege r .
The mant issa is eithe r a real numb er expre ssed in decim al notati on or an 2
I0 •
.The expon ent is an integ er with an option al + or - sign. Thus e2 mean s
Exam ple:
Valid R~al Cons tants in expon ent form 1
12e-2 -l.2E -4 10.10 E+5 10.E+ 5f .25e7
0.97e 4
A single chara cter enclo sed withi n single quote s is called chara cter
const ant.
Example:
"Hai" "Java program ming" "1998"
Operat ors
Java operato rs are the mathem atical symbols used to perform mathem atical and logic
operatio ns among two or more variable s or literals called operand s. There are many t:yJx
of operato rs availabl e in Java such as: Arithme tic Operafo rs, Relation al Operato rs, Logic
Operato rs, Bitwise Operato rs, Assignm ent Operato rs etc. We shall study them in deta
later in this book.
Separa tors
Separat ors help define the structur e of a program. The separato rs used in Java are as follow
-- Colon
Comm a
After loop varia bles i.e., for nami ng loops.
To separ ate argum ents or variab les in decla ration s.
_c:
- '
Period To separ ate packa ge name s from sub-p ackag es and type
name s, and to separ ate a field or meth od from a reference
variab le. - ' .. .·
-
vARJABLES
syntax:
__
(..__ty _al_u_e]_;_ _ _ _ _ _ _ _ _ _ _ ___;;_:__ _ _)
pe_id_e_n_tifi_·_e_r_[=v
of a class or interf ace,
Where, the type is one of Java' s primi tive data types, or the name
the identifier is the name of the variab le.
type using assig nmen t
The variable can be initia lised by assig ning a value of compatible
comm a opera tor.
operator. More than one varia ble of a type can be decla red using
lization.
Look at sorpe exam ples given below of varia ble decla ration and initia
e.g. int age = 20; II declare int variable age and assigning 20 as its initial value
int x,y,z; II declares three ints x ,y and z in a single statement. ·
char section = '/\ ; II declares and initialize a char variable and stores A as its value.
float price = 398.50f; II declare and initialize a decimal value in float variable
String name = "SGC"; II declare and in
B c.
A
I 23 I 44
111 112
110
- • > .
-- -- ----.
_ __ _ _ __ _ __:.:: .::.:.eo,n
int A = 2;
Tota l
a.~t icat
iottS - JX,.- - - - -
put err ~~~ r~- ----
.:..-- --- - ,, hich is of type "in\\ -
a vari able n a111ed the
"A VI
dat a type int late r.
hav
The stat eme nt above mea ns we " " in it.e crea ted will disc uss the
We
i.e., inte ger and it hold s the value 2
·
DAT A TYP ES rnpi ler hoW rnu ch Space this ...1:"
variab1~
t tell the Jav a co d"ff eren t dat a typ es nee d uu1erent
While dec larin g a vari ab1e we hav e o
data type s are us ed as 1
. urpo se
will occ upy . For t h is P
amo unt of memory space. . 1 can
ible valu es a van ab e
- -A - - - - . defined as the~:e:t~o:f~p~o_s_s_ __ _
data type is _ _ _ _ _ _~ -
hold.
es of data type s.
In Jav a ther e are basi·cally two typ
► Primitive data type
► Non-primitive data type
Data Type
I
*
Primitive Non-Primitive
I
i .i String
Bool
ean Numeric
I Arra y
i i
Integral Complex data structure
Character
i I
char i
Integer
i
Floating point
true false
1. Integers
Java defines four integer types: byte, short, int, and long. All these are signed positive as
well as negative values. Java does not support unsigned positive only integers. The width
and ranges of integer types are given in the table below.
3. c haractel'.S
d~tatype used to store charac ters is char. Java uses Unicode to repres ent
111 Java, the repres ent all
characters. Umcode d7fines a complete intern ationa l charac ter set that can
The range of
the characters found in all huma n langua ges. Java char is a 16-bit type.
ter s known
char is Oto 65,535. There are no negative chars. The standa rd set of charac
as ASCII, ranges from 1 to 127. For example, look at the code given below.
char ch1, ch2;
ch1 = 89; //ASCII and Unicode for Y
ch2 = 'Z'; //Character Z stored in variable
ch1 = ch1 +1; //ch1 stores 90, ASCII and Unicode for Z
4 , Boolean
two possib le
J ava has a simple type, called boolean, for logical values . It can have only
requir ed by
values, true or false. It is the type return ed by all relational opera tors . or
keyword. The
conditional expres sions. The boolea n type is declared using the boolea n
program code given below demo nstrat es the boolean type.
boolean b1, b2;
b1 = true;
b2 = false;
} . _ 90 and a-z(97-122)
Note: Every character has an mteger value. A-Z (65 )
The scien tific notati on uses a stand ard-n otatio n: ·floatin g-poin t
numb er plus a suffix that ·specif ies a power ·o f 10 by which the
numb er is .to be multip lied. The expon ent is indica ted by an 'E' or
'e' followed by a positi ve or negati ve decim al numb er. E.g. 6.022 E23
and 31415 9E-05 .
(d) Boolean const ants can take only two value s ~rue and false. These
value s .do not
conve rt to any nume rical repres enta~ on. They can only be assigr led to
a vanab le of
type boole an.
(e) String const ants are a seque nce of chara cters enclos ed in doubl e quote
s. E.g. "Hello
World", "Doub le \n lines" . The escap e seque nces given in the table work
the same way
inside string litera ls. It is impor tant to note that in Java string must begin
and end on
the same line.
The following progr am show s the use of escap e seque nce chara cters in
String literal .
class P7
{
static void main()
{
String a; . . '"
a = "Java is programming language \n developed by \t \' James Gosling\
;
System.out.println(a);
}
}
Output : Java is a progr ammi ng langu age
II developed by 'James Gos 1·mg '
Type Conv ersio n
1
•
.
·
The proce ss of conve rting one pnmi·tive type to anoth er is called
.
type conve rsion.
The two types of conve rsion suppo rted in Java are:
.
1. Implic it conve rsion; and
2. Explic it conve rsion.
Ex am ple :
int a= 5;
double b = a;
System.out.println(b );
He re va lue in a is of int eg er typ
e an d ge ts rea dil y co nv ert ed
typ e is lar ge r tha n so urc e typ e to do ub le as the de sti na ti01
In ca se of an ex pre ssi on , the co
nv ers ion follows the fol low ing
1. Re su lta nt typ e de pe nd s on rul es:
the da ta typ e of the lar ge st op
2. If an ex pre ssi on co nta ins int era nd .
eg er an d flo at va lue the res ult
will be flo at typ e. an t typ e of expressi01
3. If ~ ex pre ssi on co nta ins ch
ara cte r an d int eg er the res ult
an t wi ll be of int eg er type
Ex am ple 1:
int i; 'i/ r res ult an t typ e wi ll be flo
at wh ich when
su btr ac ted wi th do ub le da ta
float f; typ e wi ll res ult
in do ub le da ta typ e
do ub led ;
do ub le an s = i / f - d;
System.out. println( ans);
Ex am ple 2:
long a = 1OOL; 'a + r res ult an t typ e wi ll be flo
at.
float f = 5.5f; Note: he re tho ug h lon g ha s 64
bit siz e an d float
flo at an s = a + f; ha s 32 bit siz e bu t the res ult
is flo at be cau se
System.out.println(ans); it is mo re pre cis e.
Ex am ple 3:
int a= 3; He re wh en we ad d a wi th b, as
ev ery ch ara cte r I
ch ar b = 'C'; va lue ha s nu me ric rep res en tat
ion , C ha s ASCII I
int an s = a + b;
va lue 67 so res ult an t va lue wi
ll be 70 .
System.out.prif!tln(ans);
SUMMARY
~ The character set is a set of alphabets, letters and some special characters that are
valid in Java language.
~ A character encoding tells the computer how to interpret raw zeroes and ones into
real characters.
~ The Unicode Standard provides a unique number for every character, irrespective of
platform, device , application or language.
~ Escape characters (also called escape sequences or escape codes) in general are used
t o signal an alternative interpretation of a series of characters .