Chapter 5 (Concept of Data Type)
Chapter 5 (Concept of Data Type)
Introduction
Compiler contains a phase called storage assignment phase. This phase allocates-memory
for different variabBes-used in aprogram. t also helns to structure memory location for
storing te data used in the program efficiently. Hence, the compiler must know the type
of data to ensure optimum utilization of memory space. This is the reason why data types
are required in Java programming. Thus, a data type allows the compiler to allot proper
space in the memory for data storage.
Tokens nsoPotin
Each individual component used in a ava statement is termed as Token. It is the
fundamenital unit of a program. It may be compared to the cell, in the human body. As
a cell is the fundamental and functional unit of human body, a token may also be referred
to as a cell of Java program.
Tokens
b + 9
Literals (Constants)
Literals are the constants used ina Java program When yos write aprogram in java, yot
may come across some auantities, which remain fixeether-donat change) throughaut the
QSCussion of the program. Such quantities are called Literals or Costauks
characteristic
for to
used used
in
a character by
representatieA
a
or this
which the in
circumstances
withtype
without
locatiaR, data Sclence-XI
separated
classitied 45,
the - are points. of etc. represents
numbers a set a note is a name In teters
Same
986, "", a of "COMPUTER", in This is nullof chapters. tho
are literals letters
character Boolean literalnon-availabilitymethod memory of be the Computer
Lihrale
Inhger Character
iherals 0.0072,The
decimals "3, is pair usedmustquotes. Basically, symbelie can_posseSs-NYCombina
Literals
Real These 556, - String upon
The All 'd', You null later can of
are negativeReal
orvalues Literals: literal a be dependingvariables
Literals124,Literals:
Literals: 24.6, literals.
as 'A', Literals:within Literals:cannot.withinSring
constants.
A null,
a
name, named words ISC
Understandin
literals with termed Literal: the the
exampl, example, Xcharacter example,andor
example,
character
numbersCharacter enclosed Boolean enclosed word
satisfied are classin identifier.
denotesIdentifiers as
an
methods
a
is
the one
Integer String literals. False'
Integer
Real Por be Null JdentifierS Variable changethen
yariable than
Java for can For Fof key
it
a
be wordsmore
canand A can 124
thatthesame in would
necessary
to declared
integer
type which
obtain
appropriate
need is logic). variables, 125
arrays)
with use need. bits. it. variable
you
youyou you value), to prograing Types
in
locationnamed 32 value and
in makes circumstances, The objects local Data
updated changes value why store
execution variables
(Absurd specific Dynamic constant. or of
below: to following
two
ways: Concept
64 of it reason managed variables
is After
Execution m
the
variable
i typeFurther, is
not
Anb4t the(i.e.,
value
shown
memory afterPot the
variable valuemaysuchassigning definite
in variables
namese for the the You use primitive
the m*m;
thememory knowvariable.
is is their
choice.
memory garbage Under
is
operation, of This a n program. by ackual
= If variable with
the which m value
initialized
Non-primitive
int the to a storage.bits. correct. the of
in The compiler
to of Initialization variable
its Incase
value, allocates it of
itself
any allottedto 32
is for
sortexecution
of
your in before values
valuedata
proper programming
logic
is place
<variable allocated contains be a values.
setsto its
duechange
variable
named
variable the
compilerlocationa integer
for its assigning
may take of (ie,the the
assignment
made thatinitial
location location it variable
may declaration with
<spaces Before
Execution 8 must for variable mind
can that memory memoryof Initializing
avariable
during variable
them
a is variable -m m
with
a is
length Static
Initialization default
p4 to change
type> Tgat
in
stored declare
discussed
it
before memory A direct your initialize
nu assigned
int Hence, yourvariable.
a although
result,
problem
Static its
8; thethe word declare a in with
usesduring
certain is variable of
<data a m 8 in in cxample, the Initialization keep
is Thus, int Here,
to program.
already arangement
store a process initialized
to
n), may initialize
youcreate must need
nitialized
Syntax: f
value
variable.
location. Need a
to declare int
Far When Ihis You users
A is like (say are
It the to
of
is length
Initialization
Static it
character. program,
the Dynamic
Initialization
table: c=\u0000 d=Math.sqrt(a); the logic
a=0; f-0.0; d=0.0;
p=false; with st3=st1+st2;
c=atb; f-p+k; increase
following a below: the
of of assigned
initializationexecution depicts chara
given to its
underscore.
characters
the is characters. easily specia
variable
are
in during
default examples it
shown float
p=2.2,k=4.22,f;
int
a=49;double
d; String
st1,
st2,st3; that or
invalid:
Declaration double
d; ie. a Declaration of andthe digitspa
float
f; char s; boolean
c; String
p; for situation, a,b,c;
nt
fsfee numbers between so
int
a; time digits
are used Few meaningful,a a
initialization withinc
run functions. Variableletters, be
character any
at this in
to start
used
Dynamic
Initialization
initializod
Under havecase be variable
not
not
unicode logical be should shou
default a maylower should
Data
Type Initialization.
Data
Type Naming can
IntegerFloat DoubleCharacter
String Boolean gets IntegerFloat Double String name
namesa namenam
underscore
bits or contain
variable
name.
of variable
arithmetical for
examples 16 variable varia
a Dyngmic of Variable
Reasons
Variable
is may
"u0000' a Rules The
When A It "A
Few any " "
as
Data Types
Primitive Types
The data types, hich are independent of aAy other type,-are known as Primitie Data
Tirps These types are also called Basic Data Tynes
For exampler
hyte, int, long, loat double, ete
because they are defined by
Primitive data types are pre-defined or built-in-data types type 'int' which will follow
a variable of
the developers of Java system. You can declare
the in-built characteristics mentioned for this type.
For example, int p; Hence, the variable
means that the variable p follows the characteristics of "int' type.
It
pwill contain an integer value.
are further classified into Numeric and Non-numeric data types.
Primitive types
Non-primnitive Types
Non-primitive data types arealse calledDerined Data Tiypes. They are directly or indirectly
dependent on primitive data types. The method of declaring,anon-primitive data type is
addresses
different from primitive ype data. They store data with reference to memory
or locations. So, they are alse-called Refensee Det te.
For example, classes, arrays, interfaces, ete.
in the later chapters.
The non-primitive data types will be discussed in detail
Arithmetic Expression
constants and arithmetic operators, is termed
An expression, which contains variables,
as an Arithmetic Expression.
For example, x = a+b*4;
P =p*(3+tp);
ensto
Pure Arithmetic Expressiontes items (variables or constants)
contains same type of data
An arithmetic expression which,
is called Pure Arithmetic Expression.
For example, int a, b, c;
C= a b*8 - a;
a = (b+5)"(a+4); elements
the expressions in the example shown above contain all the
YOu must note that
type. Hence, they are pure expressions.
(Variables a, b, c and constant 8) of integer
Mixed Expression elements of different data types, is
known as Mixed
An expression, which includes
Expression or Impure Expression.
For example, int a; float b; double d;
double X = a + b * d; variables as int,
expression a+b®d contains different types of
the
t the given example,expression
Such is called Mixed expression.
oat and double.
Type Conversion data type.
result can be obtained in any one form of its Such
mxedmode expression, the operation of various data types into a single type.
the conversion:
Hence, it is needed to convert Conversion. There are two types of data type
Yersion is termed as Type
"Implicit type Explicit type TVDes 131
Implicit Type Conversion dmsi1,ota
Ina mixed cxpression, the tyne conversion takes pBace automatieally into the higher most
type of data available in the expressier without interventien of the user. This system of
type conversion is known as implicit Type Conversion or Coerciot.
This
system
values
prBmilvedata
contaln
Wrayy the facltyto osoDN:
pvkde following
Wrappr clases dass forthe
We nevd a Wayer valuesinojects vce-versa.
the primitive and
(0 tostore stringintoprimitiveypes computer,0 Isrecelveda
comert saluetothearlthmatleal operatlon,
) to entera numerle
usedin
Nheneveryou data cah hot be type. Thlsconverslon Is
posslble
by Hence, You
string
(string). The datainto primitlve
class. converslon
usiny ahu
onvert the string
paselat 0) ofwayerperformlngsuchtypeof values that
(Integer. In an
wrayer classallows wrapsthe primltlve data obBet,
Thus, a hbrary Java.long). It class, Some of
theJavaclas contalned In specllc wrapper he
The primitir data
items
the data
are
types are listed
below: wy
classes alongwith Whapperclassen
Dala Types
char
Character
byte
Byte
short
Short
Int
Integer
long
Long
float
Float
double
Double
Exercises
L 0in the tlanks with an appropriate word/words:
in ngloGustos
1. Acharacer literalis enclosed
2Ast of haracersis asgned.to m g i
5. The ASCl codes of lowercase letters ranges from 122
4. Aliteral nepresenting True or False comes under .EKlandata type.
5. 11Af/20d is a type constant.
6. Class is an esample of . . primitive data type.
7. Aset of charäcters used together under double quotes is called
6. Aword used in ahigh level language that carries a special meaning fo
compiler is called
9.A daracter literal is asignedto a Cemtat
10. In hierardhial arrangement, the higher most data tvpe is CAAo
11. Write Short AnsWers:
1. What do you understand by data type? 5