0% found this document useful (0 votes)
91 views

003 - Values and Data Types

Uploaded by

Bumba Dey
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views

003 - Values and Data Types

Uploaded by

Bumba Dey
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

3

Values and Data Types

Learning Scope
sequences, Tokens, Differeer
Introduction, character sets in Java, Unicode, ASCII code, Escape
type of tokens (Keywords, Literals, Identifiers, Punctuators, Operators), Constants and variables
Initializing a variable, Assignments, Data types in Java: Primitive and Non-Primitive types, Tyne
Conversion: Implicit and Explicit type conversions.

INTRODUCTION
A language is a medium of communication. People can share their viewe
with each other, if they know a common language. Hence, language plays an
important role in communicating our thoughts and ideas.
Java is a computer language that enables the users to communicate with the
computer. It uses character sets similar to alphabets of general languages. The
character sets used in Java are as shown below:

Character sets
in Java

Letters Delimiters

Digits Operators

CHARACTER SETS
The different character sets are explained as under:
" Letters : All the letters of English alphabet (A-Z and a-z) can be used in
Java language.
" Digits : Digits (0 -9) can be used in Java
language.
" Operators :Operators can be classified in three different
categories.
(i) Arithmetical Operator: All arithmetical operators like +, ,*, I %
are
applicable in Java programming.
(i) Logical operator: In Java, &&, ||, !are used as
(iii) Relational operators: The Java language uses
logical operators.
<5, >, >=, == and !=,
relational operators as

44
" Delimiters : Delimiters are the special characters. In Java
are used as language, delimiters
-,;:, ?, ., , ) , , , etc.
UNICODE
When a character is
entered from the
keyboard, a specific code is assigned Hence, Unicode is a standard
by the system for its storage in encoding system created by Unicode
the consortium that is used to encode a
memory. Earlier the ISO and IEC
character in any computer language.
coding schemes which were in use
represented only a limited number of characters. Nowadays, the latest coding
system of the characters followed world wide, is known as the Unicode.
the Unicode is awide representation of characters in the numeric form. The code
contains hexadecimal digits ranging from 0x0000 up to OxFFFF (i.e. 16-bits code). It
can address 1, 60, 755 characters in computer from 139 modern and historic scripts.
It means a code of each character is available under the Unicode character set.

Advantages of using Unicode


The advantages of the character coding scheme by using the Unicode are shown as:
" It is the universal coding scheme followed throughout the world.
" It is a more efficient coding system than the ISO or IEC.
" It supports uniform coding width for all the characters (16-bits).
" Aparticular code of a character is always unique, i.e., there will never be one
code for more than one character.

The table shown below illustrates some characters represented by their


Unicodes:

0 1 2 3 4 567 8 A B D E F

0020 ! % & (

0030 1 2 3 4 6 8 ?

D F G H K
0040 A 3 M N

R S T V W X Y Z
0050

a b d e f h j 1 m
0060
r W X y
0070 P
ASCII CHARACTERS AND CODES
used in the computer are said to beCote tr
ASCIl
intratson lnterchang.
characters.
The characters
Each character is assigned
Standard
ASCII stands for American are
called the ASCII code. The ASCN codes
is not so
the decima
aspecific numeric value, digits. The range of code large
as
numbers represented in 7 binarv tor limited
Compared to Unicode.
ASCII codes are
available
Basicallv, the
only
ASCIl codes are used number o-
127.
characters ranging from 0 towith characters.
programming, when it deals
of well-known characters are as listed below:
Some ASCII codes
ASCII Characters
ASCII Codes ASCII Characters
ASCII Codes
S6
48
87 W
49 1
X
50
89 Y
51
4 90
52

53 97
54 6 98 b
55 7 99
56 8 100
57 9 101
*******
102 f
65 103
66 B 104
67 105
D 106
E 107
70 F 108
71 G 109 m
72 H 110
73
111
74 J
112
75 K
76
113
L
77 114
M
78 115
79 I16
80 117
P
81 118
82 |19
R
83 120
S
84 T
121
85 122
U

46
Understanding Computer Applications with uc
Note
() 58-64 are different symbols such as <, >, i, etC.
(0) 91-96 represents some special symbols viz. [.),\, ^, etc.
(ii) ASCIl code of white space (blank) is 32.

Difference between Unicode and ASCII code


ASCII Code
Unicode
1. Unicode is a generalised form of coding 1. ASCII code is a specific coding scheme
scheme for numerous characters of used for limited characters.
different scripts.
of 2. ASCII code represents a limited range
2. Unicode represents a higher range
codes. of codes.

Escape Sequences direct the


non-graphic characters, which are used as commands to
Inere are some programming,
frequently used in Java
cursor while printing. These characters are sequence character begins with a
and are called Escape Seq1uences. An escape
followed by one or more characters. This is the reason
backslash (\) and it is
also called back slash characters. A table is given for
why escape sequences are
your reference.
Non-Graphic Character
Escape Sequences
Horizontal tab
t
Backslash
Single quote
Double quote
Backspace
Form feed
f
Null
Carriage return
New line feed

Using Escape Sequences programming to control the


are available Java
Basically, escape sequencesscreen. It allows the user to customise the screen and
cursor's movement on the enclosed
Escape sequences are used with print statement
get a formatted output.
within double quotes. Java programming, are
are commonly used in
Some escape sequences which
explained below: used for a new line feed.
As soon
This character is
() "\n" (Backslash n): current line and moves to
encountered, the cursor skips the
as a "\n" is
screen for printing the remaining part of the output.
the next line on the
Values and Data Types47
For example,
System.out.printn("Name: Ayush \n"+"Age: 14 yrs\n"+"Class IX'"):
In the print statement shown above, "\n" is included along with .
message in double quotes. Thus, the output of the statement will be a
shown below:
Name : Ayush
Age : 14 yrs
Class : IX
If the print statement contains variables then you need to use "\n"
separately to display the message in different lines.
For example,
int a = 4, b = 5;
System.out.println("First number ="+a+"\n"+"Second number ="+b);
The output of the statement will be as shown below:
First number = 4
Second number =5

(i) "\t" (Backslash t): This character is used to separate the values while
printing them on the screen. It will provide a gap of 8 spaces between
them. It is also called "Tab Spacing'. It acts similar to the "Tab' key, when
pressed on the keyboard. The application of "\t" is shown below:
For example,
int a=4,b=5;
System.out.println("First number="+a+"\t+"Second number="+b);
The printing of first and second numbers will appear on the screen at a
gap of 8 spaces.
Output:
First number = 4 Second number =5
You can also include "\t" along with the message under same double
quotes, if multiple messages are to be separated by tab spacing.
For example,
System.out.println("Name: Aditi\t"+"Age: 14yrs\t"+"Class: IX");
The output will appear on the screen as under:
Name: Aditi Age: 14yrs Class: IX

(üi) \" (Backslash double quotes): It is used with System.out.printin


statement when you want to display a message on the screen in Suet
way that apart of it needs to appear under double quotes.
For example,
System.out.println("Welcome to \"Computer\"world");
It will display the message on the screen as:
Welcome to "Computer" world

48 Understanding Computer Applications with Bluej-IX


(iv) \\ (Double backslash):
The double backslash is used to insert a backslash at the point of its
appearance in the message or the output used with System.out.println()
statement. The first backslash is used as a command whereas the next
backslash is used as the character to appear in the text.
For example,
System.out.println("The selected candidate is"+"\1"+"Suman
Awasthi"+"\1");
The output will be:
The selected candidate is \Suman Awasthi \
character is used to insert a single quote
(v) \' (Backslash single quote): This
() at the point of its appearance in the output message.
For example, Srivastava \");
System.out.println("Top ranking student is"+"\'Amit
The output will be:
Srivastava'
Top ranking student is 'Amit
Token
biology about the 'cell' which is a fundamental and
You would have studied in Like a cell in the human body, a token is
human body.
a functional unit of thefundamental unit of a computer program. You know that
also a functional and a statements. A statement is composed of various
is a set of statement is
a computer program component of a programming
components and each individual
referred to as a Token.
various types of tokens available in Java are:
The
" Literals defined as each
A token can be
" Identifiers individual component of a Java
statement such that it carries
" Assignments
some meaning and takes part
" Punctuators of the
in effective execution
" Separators program.
" Operators
" Keywords
Tokens

a
b

Operator Operator
Assignment
Literal Punctuator
ldentifier
ldentifier
Identifier

Values and Data Types 49


Literals (Constants)
Literals are the constants in a Java program. When you write a program in Java,
vou may come across some quantities, which remain fixed (i.e. do not change)
throughout the execution of the program. Such quantities are termed as Lilerals
or Constanls.
Java literals are classified as under:
" Integer Literals: The numbers which are represented without decimal points
are called Integer Literals. They are the whole numbers having positive or
negative values. For example, 14, 345, 8, 6392, -18, -391, etc.
. Real Literals: Real literals are also called floating-point constants. They
represent numbers with decimal points.
For example, 24.6, 0.0072, -3.652, 1.0E-03, etc.
" Character Literals: The constants, which are alphanumeric in nature, are
called character literals. All letters (upper case or lower case), digits, special
symbols can be termed as character literals.
For example, 'A', 'd', '3, , etc.
Acharacter literal represents a single character enclosed within single quotes.
" String Literals: A string is a set of alphanumeric characters. A group of
characters enclosed within a pair of opening and closing double quotes is
known as a string literal.
For example, "COMPUTER", "Year 2016", "10% per annum", etc.
" Boolean Literals: Boolean constants are special literals. They represent true
or false and can be used in a Java program to check whether a given logical
condition is satisfied or not. You must note that boolean constarnts (i.e. true
or false) are never enclosed within quotes. This characteristic makes boolean
constants different from string constants.
" Null Literal: This is also a special purpose literal. It is represernted as null
(each letter in lower case) and is used to initialize an object or reference
variable.

Identifiers (Variables)
The identifier is a term used to represent program elemnents such as function
name or class name. The variables used in Java programming are also called
identifiers. 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 variable of the same type in a statement.
Syntax: <data type> <space> <variable name>
int m;
float p4T;
The value assigned to a variable gets stored at the specified location in the
memory. lf any change in its value occurs due to an operation, it is maintaine

50 Understanding Computer Applications with Bluel--IX


in the same location by replacing the existing value. Thus, a variable can change
its value as shown below:
Before Execution After Execution

int m 5: int m = m"m; 25


'm' changes in the
Data value 5 is stored in location The value of the variable
named 'm' memory after the execution

Rules for naming a variable


characters.
" A variable may have any number of
" It may contain alphabets, digits, dollar sign and underscore.
" The underscore can be used in between
the characters to separate the words
of a variable name.
which easily depicts its purpose.
" The variable names should be meaningful,
Assignments
to store constants in variables using a token '= symbol. Here,
Assigning means
acts as an assignment operator. The data type of the variable
the symbol '= constant. You must ensure that the constant
depends upon the type of assigned
the same data type as the variable declared.
you are going to store has
<constant>;
Syntax: Data type <variable> = variable m which is integer type
: 15 is stored in the
For example, int m = 15; type
float n = 45.24; stored in the variable n which is float
:45.24 is
store a character
The data type char is used to
char chr = 'k; variable chr.
enclosed within single quotes () in a
String str = "Computer
Applications"; a word/a
is used to store
: The data type String variable name (str),
sentence/a paragraph in the
enclosed within double quotes (" "). the
illustrates the different data types along with
The following table
constants:
assignment of relevant
Declaration of variable Assigning constant
a = 5;
int a;
b= 2345763;
long b;
f= 3.45;
float f;
d= 0.000000045;
double d;
ch = 'k;
char ch;
str= "COMPUTER":
String str;
p= false;
boolean p
Initializing a variable valuel
declare a variable, it may contain garbage values (absurd
When you
the execution of your program. You may not
during
and can create a problem even though your program logic is correct. Under
obtain an appropriate result to initialize a variable to get the desired result
such circumstances, you need specific value to it.
variable is initialized by assigning a the following ways:
Initialization of a variable takes place in
" Static Initialization
" Dynamic Initialization
Static Initialization
assignment of a constant to a defined variable. The
This process uses direct (i.e., before its actual use in
variable is initialized at the time of its declaration
program logic). The table for the same is as shown below:
the
Declaration
Static Initialization
Data Type
int a; a=0;
Integer
Float float f; f-0.0;

Double double d; d=0.0;

char c; C= \u0000';
Character

String String s;
Boolean boolean p, p=false;

Dynamic Initialisation
When a variable gets initialised at run time, i.e., during the execution of program
logic, it is termed as Dynamic Initialisation. Under this situation, a variable is
assigned with the outcome of any arithmetical operation or function. The table
is as shown below:

Data Type Declaration Dynamic Initialisation


integer int a,b,c; C=a+b;
float float p=2.2,k=4.22,f; f=p+k;
double int a=49; double d;
d=Math.sqrt(a);
String String stl, st2, st3; st3=st1+st2;

Punctuators
Punctuators are the punctuation signs used as special characters in Java. Some
of the punctuators are:
(i) ? (question mark)
(ii) . (dot) colon)
(iii) ; (semi
52
Understanding Computer Applicatinns uith 1
Question mark (?)represents the action to be taken when the given condition
is true while using the ternary operator.
For example, max = (a > b)? a : b;
Dot () is used to represent the scope of a function i.e. a function belonging
to an object or a class.
For example, (0) System.out.printin():
(ii) 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 the semi colon is treated as the
next statement.
For example, int a =5; System.out.println(a); are treated as two separate
statements in Java.

Separators
separate the variables
They are the special characters in Java, which are used to
or the characters.
Square brackets [ ], etc.
For example, Comma(,), Brackets (), Curly brackets ( ),
variables under
Comma ) in a Java program, is used to separate multiple
the same declaration.
For example, int a,b,c;
relational expressions.
Brackets () are used to enclose any arithmetical or
statemernts under a compound
Curly brackets { }are used to enclose a group of
statement.
brackets are used to enclose subscript or the cell number of a
Square
dimensional array.

Operators
basically the symbols or tokens that perform arithmetical or logical
Operators are operators used in Java:
operations. Basically, there are three types of
(a) Arithmetical Operators: +, /", etc.
(b) Relational Operators: <,>=5, =, <=, etc.
(c) Logical Operators: &&, || ,, etc.
Keywords preserved by the system and carry
Keywords are the reserved words which are
During the course of programming you
special meaning for the system compiler.requirements.
need to use keywords to meet certain
for, etc.
For example, class, public, throws,

DATA TYPES IN JAVA phase


called the storage assignment phase. This
The compiler contains a phasevariables
allocates memory for different used in your program. It also creates the
the compiler must
structure in the location to store the data efficiently. Hence, ensure optimum
know the type of data you are likely to supply for storage to
Values and Data Types
utilisation of memory space. This is the reason why data types are required in
Java programming. In Java programming, we need to deal with various types
data. Hence, it becomes necessary for a programmer to select an appropriate
ofdata type according to the data taken in a program. The data and types are
given below:
Primitive Types
A
T
Numeric Types Non-Numeric Types
A Characters
Integers
Floating Numbers " Boolean
T
Y Non-Primitive Types
P
E
Classes Arrays Interface

Primitive Types
The data types which are independent of any other type, are known as Primitive
data types. These types are also called Basic Data Types.
For example, byte, int, long, float, double, etc.
Primitive data types are pre-defined or built-in data types because the system
developers of Java have defined them. You can declare a variable of type int.
which will follow the characteristics mentioned in this type.
For example, int x;
It means variable x follows the characteristics of the int type. Hence, variable
x will contain only integer value.
Let us discuss primitive data types in detail:
Integer Type
A variable declared as an 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 : Used for bit-wise
operations
" short : Used for a small range
of integers
" int : Used for integers
which are more than short integers
" long : Used for large integers
A programmer has to select an appropriate data type according to the need
of the program, as shown below:
Data
Data Type Bit Size Format
Byte byte 8bits (1 byte) byte a; a=5;
Short short
16 bits (2 bytes) short b; b=12:
Integer int
32 bits (4 bytes) int c; c=214;
Long Integer long 64 bits (8 bytes) long d; d=45687:

54 Understanding Computer
Applications with Blue<-IX
Floating type
When you need to store a fractional number (a number with decimal points),
then we declare a variable of the floating type. You can define these numbers
in two different ways to represent the data values.
float: It represents a fractional number with a small range of values.
" double: It represents a fractional number with a wide range of values.
Aprogrammer decides the data types according to the ned. Refer to the
table shown below:
Data Data types Bit size Format

Small range of decimal values float 32 bits float m: m=34.45:


Wide range of decimal values double 64 bits double n; n = 12.1269387;

Characters
A character type variable contains a single character. There are 256 ASCII
characters out of which only 128 characters are in use. Each ASCII character
is assigned a specific numeric value called ASCII code. The ASCII codes of the
characters range from 0 to 127.
Some well known characters and their ASCII codes are as follows:
A-Z:65 - 90
a - z :97 - 122 (Respectively)
0-9 48 - 57

Other codes are used for special characters.


Java language considers a single character and a set of characters (i.e. String)
differently. Each character is assigned an ASCII code, which taken into
consideration during Java programming. In Java, the declaration a character
is explained as:
Syntax: <Data type><variable> = <'character literal'>;
char p = 'm';
. a character which is assigned to variable p
data type variable

Note: It must be noted that a character is always


enclosed within single quotes.
Similarly, a String (set of characters) is declared as:
Syntax: <Data type><variable>= <"String constant">;
String p = "Computer Applications with Bluej";
-+a String which is
data type variable assigned to variable p
Note: It must be noted that a
String is always enclosed within double quotes.
The character types in Java are as folloWs:
Non-numeric Character type Bit size
16 bits (2 Format
Single character (a letter or char
bytes)
char p. p=A;
a special character). char x; X=*.
More than a characterla More than 16
word/a sentence.
String
bits String str;
Arithmetical Expression and Statement
str="School";
Aset of variables, constants and arithmetical operators used together to v.
meaningful result is known as an Arithmetical Expression. When an arith
expression is assigned to a variable then it is called an Arithmetical Stake
For example,
Arithmetical Expression
d= b*b-4*a * c;
Arithmetical Statement
Type of Arithmetical Expressions:
Based on the data types, the
" Pure Expression arithmetical expression is of the following two types:
Impure Expression
Pure Expression
An arithmetical
expression that
expression. uses all its
known the pure
For example,
components of same data types is
int a,b:
int c= a +b* 4;
In the
type data.expression shown
Hence, it is a Pureabove, all its components like a, b and 4 are
Impure Expression:
Expression. integer
An
data arithmetical expression
types, is referred in which one or
to as an more components are
Expression. Impure Arithmetical of different
For example, Expression or Mixed mote
int a; float f;
double d;
double s = af | d;
Here, a, f and d
are of
Type Conversion
different data types.
In a mixed
Hence, it is an
impure expression.
expression,
types. Hence, it is the result can be
needed to
Such
place inconversion
is convert the obtained in any one form of its data
the termed
following as Type various data types into a single type.
two ways: Conversion. In Java, type conversion takes
56

Understanding Computer Applications with


Blue<-IX
mplicit type conversion
In a mixed expression, the data type of the result gets automatically converted
intothe highest data type available in the expression without any intervention
-of the user. This system of type conversion is known as Implicil type converston
or Coercion.

Hierarchy of Data types


For example int a; long b; long ci

byte C= a b
char
short
int int long
long
float
double
(Hierarchy of the data types) long

The hierarchy shown above, indicates the increasing order of the data types. If
two data of different data types are operated upon then the result automatically
gets converted to their highest data type.
You must have noticed that two values int and long type in the expression
shown above will result in the higher 'long type' value. Hence, you must declare
variable c as long type.

Consider the following illustration:


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

= C

float double
nt

cha
double

double

double

The result obtained in the illustration shown above is of the type


the resulting variable d must be a double double. Hence.
data type.
It is further to be noted that
Java Language is very powerful on
operation. So, the user must be very caretul of data type
writing any Java expression. using suitable data types while

Values and
(fractional Explicit At A typeint. to to implicitly. isthe intervention, type.means Conversion
double
0.214;
d=
f=0.214;
float For
example, variable
the implication The int For Type a alsoresult Infloat For When
Explicit Type area final 0.because
type Explicit 5). in the
The zero At formula area float
cx,y;lower the resultWhen Suppose,
time expression example, double
= castingcalled example,
x that gets above
= the =
a,
value)Conversion (int) example the type 1.0/2.0 the for time 1/2 b,
declared type. to But, (float) = converted asthis
the
of of (x+y); is
be
type (float) the data conversion expression 0 result whatever of area; * h, the
to
initialization to typeshown forcibly
alsocasting. given (a+b); int type
user * (zero).value calculate user
(a
be either a,b; type (a execution,
of +
provided b)h;
stored of casting applicable
converted above,
conversion demands to +
b)can isthe may wants
Literals above gets another is *multiplied division area
as another h; be beyou to
into float it the converted are
becomes
by the the rectified the calculate
the result will in the
outcome
to is
system way
type (1/2) wil shown
or converting known with values
result float User get
varjable double
is to
depending as is
difficult
forced in type.before of to
of shown the confused, below: area
anotheras implicitly of
type th e the explicit get type other the of
is double Hence, the a
foat to to data expression the upon
conversion below variables trapezium.
contains get expression data result data as
say ty pe an the
type converted type type this
type
the tovalues,
integer area
whether conversion. in
get
or
fractional a implicitly, from type (a+b) user's in a, The
double after the the result b wil
(a+b) which it
a into a of
has desired desired will type always and data
higherconversion the
choice.
(i.e., h.
yr real
wil to the
types
lo a but user' s
vielda This
value cause be rocslk
literal type int data This data 0
not is and
value 0.214 to the floating as
The example shown above, initializes the same
type variables. How will you decide whether the value 0.214 is
well as double
a floating constant or a double type constant?
problem, explicit conversion of literals is allowed in Java
To sort out the floating type variable and
programming. The literal to be initialized into the
double type variable, must be suffixed with F and 'D' respectively.
For example,
float f-0.214F;
double d=0.214D;
assigned to the variable
Now, it is clear that the value 0.214F is a floating literal d.
f and 0.214D is double type literal assigned
to variable
Asimilar problem may arise during the initialization of integer and long type
a long type literal
variables. A whole number using a suffix 'L' is referred to as literal.
integer type
whereas a whole number without any suffix is an
For example,
int a=12;
long b = 12L;

Quick Recap
assignments are true or false:
Justify with reason whether the following
(i) int n =15.4;
an integer value.
Ans. False, the variablen must be assigned
(ii) float f =12.02;
fractional value.
Ans. True, the variable f is assigned a
(iii)) char ch = 'A;
Ans. True, the variable ch contains a
letter enclosed within single quotes.

(iv) String str = "Mouse";


variable str, is enclosed within
Ans. True, the String constant"Mouse", assigned to
double quotes.
(v) String p = true;
constant.
Ans. False, the variable p should not be assigned a boolean
(vi) boolean m = "true";
Ans. False, the variable m should not be assigned a String literal. i.e.: "true" is
treated as a String constant.
(vii) char p= 'Application';
Ans. False, the variable p must contain a single character within single quotes.
Practice Session
obtained when the following expressions are executed
Mention the type of values
Data type
Expressions

int x; float y; char c;


X+y*c;
d;
char c; int a; double
c-d/a;
double d; int a, long p;
(da)"4/p;
float f; char c; short s;
(int) f+cls;
int a; float f; double d; char c;
(float) (al)-d*c;

a glance:
Primitive Data Types with their sizes and ranges at
Data Type Size Range Description

byte 8 bits (1 byte) -128 to +127 Bit-wise operations


short 16 bits (2 bytes) -32768 to +32767 To represent short integers
int 32 bits (4 bytes) -231 to 231-1 To represent integers
long 64 bits bytes) -263 to 263-1 To represent long integers
float 32 bits (4 bytes) -3.4 E+38 to To represent values up to 6
3.4E+38 significant digits
double 64 bits (8 bytes) -1.7E +308 to To represent double values
1.7E+308up to 15 significant digits
REVIEW INSIGHT
(a) from
What explicit
do you conversion?
mean by type conversion? How is implicit conversion different
[ICSE 2010]
AnS. In a mixed expression, the result must be obtained in a specific data type. For
this purpose, the data type needs to be converted into the required type. This
is known as type conversion.
In an implicit type conversion, the result of a mixed mode expression is obtained
In the higher data type among the variables in the expression without any
intervention of the user.
For example, int a; float b; float C;
C= a + b;
In case of explicit type conversion, the data type gets converted to another data
type as per the user's choice and requirement.
For example, int a; float b; double c;
b = (float) (a * c);
(b) Give one example each of a primitive data type and a composite data type.
[ICSE 2012]
Ans. Example of primitive data type: int, float, double, long, char, etc. (any one)
Example of composite data type: array, class, interface, etc. (any one)
(c) State the values of n and ch.
char c='A;
int n=c+l;
char ch(char)n; [ICSE 2012]
Ans. The value of
() n =66
(i) ch = B
(d) What is a literal? [ICSE 2013]
Ans. Literal is a constant that remains fixed throughout the discussion of a program.
A literal is value that can be assigned to a specific variable,
For example,
Integer literals: 12, 4, 342, etc.
Real literals: 1.02, 345.657, 0.00002, 1.0e-05, etc.
Character literals: 'A, 'p', ':', ?, etc.
String literals: "COMPUTERS", "PAY2013", "3421", etc.
Boolean literals: true or false
(e) What are the types of casting shown in the following examples?
double x = 15.2;:
int a = 12;
() int y = (int) x;
(i) long b= a; [ICSE 2013]
Ans. () Explicit type casting
(i) Implicit type casting
() Name the primitive data type in Java that is:
A 64-bit integer and is used when you need a range of values wider than
those provided by int.
(io A single 16-bit Unicode character whose default value is \u0000'.
(ICSE 2014]
the following are:
Ans. The primitive data tvpe for
() long
(i) char
type l
difference between the floating point literal and double
(g) State one

number with short precision and the


|1CSE. 2014)
Ans. F1oating point literal is a fractional whereas double type literal is used fo
can range from -3.4E+38 to 3.4E+38, from -1.7E+308 to 1.7E+308.
precision and the values can range
primitive data type int and float?
(h) What are the default values of the
[ICSE 2015
Ans. The default value of int is 0 and float is 0.0F
(i) What are identifiers? [ICSE 2015)
memory locations to contain
Ans. ldentifiers are the variables that are named
circumstances or problems
values. The values may change depending upon
G) ldentify the literals listed below:
() 0.5 (i) 'A
(ü) false
(iv) "a" [ICSE 2015
Ans. () Real (ii) Char
(ü) boolean (iv) String
(k) Arrange the following primitive data types in an ascending order of their sze
() char (i) byte
(ii) double (iv) int [1CSE 2015
Ans. byte, char, int, double
) What are the types of casting shown by the following examples?
[1CSE 2015
() char c= (char) 120; (ii) int x =t';
Ans. () Explicit (i) Implicit
(m) Write one difference between primitive data types and composite data type
(ICSE 20::
Ans. The fundamental or basic data types (viz. int, float, char) that are prov
with some built-in characteristics, are known as primitive data types. Whereas
derived data types or non-prinitive data types are called as composlle
types. For example, class, array.
(n) Differentiate between float data type and double data type initializatiots
[lCSE MtODE
Ans. A fioat type variable is initialized with a fractional value such as 0.0F weY
d1tterenY
double type variable is initialized with a fractional 0.0D. The d
between initializations is the suffix L and D value as
For example, float f=0.0F: respectively.
double d=0.0D;
(o) What do you mean by type |ICSE MOPE
Ans. In an impure expression, where
conversion? resultnen
the
data various types is included, resultisfo
of
to be obtained in a
specific data type. The data type in which the requireme
obtained is converted as per the suitability of the or the
the user. This conversion is referred to as type
system
conversion.
EXERCISES
1. Multiple Choice Questions
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 for the
system compiler is called
(a) Class (b) Identifier (c) Keyword (d) Literal
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 range from:
(a) 65 - 90 (b) 60 - 85 (c) 65 - 91 (d) 97 - 122
7. Which of the following results in integer type?
11.4F/3.2D (b)
13.8F/4.6F: (c) 12/3 (d) none
8. Which of the following is
(a) char
non-primitive data?
(b) long (c) object (d) short
9. Which of the following type is an
exact representation of fractional values?
(a) char (b) double (c) byte (d) String
10. Boolean Data is used to test a
particular condition i.e. true or false. Which of
the following is a correct
(a) boolean m=true representation?
(b) boolean m=true'
(c) boolean m="true"
(a) none
II. Fill in the blanks:
1. The character sets of Java is like
of English language.
2. A standard encoding system of representing characters is
3 code is decimal number to
4.
represent a character.
Each individual component of a Java
5. In
statement is known as
Java, the constants are also called
6.
operator is used to store a value in the
7. The comma, exclamation, question mark etc., are variable.
Java language. termed as
8. An element of Java
program that is used to identify a
is called as class, function or value
9. Integer type value occupies
10, A Java bytes in the
expression that contains all the memory.
expression. elements of same data type is
III. Write short answers:
1. What do you mean by data type?
2. Define variable with an example.
3. What do you mean by constant? Explain with
an example. |1CSE 2006)
4. State two kinds of data types.
types of tokens.
| CSE 2006)
5. What do you understand by Token? Name different
programming?
6. What are the rules to assign a variable in a Java
7. Explain the term type casting"? |ICSE 2007)
8. Perform the following:
the requisite A.
(a) Assign the value of pie (3.142857......) to a variable with
type. |ICSE 2007)
data tyDe
(b) Assign the value of V3(1.732) to a variable with the requisite
9. Distinguish between:
(a) Integer and floating constant
(b) Token and Identifier |ICSE 2008)
(c) Character and String constant
() Character and Boolean literal [ICSE 2009|

10. Write down the data type of the following:


(a) Integer (b) Long Integer
(c) A fractional number (d) A special character
11. What do you understand by Boolean data type? Explain with an example.
12. What do you understand by primitive data type? Give two examples.
13. Why is it necessary to define data types in Java programming?
14. Define the following with an example each:
(a) Implicit type conversion (b) Explicit type conversion
15. Define 'Coercion' with reference to type conversion.
16. What do you mean by type conversion? How is implicit conversion different
(ICSE 2010)
from explicit conversion?
17. In what way is static declaration different from dynamic declaration?
18. What do you mean by non-primitive data type? Give examples.
19. Predict the return data type of the following:
() int p; double q: (ii) float m;
r = p+q; p = m/3* (Math.pow(4,3)):
System.out.println(): System.out.printin(p):
20. What are the resultant data types, if the following implicit conversions a
performed? Show the result with flow lines.
int i; float f; double d; char c; byte b;
(a) i + c/b; (b) fld + c*f;
(c) i + f - b'c: (d) (f/i)*c + s;
(e) i + f- c + b/d; () ilc + f/b:

You might also like