Chapter 2: Java Fundamentals

Post on 04-Jan-2016

107 views 3 download

Tags:

description

Chapter 2: Java Fundamentals. Operators. Content. Group of Operators Arithmetic Operators Assignment Operator Order of Precedence Increment/Decrement Operators Relational Operators Logical Operators Operator precedence Casting. Operators. Operators are special symbols used for: - PowerPoint PPT Presentation

Transcript of Chapter 2: Java Fundamentals

Chapter 2: Java Fundamentals

Operators

Content

Page 2

Group of Operators Arithmetic Operators Assignment Operator Order of Precedence Increment/Decrement Operators Relational Operators Logical Operators Operator precedence Casting

Operators

Page 3

Operators are special symbols used for: mathematical functions assignment statements logical comparisons

Examples of operators: 3 + 5 // uses + operator 14 + 5 – 4 * (5 – 3) // uses +, -, * operators

Expressions: can be combinations of variables and operators that result in a value

Groups of Operators

Page 4

There are 5 different groups of operators: Arithmetic Operators Assignment Operator Increment / Decrement Operators Relational Operators Logical Operators

Java Arithmetic Operators

Page 5

Addition +Subtraction –Multiplication Division /Remainder (modulus ) %

Arithmetic Operators

Page 6

The following table summarizes the arithmetic operators available in Java.

This is an integer division where the fractional part is truncated.

Page 7

Example

Example of division issues:

10 / 3 gives 3

10.0 / 3 gives 3.33333

As we can see, • if we divide two integers we get an integer result.

• if one or both operands is a floating-point value we get a floating-point result.

Page 8

Modulus

Generates the remainder when you divide two integer values.

5%3 gives 2 5%4 gives 1

5%5 gives 0 5%10 gives 5

Modulus operator is most commonly used with integer operands. If we attempt to use the modulus operator on floating-point values we will garbage!

Order of Precedence

Page 9

( ) evaluated first, inside-out

, /, or % evaluated second, left-to-right

+, evaluated last, left-to-right

Basic Assignment Operator

Page 10

We assign a value to a variable using the basic assignment operator (=).

Assignment operator stores a value in memory. The syntax is

leftSide = rightSide ;

Examples:i = 1;

start = i;

sum = firstNumber + secondNumber;avg = (one + two + three) / 3;

Allways it is a variable identifier.

It is either a literal | a variable identifier | an expression.

The Right Side of the Assignment Operator

Page 11

The Java assignment operator assigns the value on the right side of the operator to the variable appearing on the left side of the operator.

The right side may be either: Literal: ex. i = 1; Variable identifier: ex. start = i; Expression: ex. sum = first + second;

Assigning Literals

Page 12

In this case, the literal is stored in the space memory allocated for the variable at the left side.

int firstNumber=1, secondNumber;firstNumber = 234;secondNumber = 87;

A

B

firstNumber 1

secondNumber ???

A. Variables are allocated in memory.

A. Variables are allocated in memory.

B. Literals are assigned to variables.

B. Literals are assigned to variables.

firstNumber 234

secondNumber 87Code

State of Memory

Assigning Variables

Page 13

In this case, the value of the variable at the right side is stored in the space memory allocated for the variable at the left side.

int firstNumber=1, i;firstNumber = 234;i = firstNumber;

A

B

firstNumber 1

i ???

A. Variables are allocated in memory.

A. Variables are allocated in memory.

B. values are assigned to variables.

B. values are assigned to variables.

firstNumber 234

i 234Code

State of Memory

Assigning Expressions

Page 14

In this case, the result of the evaluation of the expression is stored in the space memory allocated for variable at the left side.

int first, second, sum;first = 234;second = 87;Sum = first + second

A

B

A. Variables are allocated in memory.

A. Variables are allocated in memory.

B. Values are assigned to variables.

B. Values are assigned to variables.

Code

State of Memory

first 1 second ???

sum ???

first 234 second 87

sum 321

Updating Data

Page 15

Code State of Memory

number ???

A. The variable is allocated in memory.

A. The variable is allocated in memory.

B. The value 237 is assigned to number.

B. The value 237 is assigned to number.

int number;

number = 237;

AB

Cnumber = 35;C. The value 35

overwrites the previous value 237.

C. The value 35 overwrites the

previous value 237.

number 237

number 35

Page 16

public class Sum { // main method public static void main( String args[] ){ int a, b, sum; a = 20;

b = 10; sum = a + b; System.out.println(a + ” + ” + b + “ = “ + sum);

} // end main } // end class Sum

Example: Sum of two integer

Arithmetic/Assignment Operators

Page 17

Java allows combining arithmetic and assignment operators into a single operator:

Addition/assignment +=Subtraction/assignment =Multiplication/assignment =Division/assignment /=Remainder/assignment %=

Arithmetic/Assignment Operators

Page 18

The syntax isleftSide Op= rightSide ;

This is equivalent to:leftSide = leftSide Op rightSide ;

x%=5; x = x % 5; x*=y+w*z; x = x*(y+w*z);

Allways it is a variable identifier. It is an arithmetic

operator.

It is either a literal | a variable identifier | an expression.

Increment/Decrement Operators

Page 19

Only use ++ or   when a variable is being incremented/decremented as a statement by itself.

x++; is equivalent to x = x+1;

x--; is equivalent to x = x-1;

Pre-increment versus post-increment

If the unary increment operator, ++, is used after the operand, e.g. x++, it is called a postincrement operator.

If the unary increment operator, ++, is used before the operand, e.g. ++x, it is called a preincrement operator.

Page 20

Pre-increment versus post-increment

It is important to note that when incrementing or decrementing a variable in a statement by itself, there is no difference between the pre and post forms of the operator.

It is only when a variable occurs in the context of a larger expression, that the pre and post forms of the operator have a different effect.

Page 21

Pre-increment versus post-increment

if we include the statement ++x as part of a larger statement, e.g.y = ++x;

the effect is :

The value in x is incremented.The new value x is stored in y.

Page 22

Pre-increment versus post-increment

x = 5;y = ++x;

After these statements are executed, x contains the value ?.y contains the value ?.

Page 23

Pre-increment versus post-increment

x = 5;y = ++x;

After these statements are executed, x contains the value 6.y contains the value 6.

Page 24

Pre-increment versus post-increment

However, if we include the statement x++ as part of a larger statement, e.g.y = x++;

the effect is somewhat different.

The old value of x is stored in y. The value in x is incremented

Page 25

Pre-increment versus post-increment

x = 5;y = x++;

After these statements are executed, x contains the value ?.y contains the value ?.

Page 26

Pre-increment versus post-increment

x = 5;y = x++;

After these statements are executed, x contains the value 6.y contains the value 5.

Page 27

Pre-increment versus post-increment

int z = 2;

System.out.println(z);

System.out.println(z++);

System.out.println(z);

System.out.println();

z = 2;

System.out.println(z);

System.out.println(++z);

System.out.println(z);

Page 28

Pre-increment versus post-increment

The output from this code would be:

2

2

3

2

3

3

Page 29

Pre-increment versus post-increment

If the unary increment operator, ++, is used after the operand, e.g. x++, it is called a postincrement operator.

Page 30

Pre-increment versus post-increment

The unary decrement operator, −−, also has two forms:postdecrement, x−−predecrement, −−x

Page 31

Pre-increment versus post-increment

The unary decrement operator, −−, also has two forms:postdecrement, x−−predecrement, −−x

The difference between the postdecrement and predecrement operators is the same as the difference between the postincrement and preincrement operators.

Page 32

Pre-increment versus post-increment

It is important to note that when incrementing or decrementing a variable in a statement by itself, there is no difference between the pre and post forms of the operator.

Page 33

Pre-increment versus post-increment

Increment the value in the integer variable x by 1 in four different ways.

1.x = x+1;

2.++x;

3.x++;

4.x += 1;

Page 34

Pre-increment versus post-increment

What is the value in i and n after the following code snippet has been executed?

int i=10;

int n = i++ % 5;

Page 35

Pre-increment versus post-increment

What is the value in i and n after the following code snippet has been executed?

int i=10;

int n = i++ % 5;

i has the value 11.

n has the value 0.

Page 36

Pre-increment versus post-increment

What would be the effect of changing the postincrement operator to a preincrement operator?

int i=10;

int n = ++i % 5;

Page 37

Pre-increment versus post-increment

What would be the effect of changing the postincrement operator to a preincrement operator?

int i=10;

int n = ++i % 5;

i has the value 11;

n has the value 1;

Page 38

39

Relational Operators

Relational operators compare two values They Produce a boolean value (true or false)

depending on the relationship

Operation

Is true when

a >b a is greater than b

a >=b a is greater than or equal to b

a ==b a is equal to b

a !=b a is not equal to b

a <=b a is less than or equal to b

a <b a is less than b

Page 39

Example

Page 40

int x = 3; int y = 5; boolean result;

result = (x > y); now result is assigned the value false

because 3 is not greater than 5

Logical Operators

Symbol Name&& AND

|| OR

! NOT

|| T F

T T T

F T F

&& T F

T T F

F F F

Page 41

Example

Page 42

boolean x = true;boolean y = false;boolean result;

result = (x && y);result is assigned the value false

result = ((x || y) && x);(x || y) evaluates to true(true && x) evaluates to trueresult is then assigned the value true

Operators Precedence

Parentheses (), inside-out

Increment/decrement ++, --, from left to right

Multiplicative *, /, %, from left to right

Additive +, -, from left to right

Relational <, >, <=, >=, from left to right

Equality ==, !=, from left to right

Logical AND &&

Logical OR ||

Assignment =, +=, -=, *=, /=, %=

Page 43

Type Conversion (Casting)

44

Used: to change one data type to another . to avoid implicit type coercion.

Syntax:

(dataTypeName) expression

Expression evaluated first, then the value is converted to dataTypeName

Type Conversion (Casting)

45

Examples:1. (int)(7.9 + 6.7) -> 142. (int)(7.9) + (int)(6.7) -> 133. (double)(17) -> 17.04. (double)(8+3) > (double)11 ->11.05. (double)(7) /2 = 7.0/2 -> 3.56. (double)(7/2) = 3.07. (int)(7.8+(double)(15)/2) ->

(int)15.3 ->15

46

Type Conversion (Casting)

8. (int)(‘A’) -> 65 9. (int)(‘8’) -> 5610. (char)(65) -> ‘A’ 11. (char)(56) -> ‘8’

Example -TRACE

Page 47

This example further illustrates how assignment statements and input statements manipulate variables. Consider the following declarations:

int firstNum;int secondNum;char ch;double z;Also suppose that the following statements execute in the order given:1. firstNum = 4;2. secondNum = 2 * firstNum + 6;3. z = (firstNum + 1) / 2.0;4. ch = 'A';5. firstNum = (int)(z) + 8;6. firstNum = secondNum-- ;

After statment 6 Variables

firstNumsecondNum

ch z

Data type compatibility

int x= 7.5; int x = false; int y=13

double z= y/2; double z =13.0;

int w=z/2;

double z= (double)y/2;

double w=z/2;int w = (int) z/2;

Page 48

Implicit VS Explicit type conversions

An implicit conversion is where the conversion happens without any syntax.

e.g.

int i = 999999999;

double d = i;

int x;

char c='a';x=c;

double>int>long>short>byte double>int>long>char

An explicit conversion is where you use some syntax to tell the program to do a conversion.

e.g.

int x=97; char c=(char)x;

int i = 999999999; byte b = (byte) i;

int y=(int)7.5;

Page 49

Implicit type conversions Explicit type conversions