Module 3 Java Operators Object Oriented ProgrammingJava JAVA
Module 3: Java Operators Object Oriented Programming(Java)
JAVA OPERATORS ØUnary Arithmetic Operators ØBitwise Operators ØShift Operators ØRelational and Logical Operators ØCast Operator ØAssignment Operator ØTernary Operator
JAVA OPERATORS Unary Arithmetic Operators + ++ -- Sign Operators Increment Decrement Increment and decrement operators have two formats: postfix and prefix.
JAVA OPERATORS Binary Arithmetic Operators + - Addition Subtraction * / Multiplication % Modulo Division
JAVA OPERATORS Arithmetic Operators There are some general guidelines to consider when creating arithmetic expressions: Ø Use parentheses to alter precedence. Ø Consider the size of resulting values and possible loss of precision. Ø Apply arithmetic operators’ precedence level.
JAVA OPERATORS Arithmetic Operators ØWhen using negative numbers with modulus calculation, drop the negative signs from either operand calculate the result. The sign of the left operand is the sign of the result. Ø The (+) operator can be used to add numbers and String objects. Ø Dividing integer by zero results in the throwing of Arithmetic. Exception
JAVA OPERATORS Bitwise Operators Ø works on integer values, by manipulating its bitpattern equivalent. Ø Operator Use Operation ~ ~Op Bitwise inversion (unary) | Op 1 | Op 2 Bitwise OR & Op 1 & Op 2 Bitwise AND ^ Op 1 ^ Op 2 Bitwise EX-OR OR logic table AND logic table op 1 op 2 result 0 0 0 0 1 1 0 1 0 1 1 0 0 1 1 1
JAVA OPERATORS Shift Operators Øperforms bit manipulation on data by shifting the bits of its first operand right or left. This table summarizes the shift operators available in the Java programming language. Operator Use Operation >> op 1 >> op 2 shift bits of op 1 right by distance op 2 << op 1 << op 2 shift bits of op 1 left by distance op 2 >>> op 1 >>> op 2 shift bits of op 1 right by distance op 2 (unsigned)
JAVA OPERATORS Relational Operators Ø compares two values and determines the relationship between them. Operator Use Returns true if > op 1 > op 2 op 1 is greater than op 2 >= op 1 >= op 2 op 1 is greater than or equal to op 2 < op 1 < op 2 op 1 is less than op 2 <= op 1 <= op 2 op 1 is less than or equal to op 2 == op 1 == op 2 op 1 and op 2 are equal != op 1 != op 2 op 1 and op 2 are not equal
JAVA OPERATORS Logical Operators Ø often used with relational operators to construct more complex decision-making expressions. Operator && Use Operation Op 1 && Op 2 Short circuit AND || Op 1 || Op 2 Short circuit OR & Op 1 & Op 2 AND | Op 1 | Op 2 OR
JAVA OPERATORS Cast Operators Øused in converting (casting) one type into another type or an object into another object. Syntax: (Cast type) Value; Example: double dbl = 55. 66; int number = (int)dbl;
JAVA OPERATORS Assignment Operators used in converting (casting) one type into another type or an object into another object. Syntax: (Cast type) Value; Example: double dbl = 55. 66; int number = (int)dbl;
JAVA OPERATORS Ternary Operator an operator that deals with three operands. Syntax: variable = condition ? value : value; Example: int x = 20, y = 30; int bigger. Number = 0; bigger. Number = x>y ? x : y; // What do u think will b d value of bigger. Number?
Questions & Comments ?
- Slides: 14