Comprog 5 - Programing Operator
Comprog 5 - Programing Operator
In this module, we are going to discuss the different operators that can
be use in Java.
4.1 Operator
▪ Arithmetic Operators
▪ Relation Operators
▪ Logical Operators
▪ Conditional Operator
▪ Bitwise and Bit Shift Operators
▪ Assignment Operators
Computer Programming 2 1
Week 3-4 Operator and Operator Precedence
System.out.println("Variable values:");
System.out.println("x = " + x);
System.out.println("y = " + y);
System.out.println("result = " + result);
2
MODULE OF INSTRUCTION
Computer Programming 2 3
Week 3-4 Operator and Operator Precedence
4
MODULE OF INSTRUCTION
Computer Programming 2 5
Week 3-4 Operator and Operator Precedence
6
MODULE OF INSTRUCTION
System.out.println("Logical Expression:");
Computer Programming 2 7
Week 3-4 Operator and Operator Precedence
8
MODULE OF INSTRUCTION
operand1?operand2:operand3
Computer Programming 2 9
Week 3-4 Operator and Operator Precedence
Let’s try another example, this time we set the value of variable age to
19.
Bitwise and Bit Shift Operators are basically used for bit manipulation,
or using bits of an integral type value to perform bit-by-bit operation.
These operators can be applied to any integral type (long, int, short,
char and byte).
10
MODULE OF INSTRUCTION
There are three Bitwise operators in Java, these are & (Bitwise And), |
(Bitwise Inclusive Or), ^ (Bitwise Exclusive Or) and ~ (One’s
Compliment). The truth table for Bitwise operators are as follows:
We will be using byte data type for all the examples in bitwise
operators, because byte can be easily represented in bits (byte is only 8
bits while other integral data types is much more than that).
The table above shows the bit computation of the example, each
bit will be computed separately using the & operator. For
example, let us execute the second row from the table 1 & 0 the
result would be 0. The result if we execute 00001100 & 00000101
expression is 00000100 or 4.
therefore: 12 & 5 = 4
Computer Programming 2 11
Week 3-4 Operator and Operator Precedence
therefore: 12 | 5 = 13
12
MODULE OF INSTRUCTION
therefore: 6 | 9 = 15
Computer Programming 2 13
Week 3-4 Operator and Operator Precedence
▪ << (Binary Left Shift) –This operator shifts or move the left
operands value to the left by the number of bits specified in the
right operand.
For example:
00000001 << 2 = 00000100
.01010001 << 3 = 1010001000
▪ >> (Binary Right Shift) – This operator shifts or move the left
operands value to the right by the number of bits specified in
the right operand, filling with the highest (sign) bit on the left.
For example:
01001000 >> 3 = 00001001
14
MODULE OF INSTRUCTION
Assignment Operators
<variable> = <expression>;
int x = 1;
x=m+1
x = y = z = 1;
Computer Programming 2 15
Week 3-4 Operator and Operator Precedence
x = 1;
y = 1;
z = 1;
16
MODULE OF INSTRUCTION
z &= 2 is same as z
&= Bitwise AND and assignment operator.
=z&2
Bitwise exclusive OR and assignment z ^= 2 is same as z
^= operator. =z^2
Bitwise inclusive OR and assignment z |= x is same as z =
|= operator. z|x
Operators Precedence
postfix expr++, expr--
++expr, --expr, +expr, -
unary
expr, ~, !
multiplicative *, /, %
additive +, -
shift <<, >>, >>>
relational <, >, <=, >=
equality ==, !=
bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
logical AND &&
logical OR ||
ternary ?:
=, +=, -=, *=, /=, %=, &=,
assignment
^=, |=, <<=, >>=, >>>=
For example:
z = x%2*3+b/4+9-c;
Computer Programming 2 17
Week 3-4 Operator and Operator Precedence
we can re-write the expression and place some parenthesis base on operator
precedence,
18