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

CH 3 Values and Data Types

Uploaded by

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

CH 3 Values and Data Types

Uploaded by

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

Values and Data Types

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

INTRODUCTION
A language is a medium of communication. People can share their views
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 Operat:or:All arithmetical operators like +, -, *, / , % are
applicable in Java programming.
(ii) Logical operator; Jn Java, I I ! are used as logical operators.
(iii) Relal:ional operal:ors;The Java language uses relational operators as C'
and
• Delimiters : Delimiters are the special characters. In Java language, delimiters
are used as 5, (f [t I etc.

UNICODE
When a character is entered from the
keyboard, a specific code is assignedHence, Unicode is a standard
encoding system created by Unicode
by the system for its storage in the consortium that is used to encode a
memory. Earlier the ISO and IEC r 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 a wide representation of characters in the numeric form. The code
contains hexadecimal digits ranging from Ox0000up to OxFFFF(i.e. 16-bits code). It
can address 1, 60 755 characters in computer from 139modern and historic scripts.
It means a code of each character is available under the Unicode character set.

Advantagesof 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).
• A particular 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:

0020

0030

c F G H 1 J K L M N o
0040

Q R s T U v w x Y z
0050

I m n
0060

0070

Values and Data Types 45


ASCII CHARACTERS AND CODES
ASCII stands for AntericanStandard Code/or Interchange.The
used in the computer are said -t&be ASCII chÅkÅétGÅlfachcharacter is assigned
a specific numeric value, called the ASCII code. The ASCII codes are the decimal
numbers represented in 7 binary digits. The range of code is not so large
compared to Unicode. ASCII codes are available only for limited numberasof
characters ranging from 0 to 127. Basically,the ASCII codes are used whil
programming, when it deals with characters.
Some ASCII codes of well-known characters are as listed below:
Ascn Codes ASCII Characters ASCII Codes ASCII Characters
48 86
49 87
50 88
51 89
52 90

53 97
54 98
55 99
56 100
57 101
102
65 103
66 104
67 105
68 106
69 107
70 108
71 109
72 110
73 111
74 112
75 113
76
77 115
78 116
79 117
80 118
81
119
82 120
83 121
84
122
85

46 Understanding Computer Applicationswith BlueJ


Note
(i) 58-64 are differentsymbols such as < , ., etc
(ii) 91-96 represents some special symbols viz. ,etc.
(iii) ASCII code of white space (blank) is 32.

Difference between Unicode and ASCII code


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

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

UsingEscape Sequences
Basically,escape sequences are available in Java programming to control the
cursor's movement on the screen. It allows the user to customise the screen and
get a formatted output. Escape sequences are used with print statement enclosed
within double quotes.
Some escape sequences which are commonly used in Java programming, are
explained below:
(i) "\n" (Backslashn): This characteris used for a new line feed. As soon
as a "\n" is encountered, the cursor skips the current line and moves to
the next line on the screen for printing the remaining part of the output.

Valuesand Data "IYpes 47 Y


For example,
System.out.println("Name:Ayush\n"+"Age: 14 yrs\n"+"Class
In the print statement shown above, "\n" is included along with the
message in double quotes. Thus, the output of the statement will be as
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("Firstnumber ="+a+"\n"+"Second number
The output of the statement will be as shown below:
First number = 4
Second number = 5
(ii) "\t" (Backslasht): 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

(iii) X" (Backslash double quotes): It is used with System.out.println( )


statement when you want to display a message on the screen in such a
way that a part of it needs to appear under double quotes.
For example,
to \"Computer\"world");
It will display the message on the screen as:
Welcometo "Computer" world

48
Understanding ComputerApplications 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 commandwhereas the next
backslash is used as the character to appear in the text.
For example,
System.out.println("The selected candidate is"+"\ \"+"Suman
Awasthi"+"\ \ ");
The output will be:
The selected candidate is, \Suman Awasthi\
(v) \ ' (Backslash single quote): This character is used to insert a single quote
(') at the point of its appearancein the output message.
For example,
System.out.println("Topranking student Srivastava\"');
The output will be:
Top ranking student is 'Amit Srivastava'

Token
You would have studied in biology about the 'cell' which is a fundamental and
a functional unit of the human body. Like a cell in the human body, a token is
also a functional and a fundamental unit of a computer program. You know that
a computer program is a set of statements. A statement is composed of various
components and each individual component of a programming statement is
referred to as a Token.
The various types of tokens available in Java are:
• Literals
A token can be defined as each
• Identifiers individual component of a Java
• Assignments statement such that it carries
Punctuators some meaning and takes part
Separators in effective execution of the
program.
Operators
Keywords
Tokens

Assignment Operator Operator

Identifier Identifier Identifier Literal Punctuator

Values and Data Types 49


Literals (Constants)
Literals are the constants in a Java program. When you write a program in Java
you may come across some quantities, which remain fixed (i.e. do not change)
throughout the execution of the program. Such quantities are termed as Literals
or Constants.
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, I.OE-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.
A character literal represents a single character enclosed within single quotes.
String Literals:A string is a set of alphanumericcharacters.A groupof
characters enclosed within a pair of opening and closing double quotesis
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 constants (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 represented as null
(eachletter in lower case) and is used to initialize an object or reference
variable.

Identifiers (Variables)
The identifier is a term used to represent program
elements 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 p,q,r;
The value assigned to a variable gets
stored at the specified location in the
memory. If any change in its value occurs
due to an operation, it is maintained

50 Understanding ComputegApp)icationswit-lyBlueJ—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; m int m = m*m; 25

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

Rules for naming a variable


• A variable may have any number of characters.
• 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.
• The variable names should be meaningful, which easily depicts its purpose.

Assignments
Assigning means to store constants in variables using a token '=' symbol. Here,
the symbol acts as an assignmentoperator.The data type of the variable
depends upon the type of assigned constant. You must ensure that the constant
you are going to store has the same data type as the variable declared.
Syntax: Data type <variable> = <constant>;
For example, int m = 15; : 15 is stored in the variable m which is integer type
float n = 45.24; • 45.24is 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 (str),
enclosed within double quotes (
The following table illustrates the different data types along with the
assignment of relevant constants:
Declaration of variable Assigning constant
int a;
long b; b - 2345763;
float f; f = 3.45;
double d; d 0.000000045;

char ch; ch-


String str; str= "COMPUTER";
boolean p false;

Values and Data Types 517


0
Initializing a variable
When you declare a variable, it may contain garbage values (absurdvalue)
and can create a problem during the execution of your program. You maynot
obtain an appropriate result even though your program logic is correct.Under
such circumstances, you need to initialize a variable to get the desired result.A
variable is initialized by assigning a specific value to it.
Initialization of a variable takes place in the following ways:
• Static Initialization
Dynamic Initialization
Static Initialization
This process uses direct assignment of a constant to a defined variable.The
variable is initialized at the time of its declaration (i.e., before its actual usein
the program logic). The table for the same is as shown below:

Data Type Declaration Static Initialization


Integer int a;
Float float f; f=o.ou

Double double d; d=o.o,

Character char c; Nu0000';

String String s; 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 DynamicInitialisation.Under this situation, a variableis
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) (iii) ; (semi colon)

52 Understanding Computer Applications with BlueJ—IX


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, (i)
(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
They are the special characters in Java, which are used to separate the variables
or the characters.
etc.
For example, Comma(,), Brackets ( ), Curly brackets { }, Square brackets [ l,
Comma (,) in a Java program, is used to separate multiple variables under
the same declaration.
For example, b,c;
Brackets( ) are used to enclose any arithmetical or relational expressions.
Curly brackets { } are used to enclose a group of statements under a compound
statement.
Square brackets [ ] are used to enclose subscript or the cell number of a
dimensional array.

Operators
Operators are basically the symbols or tokens that perform arithmetical or logical
operations. Basically,there are three types of operators used in Java:
(a) Arithmetical Operators: +, -, / , *, etc
(b) Relational Operators: <,>,= , etc.
(c) Logical Operators: &&, I I !, etc.

Keywords
Keywordsare the reserved words which are preserved by the system and carry
specialmeaning for the system compiler. During the course of programming you
need to use keywords to meet certain requirements.
For example, class, public, throws, for, sqrt, System, etc.

DATATYPES IN JAVA
The compiler contains a phase called the storage assignment phase. This phase
allocatesmemory for different variables used in your program. It also creates the
Structurein the location to store the data efficiently.Hence, the compiler must
know the type of data you are likely to supply for storage to ensure optimum

Values and Data Types 53 7


utilisation of memory space. This is the reason why data types are required.
Java programming. In Java programming, we need to deal with various types
of data. Hence, it becomes necessary for a programmer to select an appropriate
data type according to the data taken in a program. The data and its typesare
given below:

Primitive Types

Numeric Types Non-Numeric Types


• Integers • Characters
• Boolean
• Floating Numbers

Non-Primitive Types

Classes Arrays Interface

Primitive Types
type, are known a Primitive
The data types which are independent of any other
Types.
data types. These types are also called BasicData
For example, byte, int, long, float, double, etc.
because the system
Primitive data types are pre-defined or built-in data types
of type int,
developers of Java have defined them. You can declare a variable
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
er
A variable declared as an integer type contains a whole number. The numb
may be a positive or a negative number, but without decimal point. Thereare
four types of declarations under this heading:
• byte : Used for bit-wise operations
e short : Used for a small range of integers
• int : Used for integers which are more than short integers
• Jong : Used for large integers
A programmer has to select an appropriate data type according to the need
of the program, as shown below:
Dala Dato 'IYpe Bit Size Format
Byte byte 8 bits (l 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 BlueJ—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.
A programmer decides the data types according to the need. 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.1269 8

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
• 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 is taken into
consideration during Java programming. In Java, the declaration of 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 withinsingle 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 assigned to variable p
data type variable
Note: It must be noted that a String is always enclosed within double quotes.

Values and Data Types 557


The character types in Java are as follows:
Non-numeric Character type Bit size Format
Single character (a letter or char 16 bits (2 char p; p='N;
a special character). bytes) char x;
More than a character/a String More than 16 String str;
word/a sentence. bits str="School";

Arithmetical Expression and Statement


A set of variables, constants and arithmetical operators used together to yielda
meaningful result is known as an ArithmeticalExpression.When an arithmetical
expression is assigned to a variable then it is called an ArithmeticalStatement.
For example,
Arithmetical Expression

Arithmetical Statement
D
Type of Arithmetical Expressions:
Based on the data types, the arithmetical expression is of the following two types:
• Pure Expression • Impure Expression
Pure Expression
An arithmeticalexpressionthat uses all its components of same data types is
known as the pure expression.
For example,
int a,b;
int c = a + b * 4•
In the expression shown above, all its components like a, b and 4 are integer
type data. Hence, it is a Pure Expression.

Impure Expression:
An arithmeticalexpressionin which one or more components
are of different
data types, is referred to as an Impure Arithmetical
Expression or Mixed mode
Expression.
For example,
int a; float f; double d;
doubles = a * f / d;
Here, a, f and d are of different data types.
Hence, it is an impure expressi011

Type Conversion
In a mixed expression,the result can be
types. Hence, it is needed to convert the
obtained in any one form of its data
various data types into a single type•
Such conversion is termed as Type
Conversion.In Java, type conversion takes
place in the following two ways:

56 Understanding Computer Applications with


BlueJ—IX
Implicit type conversion
In a mixed expression, the data type of the result gets automatically converted
into the highest data type available in the expression without any intervention
of the user. This system of type conversion is known as Implicittype conversion
or Coercion.

Hierarchy of Data types

For example int a; long b; long c;


byte
char b;
sho
int
int long
long
float2
doubl
(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;
d

float double
Int
char double

double

double

The result obtained in the illustration shown above is of the type double. Hence,
the resulting variable d must be a double data type.
It is further to be noted that Java Language is very powerful on data type
operation.So, the user must be very careful of using suitable data types while
Writingany Java expression.

Valuesand Data Types 57 Y


The data types and
Suppose, the user wants to calculate area of a trapezium.
the formula to calculate area are shown below:
float a, b, h, area;
area = 1/2 * (a + b)*h;
the area will alwaysresult
At the time of execution, you will get confused, as
a, b and h. Thisis
in zero for whatever may be the values of the variables
integer type (i.e.,0 not
because the result of the division (1/2) is implicitly an
values, it will yieldthe
0.5).When this value is multiplied with the other data
final result as 0 (zero).
below to get the desired result:
The above expression can be rectified as shown
area = 1.0/2.0 * (a + b) * h;

Explicit Type Conversion


which the data
Explicit type conversion is another way of type conversion in
user's choice.This
type gets converted to another type depending upon the
data
means that the user demands the system to get the result in the desired
type.
When the data type gets converted to another data type after the user's
intervention, the type conversion is known as explicittype conversion.
For example, int a,b;
(float) (a+b);
In the example given above, the outcome of the expression (a+b) has to be int
implicitly. But, (float) provided by the User before the expression (a+b) will cause
the result to be forcibly converted to float type. Hence, this type of conversion
is also called type casting.
Type casting is also applicable in converting the data type from a higher type
to a lower type.
For example, double x,y;
int c = (int) (X+Y);
The expression shown above will result in double type implicitly, but due
to implication of type casting the result is forced to get converted into a lower
type int.

Explicit Conversion of Literals


A variable declared either as float or double type contains a fractional value'
At the time of initialization it becomes difficult to say whether a real literal
(fractionalvalue) to be stored into the variable is float type or double type•
For example,
float f-0.214;
double d= 0.214;

58 Understanding Computer Applications with BlueJ—IX


I
The example shown above, initializes the same value 0.214to the floating as
well as double type variables. How will you decide whether the value 0.214is
a floating constant or a double type constant?
To sort out the problem, explicit conversion of literals is allowed in Java
programming. The literal to be initialized into the floating type variable and
double type variable, must be suffixed with 'F' and 'D' respectively.
For example,
float f=O.214E;
double d=0.214D;
Now, it is clear that the value 0.214Fis a floating literal assigned to the variable
f and 0.214Dis a double type literal assigned to variable d.
A similar problem may arise during the initialization of integer and long type
variables.A whole number using a suffix 'L' is referred to as a long type literal
whereas a whole number without any suffix is an integer type literal.
For example,
int a=12;
long b = 12L;

Quick Recap
Justify with reason whether the following assignments are true or false:
(i) int n =15.4;
Ans. False, the variable n must be assigned an integer value.
(ii) float f =12.02;
Ans. True,the variable f is assigned a fractional value.
(iii) char ch = 'A';
Ans. True,the variable ch contains a letter enclosed within single quotes.
(iv) String str = "Mouse";
Ans. True, the String constant "Mouse", assigned to variable str, is enclosed within
double quotes.
(v) String p = true;
Ans. False,the variable p should not be assigneda boolean constant.
(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.

Values and Data Types 597


Practice Session
Mention the type of values obtained when the following expressions are executed,
Expressions Data type
int x; float y; char c;

char c; int a; double d;


c-d/a;

double d; int a, long p;


(d/a)*4/p;

float f; char c; short s;


(int) f+C/s;
int a; float f; double d; char c;
(float) (a/f)-d*c;

Primitive Data Types with their sizes and ranges at a glance:


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 (8 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+308 up to 15 significant digits

60 Understanding Computer Applications with BlueJ—IX


CO REVIEW INSIGHT
(a) What do you mean by type conversion?How is implicit conversion different
from explicit conversion? [ICSE 2010]
Ans. In a mixed expression,the result must be obtainedin a specificdata type. For
this purpose, the data type needs to be convertedinto the requiredtype. 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;

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);
type.
(b) Give one example each of a primitivedata type and a composite data
[ICSE 2012]

(any one)
Ans. Example of primitive data type: int, float, double, long, char, etc.
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; [ICSE 2012]
char ch=(char)n;
Ans. The value of
(0 n = 66
(it) ch = B
[ICSE 2013]
(d) What is a literal?
discussionof a program.
Ans. Literalis a constant that remains fixed throughout the variable,
A literal is a value that can be assignedto a specific
For example,
Integer literals: 12, 4, 342, etc.
Real literals: 1.02, 345.657, 0.00002,1.0+05, etc.
Character literals: A, p , etc.
etc.
string literals: "COMPUTERS", "PAY2013","3421",
Boolean literals: true or false
examples?
(e) What are the types of casting shown in the following
doublex = 15.2;
int a = 12;
(O int y = (int) x;
(ii) long b = a; [ICSE 20131
Ans. (O Explicit type casting
(ii) Implicit type casting
(f) Name the primitive data type in Java that is:
(i) A 64-bit integer and is used when you need a range of values wider than
those provided by int,
(ii) A single JG-bitUnicode character whose default value is '\u0000'
[ICSE 2014]

Values and Dat;yuypes 61 S'


Ans. The primitive (Into type Cot'Ihc following orc:
(i) long
(ii) char
(g) State one difference between the Cloutingpoint lilcrol nnd doublc type literal
[ICSE 2014')
Ans. Floating point literal is n fractional number with short prccigion and the
can range from -3./lE+38103.dE+38,whereas double type literal ig used for long
precision and the values can range from to
(h) What are the default values of the primitive data typc int and float?
[ICSE 20151
Ans. The default value of int is 0 and float is 0.0b'
(i) What are identifiers? [ICSE20151
Ans. Identifiers are the variables that are named memory locations to containthe
values. The values may change depending upon circumstances or problems,
(j) Identify the literals listed below:
(D 0.5
(iii) false (iv) "a" [ICSE 20151
Ans. (t) Real (ii) Char
(iil) boolean (iv) String
(k) Arrange the followingprimitive data types in an ascending order of their size:
(t) char (ii) byte
(iii) double (iv) int [ICSE 20151
Ans. byte, char, int, double
(l) What are the types of casting shown by the following examples?
[ICSE 20161
(1) char c = (char) 120; (ii) int x
Ans. (t) Explicit (ii) Implicit
(m) Write one difference between primitive data types and composite data types.
[ICSE 20161
Ans. The fundamental or basic data types (viz. int, float, char) that are provided
with some built-in characteristics, are known as primitive data types. Whereas,
derived data types or non-primitive data types are called as composite data
types. For example, class, array.
(n) Differentiate between float data type and double data type initializations.
[ICSE MODELI
Ans. A float type variable is initialized with a fractional value such as ().()Fwhereasa
double type variable is initialized with a fractional value as 0.0D.The differenCe
between initializations is the suffix L and D respectively.
For example, float
double d=0.0D;
(o) What do you mean by type conversion? IICSE MODELI
Ans. Jn an impure expression, where data ol' various types is included, the result needs
to be obtained in o specific (Iota type. The data type in which the result is to be
obtained converted ONper the suitability of the system or the requirementOf
the ugcr.Thiß convcrNioni" referred 10 os type conversion.

62
EXERCISE
I. Multiple Choice Questions
Tick (V) the correct answer:
l. A constant which gives the exact representationof 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)
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 followingresults in integer type?
(a) 11.4F/3.2D (b) 13.8F/4.6F; (c) 12/3 (d)none
8. Which of the following is non-primitivedata?
(a) char (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 conditioni.e. true or false. Which of
the followingis a correct representation?
(a) boolean m=true (b) boolean m='true'
(c) boolean m="true" (d) none

Il. Fill in the blanks:


I. 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 represent a character.
4. Each individual component of a Java statement is known as
5. In Java, the constants are also called
6. operator is used to store a value in the variable.
7. The comma, exclamation, question mark etc., are termed as in
Java Janguage.
8. An element of Java program that is used to identify a class, function or value
is called as
9. Integer type value occupies bytes in the memory,
10.A Java expression,that contains all the elements of same data type is
expression,

Values and Data C


IYpes 63
Ill. Write short answers:
l. What do you mean by data type?
2. Define variable with an example. [ICSE200
3. What do you mean by constant? Explain with an example.
4. State two kinds of data types. [ICSE2006]
5. What do you understand by Token? Name different types of tokens.
6. What are the rules to assign a variable in a Java programming?
7. Explain the term 'type casting'? [ICSE2007]
8. Perform the following:
(a) Assign the value of pie (3.142857......)to a variable with the requisitedata
type. [ICSE 2007]
(b) Assign the value of NS(1.732)to a variable with the requisite data type.
9. Distinguish between:
(a) Integer and floating constant
(b) Token and Identifier [ICSE 2008]
(c) Character and String constant
(d) Character and Boolean literal [ICSE2009]
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 necessaryto define data types in Java programming?
14. Define the followingwith 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
from explicit conversion? [ICSE 2010]
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:
(1)int p; double q; (it) float m;
r = P+q; p = m/3*(Math.pow(4,3));
System.out.println(r); System.out.println(p);
20. What are the resultant data types, if the following
performed? Show the result with flow lines.
implicit conversionsare
int i; float f; double d; char c; byte b;
(a) i + c/b;
(b) f/d + c*f;
(d) (f/i)*c + s;
(e) i + c + b/d;
(D i/c + fib;

•o
" 64 Understanding Computer
Applications with BlueJ—1X

You might also like