Professional Documents
Culture Documents
CT038-3-2 OODJ
Introduction
Constants
Variables
Data Types
Operators
Expressions
Learning outcomes
At the end of this lecture you should be
able to:
Identify data types, operators and
expressions.
CT038-3-2 OODJ
CT038-3-2 OODJ
Introduction
A programming language is designed to
process certain kind of data consisting of
numbers, characters and strings and to
provide useful output as information.
Every program instruction must conform
precisely to the syntax rules of the
language.
CT038-3-2 OODJ
Constants
Constants in Java refer to fixed values that
do not change during the execution of a
program.
In Java constant are identified with the
keyword final.
CT038-3-2 OODJ
Syntax - Constant
In a method:
final typeName variableName = expression;
In a class:
accessSpecifier static final typeName
variableName = expression;
Example:
final double NICKEL_VALUE = 0.05;
public static final double
LITRES_PER_GALLON = 3.785;
CT038-3-2 OODJ
Variables
A variable may take different values at
different times during the execution of the
program
CT038-3-2 OODJ
Syntax - Variable
typeName variable Name = value;
Or
typeName variable Name;
Example
String greeting = Hello Java;
CT038-3-2 OODJ
Data Types
Every variable in Java has a data type.
Data types specify the size and type of
values that can be stored.
CT038-3-2 OODJ
Non Primitive
Primitive
Numeric
Integer
CT038-3-2 OODJ
Classes
Non-numeric
Floating point
Character
Boolean
Arrays
Interface
Integer Type
Integer types can hold whole numbers.
Java supports signed values, meaning
they can be both positive and negative.
CT038-3-2 OODJ
Size
byte
short Two
bytes
int
Four
bytes
long Eight
bytes
CT038-3-2 OODJ
Min value
Max value
127
-32 786
32 767
CT038-3-2 OODJ
Double
Float
CT038-3-2 OODJ
Size
Min Value
Max Value
float
4 bytes
3.4e-0.38
3.4e+038
double
8 bytes
1.7e-0.38
1.7e+038
CT038-3-2 OODJ
Character Type
The character type, representing code
units in the Unicode encoding scheme.
The char type assumes a size of 2 bytes.
It can hold only a single character.
Example
char c = X ;
CT038-3-2 OODJ
Boolean Type
Boolean type is used when we want to test
a particular condition during the execution
of the program.
There are only two values that a boolean
type can take: true or false
Both these words are Java keywords.
boolean type uses one bit of storage.
CT038-3-2 OODJ
Operators
An operator is a symbol that tells the
program to perform certain mathematical
or logical manipulations.
CT038-3-2 OODJ
CT038-3-2 OODJ
Arithmetic operators
Relational operators
Logical operators
Bitwise operators
Conditional operators
Increment and decrement operators.
Expressions
Expressions are evaluated using an
assignment statement in the form
variable = expression;
When the statement is encountered, the
expression is evaluated first and the result
then replaces the previous value of the
variable on the left side.
CT038-3-2 OODJ
Example :
x = a*b-c
The variables a,b and c must be declared
before they are used in the program.
CT038-3-2 OODJ
CT038-3-2 OODJ
Casting a Value
Example
Consider the calculation of ratio of girls to
boys in this class.
ratio = girls_number / boys_number
CT038-3-2 OODJ
CT038-3-2 OODJ
Use of Casts
Examples
Action
x = (int)7.5
Evaluated as 21 / 4
b = (double) sum/n
y = (int)(a+b)
z = (int)a + b
CT038-3-2 OODJ
Summary
Java has eight primitive types, including
four integer types and two floating point
types.
A final variable is a constant. Once the
value is set, it cannot be changed.
You can use cast type to convert a value
to different type.
CT038-3-2 OODJ