Download presentation
Presentation is loading. Please wait.
Published byΖωτικός Δυοβουνιώτης Modified over 6 years ago
1
Lecture 5 from (Chapter 4, pages 73 to 96)
Operators Lecture 5 from (Chapter 4, pages 73 to 96) 2018/11/14
2
Review An introduction to Object-oriented programming
Arithmetic Operators Bitwise operators Relational Operators Boolean Logical Operator Assignment Operator 2018/11/14
3
An introduction to Object Oriented Programming - http://java. sun
Object-oriented Programming is the core of Java. All java programms are object-oriented. We need to understand what an object is, what a class is, how objects and classes are related, and how objects communicate by using messages. 2018/11/14
4
An overview of Object-oriented programming (1)
An object: An object is a software bundle of related variables and methods. Software objects are often used to model real-world objects you find in everyday life. Messages: Software objects interact and communicate with each other using messages. Class: A class is a prototype that defines the variables and the methods common to all objects of a certain kind. 2018/11/14
5
An overview of Object-oriented programming (2)
An inherientence: A class inherits state and behavior from its superclass. Inheritance provides a powerful and natural mechanism for organizing and structuring software programs. An Interface: An interface is a contract in the form of a collection of method and constant declarations. When a class implements an interface, it promises to implement all of the methods declared in that interface. 2018/11/14
6
An object - We will introduce them one by one per week to build up your understanding
There are many examples of real-world objects. For example, our book, cat, pet etc. These real-world objects share two characteristics, namely, : state and behavior. For example, cats have state (color, hungry) and behavior (running , easting, sleeping). Software objects are modeled following the real-world objects in that they too have state and behavior. A software object maintains its state in one or more variables . A variable is an item of data named by an identifier. A software object implements its behavior with methods . A method is a function (subroutine) associated with an object. 2018/11/14
7
Common visual software object
Everything that the software object knows (state) and can do (behavior) is expressed by the variables and the methods. For example, for a bicycle, would have variables that indicated the bicycle's current state: its speed is 7 km/hour, 2018/11/14
8
Arithmetic Operator – page 74
Operator Description + addition - Subtraction * Multiplication / Division % Modules ++ Increment Decrement 2018/11/14
9
Example – addition and subtraction
result 2018/11/14
10
Example – multiplication and division
result 0: defined as integer result 2018/11/14
11
Special operator Expression Equivalent expression a = a + 2; a +=2;
2018/11/14
12
Example of special operator
2018/11/14
13
Increment and Decrement
Expression Equivalent expression a = a + 1; ++a; a = a – 1; --a; result 2018/11/14
14
Bitwise Operators – page 81
Operator Description ~ Bitwise unary NOT & Bitwise AND | Bitwise OR ^ Bitwise exclusive OR >> Shift Right << Shift LEFT >>> Shift Right with zero fill 2018/11/14
15
Operation - examples Operator Expression AND 1 & 1 = 1; 1& 0 = 0
~ 0 =~1; 1 =~0; ^ 0^ 0 = 0; 1^1 = 0; 1^0 =1; 0^1 = 1 >> x0010 = 0x >>1 << 0x0001 = 0x <<1 >>> 0x1001 = 0x >>>1 2018/11/14
16
AND Example – bits, 0x (hexadecimal)
(0xf2) (0xfe) (and) & (0xf2) byte c = 0xf2; byte d = 0xfe; byte e = c & d; //e is 0xf2 2018/11/14
17
OR Example (0xf2) (0xfe) (or) | (0xfe) Example byte c = 0xf2; byte d = 0xfe; byte e = c | d; //e is 0xfe 2018/11/14
18
One’s complement 1111 0010 (0xf2) -------------- ~ 0000 1101 (0x0d)
Example byte c = 0xf2; byte e = ~c; //e is 0x0d 2018/11/14
19
EXCLUSIVE OR (0xf2) (0xfe) (^) (0x0c) Example byte c = 0xf2; byte d = 0xfe; byte e = c ^ d; //e is 0x0c 2018/11/14
20
Example result 2018/11/14
21
Relational Operators Operator Description == Equal to != Not equal to
> Greater than < Less than >= Grater than or equal to <= Less than or equal to 2018/11/14
22
Example int a = 3; int b = 4; boolean c = (a>b); //c is false
boolean d = (a<b); //c is true boolean e = (a==b); //c is false boolean d = (a>=b); //c is false 2018/11/14
23
Example 2018/11/14
24
SHIFT >> (right) by one bit
(0xf2) >> 1 (shift right by one bit) (0x79) Example byte c = 0xf2; byte e = c >>1; //e is 0x79 2018/11/14
25
SHIFT >> by two bits
(0xf2) >> 2 (shift right by one bit) (0x3c) Example byte c = 0xf2; byte e = c >>2; //e is 0x3c 2018/11/14
26
SHIFT << (left) by one bit
(0xf2) << 1 (shift right by one bit) (0xe4) Example byte c = 0xf2; byte e = c <<1; //e is 0xe4 2018/11/14
27
SHIFT << by two bits
(0xf2) >> 2 (shift right by one bit) (0xc8) Example byte c = 0xf2; byte e = c <<2; //e is 0xc8 2018/11/14
28
Results or bit operation (example)
(1 | 2) == (1 | 3) == (1 & 2) == (1 & 3) == (0 ^ 3) == (1 ^ 3) == (3 ^ 3) == ~0 == -1 (signed) or 255 (unsigned) 2018/11/14
29
Example of shift result 2018/11/14
30
Relational Operators Operator Description == Equal to != Not equal to
> Greater than < Less than >= Grater than or equal to <= Less than or equal to 2018/11/14
31
Example int a = 3; int b = 4; boolean c = (a>b); //c is false
boolean d = (a<b); //c is true boolean e = (a==b); //c is false boolean d = (a>=b); //c is false 2018/11/14
32
? operator It is used to replace if-then-else statement
Expression1 ? Expression2: Expression 3; If (expression1) is true, it will evaluate Expression 2, else Expression 3 int i = 3; int j = 4; int k; (i > j)? k = 10: k = 20; // (i > j) is false, k =20 2018/11/14
33
Summary Object-oriented Programming is the core of Java. Each object consists of states and behaviour. Arithmetic Operators -- +, - * /, ++, --, % Bitwise operators -- ~, ^, &, ^, >>, <<, >>> Relational operators -- ==, !=, >, < , >=, <= Boolean Logical Operator -- &. |, !, &= ? Operator – Exp1? Exp2: Exp3 2018/11/14
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.