5-Data Tpyes, Operators&Expression
5-Data Tpyes, Operators&Expression
Presented
by
Dr.C.Rani
Assistant Professor
Department of Computer Science and Engineering
Government College of Engineering, Salem-11
1
Objectives and Outcomes
Session Objective
Session Outcomes
Java Program
2
OUTLINE
Data Types
Type Casting
Types of Type Casting
Introduction to Operators
Types of Operators
Arithmetic Opertors
Integer Arithmetic
Real Arithmetic
Mixed-mode arithmetic
Relational Operators
Logical Operators
Assignment Operators
Increment and Decrement Operators
Conditional Operator
Bitwise Operators
Special Operators
instanceof Operator
Dot Operator
Arithmetic Expressions
Evaluation of Expressions
3 Precedence of Arithmetic Operators
4
5
6
7
8
9
10
11
12
13
14
15
16
SYMBOLIC CONSTANTS
Constants may appear repeatedly in number of places in
the program.
Constant values are assigned to some names at the
beginning of the program, then the subsequent use of
these names in the program has the effect of caving
their defined values to be automatically substituted in
appropriate points.
The constant is declared as follows:
Syntax : final type symbolicname= value;
Eg final float PI =3.14159;
final int STRENGTH =100;
17
Rules :-
1.Symbolic names take the some form as variable names. But they one written in capitals
to distance from variable names. This is only convention not a rule.
2.After declaration of symbolic constants they shouldn’t be assigned any other value with
in the program by using an assignment statement.
3.Symbolic constants one declared for types there are not done in c & c++ where
symbolic constants one defined using the define statement.
4.They can’t be declared inside a method . they should be used only as class data members
in the beginning of the class.
18
TYPE CASTING
Encounter situations where there is a need to store a value
of one type into a variable of another type.
We must cast the value to be stored by proceeding it with
the type name in paraenthesis.
Definition:
The process of converting one data type to another is called casting.
Syntax:
type variable1= (type) variable2;
Examples:
int m=50;
byte n- (byte )m;
long count=(long)m;
19
TYPE CASTING
20
21
22
23
24
25
26
27
28
29
OPERATORS AND EXPRESSIONS
30
Outline
Introduction
Arithmetic Operators
Integer Arithmetic
Real Arithmetic
Mixed-mode arithmetic
Relational Operators
Logical Operators
Assignment Operators
Increment and Decrement Operators
Conditional Operator
Bitwise Operators
Special Operators
instanceof Operator
Dot Operator
Arithmetic Expressions
Evaluation of Expressions
Precedence of Arithmetic Operators
31
32
33
34
Unary Operators
perat Description
or
+ Unary plus operator; indicates positive value (numbers are positive without this,
however)
- Unary minus operator; negates an expression
++ Increment operator; increments a value by 1
-- Decrement operator; decrements a value by 1
! Logical complement operator; inverts the value of a boolean
~ 1's Complement operator
35
36
37
38
39
40
41
42
43
44
45
46
47
Precedence of Arithmetic operators
48
49
50
Arithmetic operators-Types
Integer Arithmetic
Real Arithmetic
Mixed mode Arithmetic
51
Integer Arithmetic
When an arithmetic operation is performed on two
whole numbers or integers than such an operation is
called as integer arithmetic. It always gives an integer as
the result.
Let x = 27 and y = 5 be 2 integer numbers.
Then the integer operation leads to the following results :
52
Real/ Floating point Arithmetic
• When an arithmetic operation is preformed on two real numbers
or fraction numbers such an operation is called floating point
arithmetic.
• The floating point results can be truncated according to the
properties requirement.
• The remainder operator is not applicable for floating point
arithmetic operands.
• Let x = 14.0 and y = 4.0 then
53
Mixed mode Arithmetic
• When one of the operand is real and other is an integer and if the
arithmetic operation is carried out on these 2 operands then it is
called as mixed mode arithmetic.
• If any one operand is of real type then the result will always be
real thus 15/10.0 = 1.5.
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
Conditional / ternaryOperators
Conditional operator--> (?:)
the only ternary operator available in Java which operates on three operands.
The conditional operator together with operands form a conditional expression.
Syntax:
expr1? expr2:expr3
71
72
73
instanceof Operator
The instanceof operator is an object reference operator which is used
to check the type of an object reference.
The instanceof operator requires an object or array value as its left
operand and name of a reference type as its right operand.
This operator evaluates to true if the object or array on its left-hand
side is an instance of the specified type; otherwise, it returns false.
74
Dot Operator
The dot operator (.) is used to access the fields and methods of an
object.
For example: If s is an object of class Student then to access the
field rollNumber of the object s, we use the dot operator as
follows,
s.rollNumber
Similarly, if you want to access getData () method of s object then
we use the dot operator as follows,
s.getData();
75
new Operator
In Java, the new operator is used to create an object of a class or an
array.
It is a unary prefix operator.
To create an object, use the new operator with the name of the class
whose object you want to create, followed by parentheses.
The parentheses can be empty or can contain arguments,
Example
Student s = new Student();
creates an object s of class Student.
76
Precedence and Associativity of Operators
77
78