You are on page 1of 14

CONCEPT OF

DATA TYPES IIN JAVVA

Introduction
Complier contains a phase called storage assignment phase. This phase allocates memory
for different variables used in your program. It also creates the structure to store the data
efficiently in the location. Hence, the complier must know the type of data you are likely
to supply for storage to ensure optimum utilization of memory space. This is the reason
why data types are required in Java programming.

Tokens
Each individual character used in a Java
program is termed as token. It is the fundamental
unit of a Java program. It
may be related with the cell in the human body. As cell is the
fundamental and functional unit of human body, a token
may also be referred as a cell1
of Java program.

TOKENSS

C
5

D D
E S E L U
N N E
G R

M
E U
R
E
N
O A
E R L
T R
R

30
Types of Tokens
various types of tokens available in Java are:
The
Literals Identifiers
Assignments Punctuators
Seperators Operators
Literals (Constants)
Iiterals are the constants in Java program. When you write a program in Java, you may
come across some uantities, which remain fixed (i.e. do not change) throughout the
discussion of the program. Such quantities are termed as Literals or Constants.
Java uses the literals cassified in the following ways:

String Literais Real Literals

Integer Literals LITERALS Character Literals

Nuil Literals Boolean Literals

are called
Integer Literals : The numbers which are represented without decimal point
numbers having positive or negative values.
Integer Literals. These are the whole
-391 etc.
e.g. 14, 345, 8, 6392, -18,
constants. They represent numbers
Real Literals These are also called floating-point
with decimals points.
1.0E-03 etc.
e.g. 24.6, 0.0072, -3.652,
which alphanumeric in nature, are called
Character Literals: The constants, are

lower case, digits, special characters can be


character literals. All alphabets upper or

termed as character literals.


**'
e.g. 'A, d', 3,
etc.

A character literal represents a single character enclosed within single quotes.


String Literals String is a set of alphanumeric characters. A group of characters
enclosed within a pair of opening and closing double quotes is known as string literal,
"10%% per annum" etc.
e.g.. "VIJAY KUMAR PANDEY", "Year 2006",
Boolean Literals: Boolean constants are special literals. They represent true orfalse
and can be applied in a java program to ensure that a given logical condition is
satisfied or not. You must note that Boolean constants (1.e. true or false) are never
enclosed within quotes. This characteristic makes Boolean constants different than
string constants.
Null Literal This is another special purpose literal. it is represented as "0". It is
applied in a java program as a string terminator to mark end of the string

Punctuators
Punctuators are the punctuation signs used as special characters in Java. Some of the
punctuators are: ? (question mark), ; (semi colon), (dot) etC.
Dot() is used to represent the scope ofa function ie. a function belonging to an object
or a class.

e.g.. System.out.println), java.io.* etc.


Semi colon (;) is used in a java program as a statement terminator. It indicates the end
of a statement. Any line continued after semi colon is treated as the next line.
e.g., int a =5; System.out.println(a); are treated two separate statements in java
Separators
They the
are special characters in Java, which are used to separate the variables or the
characters.
Braces (,)
e.g.: , Curly braces {}, Square braces [.] etc.
Comma () in a java program is applied to separate multiple variables under same
declaration.
e.g.: int a,b.c;
Braces () are used to enclose any arithmetical
or relational expressions.
Curly brackets {}are applied to enclose a
group of statements
statement. under a
compound
Square brackets [ ] are used to enclose subscript or cell number of a dimensional array.
Escape Sequences: There are
certain non-graphic characters, Escape Sequences
Non-Graphic Character
which cannot be typed directly
Horizontal Tab
from the keyboard. However,
Vertical tab
they are frequently used in Java
programming. These characters Backslash
are represented by using escape Single Quote
sequences, which begin with a Double Quote
b
backslash () and are followed
f
Backspace
or more characters. Formfeed
by one
is given for your 0
A table Null
reference.
Carriage retum
Operators

basically the symbols or tokens, which perform arithmetical or logical


Operators are ba
erations to yield results. Basically, there are three types of operators used in Java:
ope
*
a.Arithmetical Operators: +, -, , etc.

Relational Operators: <,>,= =, 5, << etc.


b.
I ! etc
c. Logical Operators: &&, ,

In Java
Data Types
deal with the various types of data, hence it becomes
we have to
In Java programming taken
to select an appropriate data type according to the data
necessary for a programmer
The data and its types are given below:
in a program.
DATA TYPES

Non-Primitive
Primitive
Types Types

Non-Numeric Classes Arrays Interface


Numeric
Type
Type

Integers Floating
Numbers Characters Boolean String

Primitive Types known as Primitive data types.


other type, are
independent of any
The data types, which are
float, double etc.
called Basic Data Types/e.g.
byte, int, long,
are also
These types or built-in data types because the system
are pre-defined
data types
Primitive variable of type int, which will
them. You can declare a
developers of Java have defined
in this type.
characteristics mentioned
follow the
e.g. int X; contains
characteristics of int type. Hence variable x

means variable
x follows the
It
only integer value.

Nog-Primitive Types
are directly or indirectly dependent
basically defined as derived data types. They
They are
The method of storing non-primitive type
data is different from
on primitive
data types.
reference to the addresses or locations.
data. 'They store the data vaBues with
primitive type
So, they are also called "Reference Data' iype
eg ckasses, arrays, interfaces etc.
data types in detail.
Let us discuss primitive
Integer ype
A variable declared
integer type contains a whole number. The number may be a positive
or a
negative number but without decimal point. There are four
types of declarations
under this heading:
byte: Applied for bit wise operations
short
Applied for a small range of integers
int :Applied for integers which are more than short integers
long Applied for large integers
A programmer has to
select an appropriate data
program as shown below: type according to the need of the

Data
Data Type Bit Size Format
Byte byte 8 bits byte a; a=5;
Short short 16 bits short b; b=12;
Integer int 32 bits int c; c=214;
Long Integer long 64 bits long d; d=45687;

Floating type
If you declare a
variable to be floating
type then
i.e. A number with
decimal point. You can use twoyou
can store a
fractional number in it.
data values. different types to
represent fractional
float It represents a fractional number with
small range.
double: It represents a fractional
number with wide
A
programmer decides the types according to the range values.
shown below: need in his
program. Refer the table
Data Data types Bit size
Small range of Format
float 32 bits
decimal values float m; m=34.45
Wide range of double 64 bits
decimal values double n;
n=0.00056435869;
Character type
A character type variable contains a
single
with computer. These characters are termed character. There are 256 characters
is assigned as ASCIl
Characters. available
a
specific numeric value called ASCII code. Each ASCII
character
characters range from 0 to 255. The ASCII
codes of the
ASCII codes of the characters are as follows:
A -Z 65 90
a z 97 122 (Respectively)
0 9 : 48 57
Other codes are used for special characters.
The character types in Java are as follows:
Non numeric Character type Bit size Format
Single character char 16 bits (2 char p: p= 'A':
bytes)
(An alphabet or a
char x; X= **°;
special character).
More than one String More than 16 bits String a;
character/ a word/ a= "Vidyalaya":
a sentence.

Boolean Type
It is special type in which a variable contains a constant as True or False. These are non-
figurative constants. You can use Boolean type variable to set True or False in order to
ensure whether a logical condition is satisfied or not.
e.g. boolean flag = false; or boolean flag = true:

Special Notes 1. A character is enclosed in single quotes ( ( ' )


2. Strings are enclosed in double quotes (" .
3. A Boolean type assumes one of the values true or false without
quotes.

Identifiers (Variables)
Identifiers are also called variables in Java programming. A variable is a named memory
location, which contains a value. The value of a variable can change depending upon the
circumstances and problems in a program. A variable can possess any combination of
letters without space. We can declare more than one variables of the same type in a
statement.

Syntax: <data type> <space> <variable name>

int m
float p.q.
A value assigned to variable sets itself in the memóry location named with the variable.
If certain change is brought about due to an operation, it is maintained in the same
location. Thus, a variable can change its value, which is shown as:

Before Execution After Execution

int m = 5; - m 5 int m m"m; 25

Data 5 is stored in location named m The value of the variable m changes


in the memory after the execution

Need to declare a variable


I is already discussed that compiler allocates memory for the variables you use in the
program. Hence, it is must for the compiler to know the type of value you would like to
store in the memory location allotted to a variable so that necessary arrangement in a
memory can be done for its proper storage. This is the reason you need the
variable before assigning a value to it.
to declaroea
e.g. word length of integer data is 32 bits. If a variable is declared
integer ttype
e
(say int n), the memory location allocated for variable n 1s managed to store
32 hitss.
Initializing a variable
When you declare variable it contains sort of garbage value (Absurd
a
value),
create problem during execution of your program. You may not obtain which mav
although your program logic is correct. Under such circumstances proper result
a variable. A you need to initialize
variable may be initialized by
assigning specific value to it.
Initialization of a variable may take
place the following two ways:
in

Initialization

Static
Dynamic
Static Initialization
This process uses direct
assignment of a variable with defined constant. The
itialized during its declaration
(i.e., before its actual use in the variable is

e.g. programming logic).


Data Type Declaration Static Initialization
Integer int a
a=0;
Float float f:
Double double d:
f-0.0
d=0.0;
Characteer char c C=
String String s S= *»,

Boolean boolean p
p=false;
Dynamic Initialization
When a variable gets initialized at run
termed time, i.e., during
as
Dynamic Initialization. Under this execution of logic 1s
outcome of any arithmetical situation, a variable is program with
or
logical functions. assigned the

e.g
Data Type
Integer
Declaration
int a,b,c; Dynamie Initialization
Float
c=a+b;
float p-2.2.k=4.22,f;
Double int a=49:double d; f=p+k;
String String stl, st2,st3 d=Math.sqrt(a);
st3=sti+st2:
Rules of Naming a Variable
A variable may have any numbers of characters.
It may contain alphabets, digits and underscore.
.The underscore can be applied in between the characters to increase the length of a
variable name.

Variable names should be meaningful which easily depicts the logic.

Reasons for a variable to be invalid


A variable name should not start with a digit or special character.
A variable name should not include a space in between its characters.
A digit should not be applied in between the characters of a variable name.
A variable name should not be a reserved/key word.

Assignments
Assigning means to store constants in variables using a token = symbol. However, the
symbol '=" may refer as assignment operator. The data type of variable depends upon the
constant. You must ensure that a constant, which you are going to store, must have same
type as the variable is declared.
Syntax: variable = constant;
e.g., int m = l5; :15 is stored in the variable m which is integer type
float n = 45.24; 45.24 is stored in the variable n which is float type
char chr = k'; : The data type char is used to store a character enclosed within
single quotes ( ) in a variable chr.
String str = "Computer Applications"; : The data type String is used to store a
word/a sentence/a paragraph in the
variable name, enclosed within double
quotes (" *").

Note: 1. Numeric variables are used to assign numeric constants.


2. Non-numeric variables are used to assign Non-numeric constants.
3. The data type must be used along with the variables accordingly.

The following table illustrates the different data types along with assignment of
relevant constants
Declaration of variable Assigning constant
int a; a 5;
long b b 2345763;
float f f 3.45;
double d; d =0.000000045
char ch; ch = "k'
str = "COMPUTER":
String str
boolean p P= true;

37
Give reasons why the following assignments are false?
i. int n =15.4; ii. float f =12;
ii. char x = "A": iv. String s = "Mouse
vi. boolean p = "true";
V. String x =
true
vii. char p=
'Application
Ans. i. Variable n must be assigned to an integer value.
ii. Variable f must be assigned to a fractional value.
ii. Variable x must contain a value enclosed within single quotes.
iv. String Mouse, variable
constant assigned to s must be enclosed within double
quotes.
v. Variable x should not be assigned to a boolean constant.
vi. Variable p is not
assigned a boolearn literal. i.e.; "true is treated as a
String
constant.
Vii. Variable p must contain a single character within single quotes.
Assign the following literals to the variables declared under
types: various
"Java Language", 345, 'G', 45.65, false
i. int x;
ii. char C
ii. String s;
iv. boolean b;
v. double d;
Ans. i. x=345; ii. c=°G';
ii. s="Java Language"
iv. b=false
v. d-45.65;

Arithmetic Expression
An expression, which contains
variables, constants and arithmetic
an Arithmetic expression. operators, is termed as

X = a+b*4;
e.g.
P p (3+p);

Pure Arithmetic Expression


An arithmetic expression which, contains same type of data items
is called Pure Arithmetic Expression. (variables or constants)
e.g., int a, b, c;
C =a+b*8 -a;
a =(b+5)*(a+4);
You must note that in the given
cxpression a the tems
8 are of integer type). Hence, it is pure expression. (variables a, b, c and constant
Mixed Expression
An expression,
which includes
different types of variables
known as Mixed expression: or values to yield a result, is
e.g., int a; float b; double d;
double x = a + b*d:

In the given example the


expression a+b*d contains different types of variables
float and double. Such expression is called as int,
Mixed expression.

Type Conversion
In a mixed expression the result can be obtained in
any one form of its data types. Hence,
it is needed to convert the various data into
types a
single Such conversion is termed
as Type Conversion. In Java the type conversion can be type. done in
are as shown below:
two different ways which

Implicit fype conversion


In mixed expression, the data type of the result
gets converted automatically into its
higher type without intervention of the user. This system of
type conversion is known as
Implicit type conversion.

Hierarchy of Data types


byte
char The hierarchy shown indicates the
short increasing order of data types. If two data
int
Hierarchy of different types are
operated then result
long automatically gets converted to their
float higher type.
double

e.g. int a; long b; long c;

C)= a b;

int long

long

You must have noticed that two values int and long type in the expression shown above
will result in declare variable
long type value. Hence, you must c as
long type.
Consider the following illustration

char c; int i; float f; double d;

d C + f d
float double
int
char double

double

double

The result obtained in the


illustration shown above is of double
variable d must be a double type. Hence, the resulting
type.

Explicit Type Conversion


Explicit type conversion is another way of type conversion
converted to another type depending in which the data
upon the User choice. This
type gets
the system to get back into the desired means the user forces
data type.
When the data type gets converted to
another type after User
type conversion is known as explicit type intervention, the
conversion.
e.g int a,b;
float x =
(float) (a+b);
In the example given above,the outcome of the
But, (float) provided by the User before the expression a+b has to be int implicitly.
converted to float type. Hence, this expression a+b will cause the result forcibly
type of conversion is also called
or type casting. forced conversio
Type casting is also
applicable to convert the type from
double x,y; higher type to lower type.
e.g.
int c (int) (x+y);
=

The expression shown above will result in double


anlication of type casting the result 1s
type implicitly, but due to
torced to get
converted into a lower type int.

40
Mention the type of values
obtained in the following
i. int x; float y; char c;
ii. char c; int a; double d;
expressions:
X+y#c;
ii. double d; int a, long 1;
c-d/a
iv. float f; char c; short s;
(d/a)*4/1:
(int) f+c/s
v. int a; float f; double d; char c;
(float) (a/f)-d*c;
Ans. i. Implicit
conversion takes place. As float is the
result is obtained
as float
higher type in the expression,
type.
ii. double is the highest
type in the expression. Hence, result is a double type value.
ii. double being the highest
type tends the result to be double type.
iv. As a result of explicit
type conversion integer type value is obtained.
v. As a result of
explicit type conversion double type value is obtained.

Primitive Data Types with their sizes and the ranges at a glance:
Data Type Size Range Deseription
byte 8 bits (1 byte) -128 to +127 Bit wise operations
char 16 bits (2 bytes) To represent a character

short 16 bits (2 bytes) -32768 to +32767 To represent short integers

int 32 bits (4 bytes) -23 to 23 1 To represent integers

long 64 bits (8 bytes) -263 to 263 -1 To represent long integers

float 32 bits (4 bytes) -3.4 E+38 to 3.4E+38 To represent values up


to 6 significant digits

double 64 bits (8 bytes) -1.7E +308 to 1.7E+308 To represent double value


up to 15 significant digits

Exercise
Multiple choice questions
I. Tick ( ) the correct answer:
1. A constant which gives the exact representation of data is called
(a) Variable (b) Literal
(c) Identifier (d) Character

2. A word used in a high level language which has a special meaning attached
to it is called
(a) Class (b) ldentifier
(d) Literal
(c) Keyword
41
3. A character literal is assigned to a
(a) Char variable (b) Char type literal
(c) String variable (d) String literal
4. A character literal is enclosed in:
(a) (b)
(c): d)
5. A set of characters is
assigned to:
(a) String variable (b) Static variable
(c) Boolean variable (d) None
6. The ASCII codes of upper case alphabets ranges from:
(a) 65 9 0 (b)60 85
(c)65 91 (d) 97 122
7. Which of the
following results in integer type:
(a) 11.4F/3.2D
(b) 13.8F/4.6F;
(c) 12/3
(d) none of these
8. Which of the
following is non-primitive data:
(a) char
(b) long
(c) object
(d) short
9. Which of the
fotlowing type is exact
representation of fractional values:
(a) char
(b) double
(c) byte
(d) String
10. Boolean Data is used to test
of the
a
particular condition i.e. true false. Which
following is correct
representation?
or

(a) boolean m =true


(b) boolean m='true'
(c) boolean m="true"
(d) none
II. Write short answers:
1. Define variable.
2. What do you mean by constant? Explain with ICSE-2006]
3. State two kinds of data
types.
an
example.
4. What do understand [ICSE-2006]
by Token? Name different
5. What are the rules to types of tokens.
assign a variable in a Java
6. Distinguish between programming?
(a) Integer and floating constant
(b) Character and String constant
(c) Suring and Boolean constant
7. Write down the Data
type of the following:
(a) Integer b) Long Integer
(c) A fractional number
(d) A special character
8 What do you understand by Boolean type data?
a what do you understand by primitive and
Explain with an example.
non
examples of each. primitive data type? Give two

42
10. Define the following with
(a) Implicit type conversion
an
example each:
(b) Explicit type conversion
(c) Literals
(d) Identifiers
11. What is the need of
declaring a variable in java?
12. Differentiate between tokens and
identifiers.
13. Differentiate
between separators and [ICSE-2008]
14. What do you mean by initialization
punctuators.
of a variable?
15. In what way static declaration is
different than dynamic declaration?
16. What do you by non-primitive data type? Give
mean

17. A examples.
non-primitive data type is also referred as reference type.
18. Why?
Explain the term type casting?
19. Perform the
(ICSE-2007|
following:
(a) Assign the value of pie (3.142) to a variable with the requisite data type.
(b) Assign the value of 3 (1.732) to a variable with the ICSE-2007
requisite data type.
20. Predict the return data type of the
following:
i. int p; double q;
r = p+

System.out.println(r);
ii. float m;
P m/3*(Math.pow(4,3);
System.out.println(p:
21. What are the resultant types if the following implicit conversions are performed?
int i; float f; double d; char c: byte b;
(a) i + c/b; (b) f/d + c*f;
(c) i + f- b#c; (d) i/c + f/b;
(e) i + f- c+ b/d;
22. What are the outputs of the following explicit conversions?
int i; float f; double d; short s; char c; byte b;
(a) (float) i/b + d: (b) (int)f*d + c/s;
(c) (char)i + f - b*d; (d) (double) (f/i)*e + s;
(e) (char) d + b/i - f#s;

You might also like