2 0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE 2

  • Slides: 15
Download presentation
2. 0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE 2. 3 Use operator to modify values.

2. 0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE 2. 3 Use operator to modify values. 1

FP 301 OBJECT ORIENTED PROGRAMMING Learning Outcome Subtopic 2. 3 1 • Modify variable

FP 301 OBJECT ORIENTED PROGRAMMING Learning Outcome Subtopic 2. 3 1 • Modify variable values using operator: . 2 • Apply the rules of precedence for an expression in modifying variables values 3 • Explain type casting and promotion in Java programs 4 • Implement type casting and promotion to change the data types 2

FP 301 OBJECT ORIENTED PROGRAMMING STANDARD MATHEMATICALS OPERATORS Purpose Addition Subtraction Multiplication Division Operator

FP 301 OBJECT ORIENTED PROGRAMMING STANDARD MATHEMATICALS OPERATORS Purpose Addition Subtraction Multiplication Division Operator Example + sum = num 1 + num 2; If num 1 is 10 and num 2 is 2, sum is 12. - diff = num 1 – num 2; If num 1 is 10 and num 2 is 2, diff is 8. * / Comments prod = num 1 * num 2; If num 1 is 10 and num 2 is 2, prod is 20. quot = num 1 / num 2; If num 1 is 31 and num 2 is 6, quot is 5. Division returns an integer value (with no remainder). 3

FP 301 OBJECT ORIENTED PROGRAMMING Cont. . Purpose Operator Remainder % Example mod =

FP 301 OBJECT ORIENTED PROGRAMMING Cont. . Purpose Operator Remainder % Example mod = num 1 % num 2; If num 1 is 31 and num 2 is 6, mod is 1. Comments Remainder finds the remainder of the first number divided by the second number. Remainder always gives an answer with the same sign as the first operand. 4

FP 301 OBJECT ORIENTED PROGRAMMING INCREMENT AND DECREMENT OPERATORS ( ++ and -- )

FP 301 OBJECT ORIENTED PROGRAMMING INCREMENT AND DECREMENT OPERATORS ( ++ and -- ) i) The long way : age = age + 1; ii) The short way : Operator ++ Purpose Example Pre-increment (++variable) int i = 6; int j = ++i; i is 7, j is 7 Post-increment (variable++) int i = 6; int j = i++; i is 7, j is 6 Notes The value of i is assigned to j before i is incremented. Therefore, j is assigned 6. 5

FP 301 OBJECT ORIENTED PROGRAMMING Cont. . Operator -- Purpose Example Pre-decrement (--variable int

FP 301 OBJECT ORIENTED PROGRAMMING Cont. . Operator -- Purpose Example Pre-decrement (--variable int i = 6; int j = --i; i is 5, j is 5 Post decrement (variable--) int i = 6; int j = i--; i is 5, j is 6 Notes The value i is assigned to j before i is decremented. Therefore, j is assigned 6. 6

FP 301 OBJECT ORIENTED PROGRAMMING LOGICAL OPERATORS • The boolean operators are: ! :

FP 301 OBJECT ORIENTED PROGRAMMING LOGICAL OPERATORS • The boolean operators are: ! : NOT & : AND | : OR ^ : XOR • The short-circuit boolean operators are: && : AND || : OR • You can use these operators as follows: My. Date d = reservation. get. Departure. Date(); if ( (d != null) && (d. day > 31) { // do something with d } 7

FP 301 OBJECT ORIENTED PROGRAMMING BITWISE LOGICAL OPERATORS • The integer bitwise operators are:

FP 301 OBJECT ORIENTED PROGRAMMING BITWISE LOGICAL OPERATORS • The integer bitwise operators are: ~ : Complement (vice versa) & : AND (only true will become true) ^ : XOR (x+y) % 2 | : OR (only false will become false) • Byte-sized examples include: ~ 0 1 1 0 ^ 0 0 1 1 1 0 0 1 1 0 1 0 0 1 1 0 0 0 1 0 & | 0 0 1 1 0 1 0 0 1 1 0 0 1 1 0 1 1 1 1 8

FP 301 OBJECT ORIENTED PROGRAMMING RIGHT-SHIFT OPERATORS • Arithmetic or signed right shift (>>)

FP 301 OBJECT ORIENTED PROGRAMMING RIGHT-SHIFT OPERATORS • Arithmetic or signed right shift (>>) operator: • Examples are: 128 >> 1 returns 128/21 = 64 256 >> 4 returns 256/24 = 16 -256 >> 4 returns -256/24 = -16 • The sign bit is copied during the shift. • Logical or unsigned right-shift (>>>) operator: • This operator is used for bit patterns. • The sign bit is not copied during the shift. 9

FP 301 OBJECT ORIENTED PROGRAMMING LEFT-SHIFT OPERATORS Left-shift (<<) operator works as follows: 128

FP 301 OBJECT ORIENTED PROGRAMMING LEFT-SHIFT OPERATORS Left-shift (<<) operator works as follows: 128 << 1 returns 128 * 21 = 256 16 << 2 returns 16 * 22 = 64 10

FP 301 OBJECT ORIENTED PROGRAMMING SHIFT OPERATOR EXAMPLES 11

FP 301 OBJECT ORIENTED PROGRAMMING SHIFT OPERATOR EXAMPLES 11

FP 301 OBJECT ORIENTED PROGRAMMING STRING CONCATENATION WITH ‘+’ • The + operator works

FP 301 OBJECT ORIENTED PROGRAMMING STRING CONCATENATION WITH ‘+’ • The + operator works as follows: • Performs String concatenation • Produces a new String: String salutation = "Dr. "; String name = "Pete" + "Seymour"; String title = salutation + " " + name; • One argument must be a String object. • Non-strings are converted to String objects automatically. 12

FP 301 OBJECT ORIENTED PROGRAMMING APPLY THE RULES OF PRECEDENCE FOR AN EXPRESSION IN

FP 301 OBJECT ORIENTED PROGRAMMING APPLY THE RULES OF PRECEDENCE FOR AN EXPRESSION IN MODIFYING VARIABLES VALUES Example : Applying the operator precedence and associativity rule, the expression 3 + 4 * 4 > 5 * (4 + 3) - 1 is evaluated as follows: 13

FP 301 OBJECT ORIENTED PROGRAMMING TYPE CASTING AND PROMOTIONS IN JAVA PROGRAMS Type Casting

FP 301 OBJECT ORIENTED PROGRAMMING TYPE CASTING AND PROMOTIONS IN JAVA PROGRAMS Type Casting refers to changing an entity of one datatype into another. • Example of potential issue: int num 1 = 53; // 32 bits of memory to hold the value int num 2 = 47; // 32 bits of memory to hold the value byte num 3; // 8 bits of memory reserved num 3 = (num 1 + num 2); // causes compiler error • Example of potential solution: int num 1 = 53; int num 2 = 47; long num 3; num 3 = (num 1 + num 2); 14

FP 301 OBJECT ORIENTED PROGRAMMING Cont. . • Variables are promoted automatically to a

FP 301 OBJECT ORIENTED PROGRAMMING Cont. . • Variables are promoted automatically to a longer form (such as int to long). • Expression is assignment-compatible if the variable type is at least as large (the same number of bits) as the expression type. long bigval = 6; // 6 is an int type, OK int smallval = 99 L; // 99 L is a long, illegal double z = 12. 414 F; // 12. 414 F is float, OK float z 1 = 12. 414; // 12. 414 is double, illegal 15