ITE 1122 - Fundamental Structures of Programming - 2
ITE 1122 - Fundamental Structures of Programming - 2
programming_2
Agenda
Basic Operators
Arithmetic Operators
Relational Operators
Bitwise Operators
Logical Operators
Assignment Operators
Conditional Operator
Basic Operators
Java provides a rich set of operators to manipulate variables. We can
divide all the Java operators into the following groups −
Arithmetic Operators
Relational Operators
Bitwise Operators
Logical Operators
Assignment Operators
Misc Operators
Arithmetic Operators
Arithmetic Operator Description Example
Adds values on either side
operators are used + (Addition) of the operator. A + B will give 30
on bits and performs bit- Binary Left Shift Operator. The left operands
A << 2 will give 240 which is
by-bit operation. << (left shift) value is moved left by the number of bits
specified by the right operand.
1111 0000
Assume if a = 60 and b =
Binary Right Shift Operator. The left operands
13; now in binary format >> (right shift)
value is moved right by the number of bits A >> 2 will give 15 which is
specified by the right operand.
they will be as follows − 1111
a = 0011 1100 Shift right zero fill operator. The left operands
value is moved right by the number of bits
b = 0000 1101 specified by the right operand and shifted A >>>2 will give 15 which is
>>> (zero fill right shift) values are filled up with zeros. 0000 1111
Bitwise Operators - example
public class Test {
c = a >> 2; /* 15 = 1111 */
System.out.println("a >> 2 = " + c );
supported by Java -= from the left operand and assign the result to left operand. C -= A is equivalent to C =
C–A
higher precedence than others; for example, Multiplicative >* / Left to right
the multiplication operator has higher Additive >+ - Left to right
precedence than the addition operator
Shift >>> >>> << Left to right
with 3 * 2 and then adds into 7. Bitwise XOR >^ Left to right
operators with the highest precedence appear Logical AND >&& Left to right
at the top of the table, those with the lowest Logical OR >|| Left to right
appear at the bottom. Within an expression, Conditional ?: Right to left
higher precedence operators will be evaluated
Assignment >= += -= *= /= %= >>= <<= &= ^= |= Right to left
first.