CH 3 Values and Data Types
CH 3 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.
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
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
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.
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
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
Data value 5 is stored in location The value of the variable'm' changes in the
named 'm' memory after the execution
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;
String String s; s=
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;
Punctuators
Punctuators are the punctuation signs used as special characters in Java•
some
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
Primitive Types
Non-Primitive Types
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
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
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:
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.
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.
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.
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]
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
•o
" 64 Understanding Computer
Applications with BlueJ—1X