data types & operators

33
111/07/03 1 Data Types & Operators Lecture from (Chapter 3,4)

Upload: chesna

Post on 07-Jan-2016

36 views

Category:

Documents


0 download

DESCRIPTION

Data Types & Operators. Lecture from (Chapter 3,4). Review. An introduction to Object-oriented programming Arithmetic Operators Bitwise operators Relational Operators Boolean Logical Operator Assignment Operator. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Data Types & Operators

112/04/20 1

Data Types & Operators

Lecture from (Chapter 3,4)

Page 2: Data Types & Operators

112/04/20 2

Review

An introduction to Object-oriented programming

Arithmetic OperatorsBitwise operatorsRelational OperatorsBoolean Logical OperatorAssignment Operator

Page 3: Data Types & Operators

112/04/20 3

An introduction to Object Oriented Programming - http://java.sun.com/docs/books/tutorial/java/concepts/

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.

Page 4: Data Types & Operators

112/04/20 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.

Page 5: Data Types & Operators

112/04/20 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.

Page 6: Data Types & Operators

112/04/20 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 , eating, 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.

Page 7: Data Types & Operators

7

Java Data Types

Primitive Data Types:boolean true or falsechar unicode! (16 bits)byte signed 8 bit integershort signed 16 bit integerint signed 32 bit integerlong signed 64 bit integerfloat,double IEEE 754 floating point

not an int!

Page 8: Data Types & Operators

112/04/20 8

Arithmetic Operator – page 74

Operator Description+ addition- Subtraction* Multiplication/ Division% Modules++ Increment-- Decrement

Page 9: Data Types & Operators

112/04/20 9

Example – addition and subtraction

Page 10: Data Types & Operators

112/04/20 10

Example – multiplication and division

Page 11: Data Types & Operators

112/04/20 11

Special operator

Expression Equivalent expression

a = a + 2; a +=2;

a = a – 4; a -= 4;

a = a*3; a *= 3;

a = a/4; a /=4;

a = a%2; a %=2;

Page 12: Data Types & Operators

112/04/20 12

Example of special operator

Page 13: Data Types & Operators

112/04/20 13

Increment and Decrement

Expression Equivalent expression

a = a + 1; ++a;

a = a – 1; --a;

Page 14: Data Types & Operators

112/04/20 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

Page 15: Data Types & Operators

112/04/20 15

Operation - examples

Operator ExpressionAND 1 & 1 = 1; 1& 0 = 0OR 1 |1 = 1; 1| 0 = 1; 0|0 = 0~ 0 =~1; 1 =~0;^ 0^ 0 = 0; 1^1 = 0; 1^0 =1; 0^1 = 1>> 0x0010 = 0x0001 >>1<< 0x0001 = 0x0010 <<1>>> 0x1001 = 0x0100 >>>1

Page 16: Data Types & Operators

112/04/20 16

AND Example – bits, 0x (hexadecimal)

1111 0010 (0xf2)1111 1110 (0xfe)---------------- (and) &1111 0010 (0xf2)

byte c = 0xf2;byte d = 0xfe;byte e = c & d; //e is 0xf2

Page 17: Data Types & Operators

112/04/20 17

OR Example

1111 0010 (0xf2)1111 1110 (0xfe)--------------(or) |1111 1110 (0xfe)

Examplebyte c = 0xf2;byte d = 0xfe;byte e = c | d; //e is 0xfe

Page 18: Data Types & Operators

112/04/20 18

One’s complement

1111 0010 (0xf2)

-------------- ~0000 1101 (0x0d)

Examplebyte c = 0xf2;byte e = ~c; //e is 0x0d

Page 19: Data Types & Operators

112/04/20 19

EXCLUSIVE OR

1111 0010 (0xf2)1111 1110 (0xfe)-------------- (^) 0000 1100 (0x0c)

Examplebyte c = 0xf2;

byte d = 0xfe;byte e = c ^ d; //e is 0x0c

Page 20: Data Types & Operators

112/04/20 20

Example

Page 21: Data Types & Operators

112/04/20 21

Relational Operators

Operator Description

== Equal to

!= Not equal to

> Greater than

< Less than

>= Grater than or equal to

<= Less than or equal to

Page 22: Data Types & Operators

112/04/20 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

Page 23: Data Types & Operators

112/04/20 23

Example

Page 24: Data Types & Operators

112/04/20 24

SHIFT >> (right) by one bit

1111 0010 (0xf2)>> 1 (shift right by one bit)---------------------

0111 0001 (0x79)

Examplebyte c = 0xf2;byte e = c >>1; //e is 0x79

Page 25: Data Types & Operators

112/04/20 25

SHIFT >> by two bits

1111 0010 (0xf2)>> 2 (shift right by one bit)---------------------

0011 1100 (0x3c)

Examplebyte c = 0xf2;

byte e = c >>2; //e is 0x3c

Page 26: Data Types & Operators

112/04/20 26

SHIFT << (left) by one bit

1111 0010 (0xf2)<< 1 (shift right by one bit)---------------------

1110 0100 (0xe4)

Examplebyte c = 0xf2;byte e = c <<1; //e is 0xe4

Page 27: Data Types & Operators

112/04/20 27

SHIFT << by two bits

1111 0010 (0xf2)>> 2 (shift right by one bit)---------------------

1100 1000 (0xc8)

Examplebyte c = 0xf2;

byte e = c <<2; //e is 0xc8

Page 28: Data Types & Operators

112/04/20 28

Results or bit operation (example)

(1 | 2) == 3 (1 | 3) == 3 (1 & 2) == 0 (1 & 3) == 1 (0 ^ 3) == 3 (1 ^ 3) == 2 (3 ^ 3) == 0 ~0 == -1 (signed) or 255 (unsigned)

Page 29: Data Types & Operators

112/04/20 29

Example of shift

Page 30: Data Types & Operators

112/04/20 30

Relational Operators

Operator Description

== Equal to

!= Not equal to

> Greater than

< Less than

>= Grater than or equal to

<= Less than or equal to

Page 31: Data Types & Operators

112/04/20 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

Page 32: Data Types & Operators

112/04/20 32

? operator

It is used to replace if-then-else statementExpression1 ? 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

Page 33: Data Types & Operators

112/04/20 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