Download presentation
Presentation is loading. Please wait.
1
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
2.3 Use operator to modify values.
2
Learning Outcome Subtopic 2.3
FP301 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
3
STANDARD MATHEMATICALS OPERATORS
FP301 OBJECT ORIENTED PROGRAMMING STANDARD MATHEMATICALS OPERATORS Purpose Operator Example Comments Addition + sum = num1 + num2; If num1 is 10 and num2 is 2, sum is 12. Subtraction - diff = num1 – num2; If num1 is 10 and num2 is 2, diff is 8. Multiplication * prod = num1 * num2; If num1 is 10 and num2 is 2, prod is 20. Division / quot = num1 / num2; If num1 is 31 and num2 is 6, quot is 5. Division returns an integer value (with no remainder).
4
Cont.. Purpose Operator Example Comments Remainder %
FP301 OBJECT ORIENTED PROGRAMMING Cont.. Purpose Operator Example Comments Remainder % mod = num1 % num2; If num1 is 31 and num2 is 6, mod is 1. 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.
5
INCREMENT AND DECREMENT OPERATORS ( ++ and -- )
FP301 OBJECT ORIENTED PROGRAMMING INCREMENT AND DECREMENT OPERATORS ( ++ and -- ) The long way : age = age + 1; The short way : Operator Purpose Example Notes ++ Pre-increment (++variable) int i = 6; int j = ++i; i is 7, j is 7 Post-increment (variable++) int j = i++; i is 7, j is 6 The value of i is assigned to j before i is incremented. Therefore, j is assigned 6.
6
Cont.. Operator Purpose Example Notes -- Pre-decrement (--variable
FP301 OBJECT ORIENTED PROGRAMMING Cont.. Operator Purpose Example Notes -- Pre-decrement (--variable int i = 6; int j = --i; i is 5, j is 5 Post decrement (variable--) int j = i--; i is 5, j is 6 The value i is assigned to j before i is decremented. Therefore, j is assigned 6.
7
LOGICAL OPERATORS • The boolean operators are: ! : NOT & : AND | : OR
FP301 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: MyDate d = reservation.getDepartureDate(); if ( (d != null) && (d.day > 31) { // do something with d }
8
BITWISE LOGICAL OPERATORS
FP301 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: ~ 1 1 1 1 1 1 1 1 1 & 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | 1 1 1 1 ^ 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
9
RIGHT-SHIFT OPERATORS
FP301 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.
10
LEFT-SHIFT OPERATORS Left-shift (<<) operator works as follows:
FP301 OBJECT ORIENTED PROGRAMMING LEFT-SHIFT OPERATORS Left-shift (<<) operator works as follows: 128 << 1 returns 128 * 21 = 256 16 << 2 returns 16 * 22 = 64
11
SHIFT OPERATOR EXAMPLES
FP301 OBJECT ORIENTED PROGRAMMING SHIFT OPERATOR EXAMPLES
12
STRING CONCATENATION WITH ‘+’
FP301 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.
13
FP301 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:
14
TYPE CASTING AND PROMOTIONS IN JAVA PROGRAMS
FP301 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 num1 = 53; // 32 bits of memory to hold the value int num2 = 47; // 32 bits of memory to hold the value byte num3; // 8 bits of memory reserved num3 = (num1 + num2); // causes compiler error • Example of potential solution: int num1 = 53; int num2 = 47; long num3; num3 = (num1 + num2);
15
FP301 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 = 99L; // 99L is a long, illegal double z = F; // F is float, OK float z1 = ; // is double, illegal
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.