Concepts of Data Types in Java
Concepts of Data Types in Java
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:
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
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.
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
Integers Floating
Numbers Characters Boolean String
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:
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.
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:
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
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.
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 (" *").
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);
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
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
d C + f d
float double
int
char double
double
double
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
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
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;