Computer Applications
Grade IX
Chapter 3
Values and Data Types
Character Sets in Java
Character
sets in Java
Letters Digits Operators Delimiters
Character Sets
• Letters: All letters of English alphabet(A-Z) and (a-z)
• Digits: 0-9
• Operators:
• Arithmetical(+,-,*,/,%)
• Logical(&&,||,!)
• Relational(<,<=,>,>=,==,!=)
• Delimiters: Special characters ; : ? . ( ) { } [ ]
UNICODE
• Unicode is a standard encoding system created by
Unicode consortium that is used to encode a character in
any computer language.
• Unicode is wide representation of characters in numeric
form.
• Contains hexadecimal digits ranging from 0x0000 to
0xFFFF(i.e. 16bits code)
• It can address characters from 139 modern and historic
scripts.
Advantages of using UNICODE
• It is Universal coding scheme throughout the world
• More efficient than other
• Supports uniform coding width for all characters(16-bits)
ASCII characters and codes
• ASCII stands for “American Standard Code for Information
Interchange”
• Each character is assigned a ASCII code
• ASCII codes are decimal numbers represented in 7 binary
bits.
• Range of code is not as large as UNICODE
• Available only for limited number of characters ranging
from 0 to 127
• ASCII codes are basically used while programming when it
deals with characters.
Some ASCII codes of common characters:
A – 65 a – 97
B – 66 b – 98
C – 67 c – 99
and so on till z – 122
And so on till Z – 90
58-64 are different symbols such as <, >, ; etc
91-96 also contain special symbols viz [, ], \, ^, etc
ASCII code of white space(blank) is 32
Difference between UNICODE and ASCII Code
• Generalised form of • Specific coding
ASCII
UNICODE
coding scheme for scheme for limited
numerous characters
characters of • Represents limited
different scripts range of codes
• Represents higher
range of codes
Escape Sequences
• Non-graphic characters – used as command to direct the cursor
while printing
• Escape sequence character begins with a backslash(\) and it is
followed by one or more characters
Escape Sequences Non-graphic characters
\t Horizontal tab
\v Vertical tab
\\ Backslash
\’ Single quote
\” Double quote
\b Backspace
\f Form feed
\0 Null
\r Carriage return
\n New line feed
Tokens
• A token is functional and fundamental unit of computer
program
• Each program statement is composed of various
components known as a ‘Token’.
• Various tokens in java are:
• Literals
• Assignments
• Separators
• Keywords
• Identifiers
• Punctuators
• Operators
Literals(Constants)
• Literals are the constants i.e. the fixed values in Java
program.
• Java literals can be classified as:
• Integer Literals e.g. 14, 345, -18, -391
• Real Literals e.g. 24.6, 0.0072, -3.652
• Character Literals e.g. ‘A’, ‘d’, ‘3’, ‘*’
• String Literals e.g. “COMPUTER”, “Year 2020”, “10% per annum”
• Boolean Literals only two possible values– true or false(never
enclosed in quotes)
• E.g. boolean ans = true;
• Null Literal: special purpose literal. Represented as ‘\0’. To mark
the end of the string.
Identifiers(Variables)
• Term used to represent program elements such as function
name or class name.
• Variable names are also called as identifiers.
• Variable is named memory location that contains a value.
• Value can change
Before Execution After Execution
int m = 5; m 5 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
• Syntax: <data type> <space><variable name>
e.g int m;
float p,q,r;
Rules for naming a variable
• Java keywords cannot be used as a variable name.
• A variable name must start with a letter. It can also start
with an underscore character (_) or dollar($) sign but
should be avoided.
• A variable name cannot start with a digit.
• A variable name can only contain alpha-numeric
characters, underscore and dollar sign.
(a-z, A-Z , 0-9 , _ and $)
Assignments
• Assigning means to store constants in variables using a
token ‘=‘
• Syntax: Datatype <variable> = <constant>;
Declaration of variable Assigning constant
int a; a = 5;
long b; b = 2345763;
float f; f = 3.45;
double d; d = 0.0000045;
char ch; ch = ‘k’;
String str; str = “COMPUTER”;
boolean p; p = false;
Initializing a variable
• Uninitialized variable contains garbage value(absurd value)
• Initialization of a variable may take place in two different
ways:
• Static initialization
Uses direct assignment of a constant to a defined variable
e.g. int a = 0;
float f = 0.0;
• Dynamic initialization
• When variable gets initialised at run time i.e. during execution of the program
• e.g. int a, b, c; c = a + b;
int a = 49; double d; d = Math.sqrt(a);
Punctuators
• Punctuation signs used as special characters
• Question mark(?): represents the action to be taken when the
given condition is true while using ternary operator
• E.g. int max = (a>b)? a : b;
• Dot(.): used to represent scope of a function
• System.out.println();
• Semi colon(;) is used as a statement terminator
Separators
• Special characters used to separate variables in Java.
• Comma(,) --- to separate multiple variables in declaration
• E.g. int a,b,c;
• Brackets() are used to enclose any arithmetical or
relational expressions
• Curly brackets{} are used to enclose a group of
statements
• Square brackets [] are used to enclose subscript or cell
number of a dimensional array
Operators
• Operators are basically symbols or tokens to perform
arithmetic or logical operations.
• Types of operators:
• Arithmetical(+,-,*,/,%)
• Logical(&&,||,!)
• Relational(<,<=,>,>=,==,!=)
Keywords
• Keywords are reserved words which are preserved by the
system and carry special meaning for the system compiler.
• E.g. class, public, void, etc
Data types in Java
Data types
Primitive Non-Primitive
1. Independent 1. Dependent on basic datatypes
2. Pre-defined or built-in 2. User defined
E.g.: int, float, long, double, E.g.: Classes, Arrays, interface
char
Primitive data types
Primitive
Numeric Non-Numeric
Integers Characters
Floating numbers Booleans
Integer type
• A variable of integer type can store a whole number.
• Either positive or negative, but without a decimal point.
• It has four types as shown below:
Data Data type Bit Size Format
Byte byte 8 bits(1 byte) byte b = 5;
Short short 16 bits(2 bytes) short a = 12;
Integer int 32 bits(4 bytes) int c = 214;
Long Integer long 64 bits(8 bytes) long d = 45687;
Floating point
• To store a fractional number i.e. a number with decimal
points.
• It has two types as shown below:
Data Data type Bit Size Format
Small range of float 32 bits(4 bytes) float m = 32.14;
decimal values
Wide range of double 64 bits(8 bytes) double n = 12.1269387
decimal values
Characters
• A character type variable contains a single character.
• The declaration of a character can be given as:
• Syntax: <data-type> <variable> = <‘character literal’>;
• For e.g.: char ch = ‘a’;
• A character literal is always enclosed within single quotes.
• Similarly, a set of characters(String) can be declared as,
• Syntax: <data-type> <variable> = <“String constant”>;
• For e.g.: String p = “Computer Applications”;
• A string is always enclosed within double quotes.
Boolean
• Has only two values true or false
For example: boolean ans = true;
Primitive data types, their sizes and ranges
Type Conversion
• In a mixed expression the result can be obtained in any
one form of its datatypes.
• To convert various data types into a single type is known
as type conversion
• There are two types of conversion:
• Implicit
• Explicit
Implicit type conversion
• In a mixed expression, the data type of the result gets
automatically converted into the higher most type
available in the expression
byte
char
short
int
long
float
double
(hierarchy of data types)
Explicit type conversion
• In this type the data gets converted to another type
depending upon the user’s choice.
• i.e. the user demands the system to get the result in
desired data type.
• E.g. float x = (float) (a + b);
Higher to lower type:
e.g. double x, y;
int c = (int)(x + y);