003 - Values and Data Types
003 - 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.
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.
(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
a
b
Operator Operator
Assignment
Literal Punctuator
ldentifier
ldentifier
Identifier
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
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:
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,
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
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
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.
= C
float double
nt
cha
double
double
double
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.
a glance:
Primitive Data Types with their sizes and ranges at
Data Type Size Range Description