0% found this document useful (0 votes)
27 views10 pages

Chapter 5 (Concept of Data Type)

The document discusses different types of tokens used in Java programming including literals, identifiers, punctuators, separators, and operators. It explains that literals are constants used in a program that do not change, such as numeric or string values. The document also describes the different types of literals including integer, floating-point, character, string, and boolean literals.

Uploaded by

harritjevi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views10 pages

Chapter 5 (Concept of Data Type)

The document discusses different types of tokens used in Java programming including literals, identifiers, punctuators, separators, and operators. It explains that literals are constants used in a program that do not change, such as numeric or string values. The document also describes the different types of literals including integer, floating-point, character, string, and boolean literals.

Uploaded by

harritjevi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

AAAAA

CONCEPT OF DATA TYPES


In this chapter
Introduction, Tokens, Types of tokens: Literals, Identifers, Punctuators, Seperators,
Operators, Assignment, Data Types: Primitive (short, int, long, float, double, boolean,
char), Non-Primitive; Type conversion: Implicit and Explicit; Wrapper classes.

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

Identifier Literal Punctuator


Assignment Identifier Operator
Types of Tokens whm6,Poxtdo
There are various types of tokens whict are used în Java program. They are:
"Literals. "Jdentifiers " Punctuators
" Separators "Operators "Assignments

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

Concept of Data Types 123


posit
are quotes.
are
pointlhaving
Ca
They as T de
special
nature, of known represent con diffy ThevalnutE
) can
contalns
decimal
constants) single
group false
logical represent
In abo prosrata
constants. constants We
ways:
followlog
Literale
Slring Literals
Boolean
Literals
Null in
digits, is or
class. structure
discusdiscuss
().
within
A quotes,etc. They true
a vaable
without alphanumeric
case), characters.1947", that is a willto a underscore
peint floating-point boolean
(i.e. It of in
value.
encloseddouble literals. object
purpose. logical going operations
lower ensure We a statement.
constants In-ease,
represented
(fxed Aug an String. a name. are a of
alphanumeric makes contains
etc. are or(upper character closing special
"15th to initialize
special which we help
space.
numbers a variable and
the called 1.0E-03,
program
boolean chapter, a
into 1 Pot)
Am8, are
whichwvhole etc. also -3.56002,
which
alphabet single and
constants,literals. of
2020",
are
opening constants
"Year Javathat

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 Non-primitive Types

Numeric Type Non-numeric Type Classes Arrays Interface

Integers Floating Characters Booleàn

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

Numeric Data Types values. It includes Integer and Floating


number oriented
Numeric data type deals with
type data.

Integer Type number. The number may be a positive


integer type contains a whole of declarations
A Variable declared without decimal point. There are four types
but
Or a negative number
under this category. They are:
operations
" byte : Used for bit wise integers
short: Used for asmall range of
integers having size bigger than short
int : Used for
" long : Used for large integers data type, according to the need of the
Programmer has to select an appropriate
following table:
Program as shown in the Bit Size
Format
Data Type byte a;a-5;
Data 8 bits
byte short b; b=12;
Byte 16 bits
short int c; c=214;
Short 32 bits
int long d; d-45687;
Integer 64 bits
long
Long Integer
ata Tvpes 127
(d) String constant Mouse, assigned to variable s must be enclosed within double
quotes.
(e) String the variable x should not be assigned with a boolean constant.
(0 Variable p is not assigned a boolean literal. ie,, "true" is treated as a String
Constant.
(g) Variable p must contain asingle character within single quotes.
Example 2: Assign the following literals to the variables declared under various types:
"Java Language", 345, 'G,45.65, false
(a) int x; (b) char c; (c) String s; (d) boolean b; (e) double d;
Ans. (a) x=345; (b) c= 'G'; (c) s= "Java Language'";
(d) b=false; (e) d=45.65;

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.

Hierarchy of Data types


The type hierarchy shown indicates the increasing order of data types. If two data of
different types are operated then result is automatically gets converted to their higher type.
byte
char
short
int
Hierarchy
type.
Conversion
132 Explicit
Another
conversion Type
derstanding way
depends
of
type
ISC upon
Computer conversion
usorchoica
Sclenca
1s
explicit
o-Hser's
type
dermand data
Anad,hot convefsion.

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

all lowercase letters whereas, its wrapper classi


Nota: Adata type is written by using
with initial letter in uppercase.

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

134 Understanding 180 tomputer Scienca-0


What do you mean by a variable? Explain with an example.
4. Define constant with an example. 123
5. What do you understar by primitive nd non primitive data types? Give two examples
in Java Ph
6.What do yod understand'by Token? Name different types of tokens used lin D3
7.What are the points to be taken care while assigning variables in Java programming!
8. Differentiate between:
(a) Integer and Floating constant Pa121 ) Character and String constant
String and Boolean constant a (4) Tpken and ldentifier
(e) Separator and Punctuator
9. Write down the Data type of the following:
(a) Integer t b) Long Integer Uomg
(c) Afractional number. loot (d) A special character
10, What do you understand by Boolean type data ? Explain with an example.
11. Define of the following terms with an example:
Pý124
(a) Implicit type conversion
() LiteralsJns Pg132 (b) Explicit type conversion
(a) ldentiersa
12. What is the significance of declaring a variable in
13. What do you mean by initialization of a variable?
14. In what way isstete de ,from dynamic declration?
15. What do you mesroy non-prtnttive dta typer
16 Anon-primitive dáta type is also referred to as reference type. Why?
17. Explain the term type casting?
18. Perfom the following:
type.
a) Assign the value of pie (3.142) to a variable with the requisite data
b) Assign the value "TEST" to a variable with the requisite, data
EST?
=34%
redict the refurn data type in the follbnD=
L int p; ouble q;
m
m =m
2. float m; 3464 doue
m/3(Math,pow(4,3); =
conversions are performed?
hat are the resulting data types, if the following implicit
int i; float f; double d; char c; byte b;
(a) i +c/b znt
Ole +f/beot (e) i+fc+ b/d- doule
are the resiting data tvpes of the following explicit conversions?
nt i; float f; char c;byte b;
Short(b)s;(int)raV (char))+f- bdChat
(a) (float) (c)
(e) (char) d+
(4) (double)
(G

Conceot of Data Types 135

You might also like