Unit 03.
Values and data types
------------------------------------------------------------------------------------------------------------------------
Scope of Syllabus
Character set, ASCII code, Unicode, Escape sequences, Tokens, Constants and Variables, Data types,
type conversions.
Escape sequences [\n, \t, \\, \”, \’], Tokens and its types [keywords, identifiers, literals, punctuators,
operators], primitive types and non-primitive types with examples, Introduce the primitive types with
size in bits and bytes, Implicit type conversion and Explicit type conversion.
------------------------------------------------------------------------------------------------------------------------
Character Set
A character set is a set of valid characters that a language can recognise. The most used character set
is ASCII (American Standard Code for Information Interchange), which includes the alphabet, digits,
punctuation marks and control characters. Another widely used character set is Unicode, which aims
to create characters for many scripts worldwide.
Java uses the Unicode character set, which is a 2 bytes (16 bits) character coding system and supports
more than 34,000 defined characters derived from 24 languages from America, Europe, Middle East,
Africa and Asia. The first 128 characters in the Unicode character set are identical to the common
ASCII character set. [Note: ASCII/Unicode for ‘A’ is 65 and ‘a’ is 97]
Escape Sequences
Java supports some special characters that are used in output methods. For example, the symbol ‘\n’
stands for newline character. A list of such special characters is given in Table 3.1. Note that each
one of them represents one character, although they consist of two characters. These special
characters are known as escape sequences.
Table 3.1 Escape Sequences (Special Characters)
Escape Sequence Meaning
\n new line
\t horizontal tab
\\ backslash
\” double quote
\’ single quote
Tokens
Smallest individual units in a program are known as tokens. The compiler recognizes them for
building up statements. In simple terms, Java program is a collection of tokens.
There are several types of tokens in Java, including: Keywords, Identifiers, Literals, Operators and
Separators.
Keywords
These are reserved words that have a specific meaning in the Java language. These keywords,
combined with the syntax of the operators and separators, form the foundation of the Java language.
In general, keywords cannot be used as identifiers, meaning that they cannot be used as names for a
variable, class, or method. Some of the keywords are given in Table 3.2.
Table 3.2 Some of the Java keywords
boolean break byte case char
class continue default do double
else extends final float for
if import int long new
package private protected public return
short static switch void while
In addition to the keywords, Java reserves three other names that have been part of Java from the
start: true, false and null. These are values defined by Java. You may not use these words for the
names of classes, methods, variables and so on.
Identifiers
Identifiers are names given to classes, methods, variables and other program elements. An identifier
may be any descriptive sequence of uppercase and lowercase letters, numbers, or the underscore and
dollar-sign characters. They must not begin with a number. Identifiers are case-sensitive.
Literals
These represent fixed values in the code. A constant value in Java is created by using a literal
representation of it. A literal can be used anywhere a value of its type is allowed. Java supports various
types of literals, including integer literals, floating-point literals, character literals, string literals,
boolean literals and null literals.
Operators
These rare symbols used to perform operations on variables and values. Examples of operators in
Java includes arithmetic operators(+. -, *, /. %), assignment operator (=) and many more.
Separators
These are symbols (also known as punctuators) used to separate tokens or indicate the beginning or
end of a program element. The most used separator in Java is the semicolon, which is often used to
terminate statements. Other common separators in Java include parentheses (( and )), braces ({ and
}), brackets ([ and ]), commas (,), periods (.), colons (:), and more.
Data types
In computer programming, data types are used to define the kind of information / data that can be
stored and manipulated by a computer. They help computer understand and handle different types of
data appropriately.
Java has two main categories of data types: primitive data types and reference data types.
Primitive Data Types
These are the basic data types built into the Java language. They are used to store simple values. Java
has eight primitive data types: byte, short, int, long, boolean, char, float and double. These can be
put in four groups:
Integers – This group includes byte, short, int and long, which are for whole-numbers.
Floating-point numbers – This group includes float and double, which represent numbers with
fractional precision.
Characters – This group includes char, which represent symbols in a character set, like letters and
numbers.
Boolean – This group includes boolean, which is a special type for representing true/false values.
Table 3.3 Primitive Data types
Name Size Range of Values Default
Value
byte 1 Byte – 128 to + 127 0
short 2 Bytes – 32,768 to + 32,768 0
int 4 Bytes – 2,147,483,648 to + 2,147,483,647 0
long 8 Bytes – 9,223,372,036,854,775,808L to + 9,223, 372,036,854,775,807L 0L
boolean 1 Byte It can have only one of two possible values, true or false. false
(but uses
only 1 bit)
char 2 Bytes It can have single character enclosed within single quotes such as ‘\u000’
‘A’, ‘z’, or ‘$’ etc.
float 4 Bytes It can have precision of about 7 decimal places. 0.0F
double 8 Bytes It can have precision of about 15 decimal places. 0.0
The most used integer type is int. When byte and short values are used in an expression, they are
promoted to int when the expression is evaluated. Therefore, int is often the best choice when an
integer is needed.
Reference Data Types
These data types hold a reference (memory address) to the actual data stored in memory, rather than
storing the data directly. Examples for reference data types include classes, arrays, and interfaces.
Variables
Variables are used to store values that can be manipulated and accessed within a program. Before
using a variable, we need to declare it with a specific data type. Here are some important aspects of
variables in Java:
1. Declaration: Before using a variable, we need to declare it with a specific data type. For
example:
int num;
2. Assignment: Once a variable is declared, we can assign a value to a variable. We can also
change or update the value of variable throughout the program. For example:
num = 9;
3. Initialization: Variables can be initialised at the time of declaration, which means assigning
an initial value to the variable. For example:
int num = 9;
4. Constants: If we want a variable to hold a value that should not be changed, we can declare it
as a constant using the final keyword. Once a constant is initialised, it cannot be modified.
Constants are typically named using uppercase letters. For example:
final double PI = 3.14159;
Type Conversion
Type conversion is the process of converting a value from one data type to another. It allows us to
change the representation of data to make it compatible with operations or assignments that expect a
different type.
It is common to assign a value of one type to a variable of another type. If the two types are
compatible, then Java will perform the conversion automatically. For example, it is always possible
to assign an int value to a long/double variable. However, not all types are compatible and thus, not
all type conversions are implicitly allowed. For instance, there is no automatic conversion defined
from double to int. Fortunately, it is still possible to obtain a conversion between incompatible types.
To do so, we must use a cast, which performs an explicit conversion between incompatible types.
In Java, there are two types of type conversions: implicit (automatic) and explicit (manual).
Implicit Type Conversion
Implicit type conversion, also known as automatic type conversion or widening conversion, occurs
when the compiler automatically converts a value from smaller data type to a larger data type. This
conversion is done when no data is lost, or precision is compromised during the conversion. For
example, converting an integer value to a floating-point value.
Example:
int num1 = 9;
double num2 = num1;
Explanation:
In the example above, we have an int variable named num1 with a value of 9. We then
declare a double variable named num2 and assign num1 to it. This is an example of implicit
type conversion because we are automatically converting an int to a double.
Since double is a larger data type than int, no data loss or precision compromise occurs
during the conversion. The Java compiler automatically performs the conversion, allowing
the int value to be stored in the double variable.
Note:
When one type of data is assigned to another type of variable, an automatic / implicit type conversion
will take place if the following conditions are met:
• The two types are compatible.
• The destination type is larger than the source type.
When these two conditions are met, a widening conversion takes place.
Explicit Type Conversion
Explicit conversion, also known as type casting or narrowing conversion, involves manually
converting a value from a larger data type to a smaller data type. This conversion may result in loss
of data or precision, and it requires the use of casting operators to indicate the desired data type. For
example, converting a floating-point value to an integer value.
It’s important to note that explicit type conversion can lead to data loss or unexpected results if the
value being converted exceeds the range or precision of the target data type. Therefore, it is crucial
to handle type conversions carefully and consider potential risks.
Example:
double num1 = 9.5;
int num2 = (int) num1;
Explanation:
In the example above, we have a double variable named num1 with a value of 9.5. We then
declare an int variable named num2 and assign num1 to it after performing explicit type
conversion.
To explicitly convert num1 from a double to an int, we use the casting operator (int) before
the variable num1. This tells the compiler to convert the double value to an int value, which
may result in truncation of the decimal part. The value of num1 remains as 9.5 (a double),
while the value of num2 is explicitly converted to 9 (an int). The explicit conversion allows
us to store the truncated double value in the int variable.
Note:
Although the automatic type conversions are helpful, they will not fulfil all needs. To create a
conversion between two incompatible types, we must use a cast. A cast is simply an explicit type
conversion. It has this general form:
(target-type) value
Here, target-type specifies the desired type to convert the specified value to.
===========================