chapter4

15
4.1 Object Operations

Upload: teknik-komputer-ui

Post on 22-May-2015

293 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter4

4.1 Object Operations

Page 2: Chapter4

4.1.1 operators

• Dot ( . ) and new operate on objects

• The assignment operator ( = )

• Arithmetic operators + - * / %

• Unary operators ++, --, *=, %=

• Operator precedence

• Arithmetic expressions are left associative

• Assignment operators are right associative

Page 3: Chapter4

4.2.6 Boolean data and comparison operators

• boolean values are true or false ( not 1 or 0)

• Less than <, greater than >, equal to ==, less

than or equal to <=, greater than or equal to >=, not equal !=

• AND &, OR |, XOR ^

• Short curcuit AND &&, OR ||

Page 4: Chapter4

4.2.8 Conditional operator

• Alternative for if - else

y = x > 4 ? 99: 9;

Is the same as:

if(x > 4)

y = 99;

else

y = 9;

Page 5: Chapter4

4.2.9 Bitwise operators

• Perform shifting of bits on integral types, preferably int of long

• <<3 shifts all bits left 3 places. All new bits are 0

• >>>3 shifts all bits right 3 places. All new bits are 0

• >>3 shifts all bits right 3 places. The new bits are the same as the most significant bit beforethe shift.

Page 6: Chapter4

4.3.1 Casting and conversion

• Casting assigns a value of one type to a variable of another type

• If it is possible to lose information, an explicitcast is required

long bigValue = 99L;

int squashed = (int)bigValue;

Page 7: Chapter4

4.4.2 String and StringBuffer class

• The String object can be used to store an arbitrary number of textual characters

• Strings are immutable: They do not change

• Concatenating two Strings results in the creation of another String

• Use StringBuffer to hold Strings that will change

Page 8: Chapter4

4.5.1 Decision making and repetition

• Control structures control the flow of statement execution

• Three control structures: Sequence, selectionor decision, repetition

• In OOP, control structures exist within methodsonly

• Selection control structure provides conditionalexecution (if-else)

• Repitition control structure causes the computer to repeat certain actions (for <loop>, while <loop>, do <loop>)

Page 9: Chapter4

4.5.3 If statement

• Basic

if(x == 3) {

System.out.println(“x equals 3”);

}

else {

System.out.println(“x does not equal 3”);

}

Page 10: Chapter4

4.5.4 Multiple condition If

• Multiple condition

if(a < b) {

System.out.println(“a is less than b”);

}

else if (a < c) {

System.out println(“a is less than c”);

}

else {

System.out.println(“a is not less than b or c”);

}

Page 11: Chapter4

4.5.5 Nested if

• Nested If

if(x == 3) {

System.out.println(“x equals 3”);

if( y == 4) {

System.out.println(“ …and y equals 4”);

}

}

else {

System.out.println(“x does not equal 3”);

}

Page 12: Chapter4

4.5.6 Switch statements

• A conditional control structure that allows a value to be compared to more than one other value

switch(test) {

case 1:

System.out.println(“test equals 1”);

break;

case 2:

System.out.println(“test equals 2”);

break;

case 3:

System.out.println(“test equals 3”);

break;

default:

System.out.println(“test does not equal 1, 2 or 3”);

break;

}

Page 13: Chapter4

4.5.7 Loop

• do while: Execution loops. Conditional evaluation at the end of the loop

• while: Execution loops through the block. Conditional evaluation occurs at the start of the loop

• for next: Specifies an initialization block, a conditional evaluation and a block that is executed in every loop

Page 14: Chapter4

4.5.11 Use of break, continue, and label

• break is used to exit a block

• continue is used to return to the start of the loop

• label can be applied to a statement or block, then used with continue or break

• break can be used to exit a labeled block

• continue can be used to resume at a statement

Page 15: Chapter4

4.6.1 The java.lang.System class