W4-Presentation-Operators and Operator Precedence
W4-Presentation-Operators and Operator Precedence
Precedence
Operator
In Java, we use different types of operators and these are the following:
System.out.println("Variable values:");
System.out.println("x = " + x);
System.out.println("y = " + y);
System.out.println("result = " + result);
System.out.println(); //add a new line
System.out.println("Arithmetic Operation:");
System.out.println("Logical Expression:");
System.out.println(); //add a new line
System.out.println("Logical AND:");
result = x && x; //result here is true
System.out.println(x + " && " + x + " = " + result);
result = x && y; //result here is false
System.out.println(x + " && " + y + " = " + result);
result = y && x; //result here is false
System.out.println(y + " && " + x + " = " + result);
result = y && y; //result here is false
System.out.println(y + " && " + y + " = " + result);
System.out.println(); //add a new line
System.out.println("Logical OR:");
result = x || x; //result here is true
System.out.println(x + " || " + x + " = " + result);
result = x || y; //result here is true
-is also called as the ternary operator because this operator consists
of three operands.
operand1?operand2:operand3
The operand1 is a Boolean expression that will be evaluated, if the value of
operand1 is true, operand2 is the value returned, otherwise operand3 will be
returned.
public class ConditionalOperatorDemo {
public static void main(String[] args) {
String result = "";
int age = 17;
result = (age >= 18) ? "qualified to Vote!" : "too young to vote!";
System.out.println(age + " is " + result);
}
}
The output of the program above is:
• 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.
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:
A B A& B A| B A^ B ~A
0 0 0 0 0 1
0 1 0 1 1 1
1 1 1 1 0 0
1 0 0 1 1 0
& (Bitwise And)
– The result of the operation will be 1 if it exists in both operands.
For example: 12 & 5
We need to convert both values to binary:
12 = 00001100, 5 = 00000101
Value Bit Representation
12 1 1 0 0
5 0 1 0 1
Resul
0 1 0 0
t
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
Here is the example of Bitwise And in Java program:
14 = 00001110, 5 = 00000101
Value Bit Representation
a = 14 1 1 1 0
b=5 0 1 0 1
Result 0 1 0 0
c = 00000100 or 4
| (Bitwise Inclusive Or)
For example: 9 | 13
We need to convert both values to binary:
9 = 00001001, 13 = 00001101
Value Bit Representation
9 1 0 0 1
13 1 1 0 1
Result 1 1 0 1
therefore: 12 | 5 = 13
Here is the example of Bitwise Inclussive Or in Java program:
20 = 00010100, 16 = 00010000
Value Bit Representation
a = 20 1 0 1 0 0
b = 16 1 0 0 0 0
Result 1 0 1 0 0
c = 00010100 or 20
^ (Bitwise Exclussive Or)
– The result of the operation will be 1 if both operands are different.
For example: 6 | 9
We need to convert both values to binary:
6 = 00000110, 9 = 00001001
Value Bit Representation
6 0 1 1 0
9 1 0 0 1
Result 1 1 1 1
therefore: 6 | 9 = 15
Here is the example of Bitwise Exclussive Or in Java program:
20 = 00010010, 17 = 00010001
Value Bit Representation
a = 18 1 0 0 1 0
b = 17 1 0 0 0 1
Result 0 0 0 1 1
c = 00000011 or 3
~ (Bitwise One’s Complement)
– This operator is used to flip the bits, it toggles each bit from 0 to 1 or
from 1 to 0.
For example:
10001001 and if we flip the each bit it will result to 01110110.
<< (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
.10100001 >> 2 = 00101000
• Assignment Operators
Assignment statement
- is use to assign value to a variable. It uses = (equal) operator
or any assignment operators to designate a value for a variable.
<variable> = <expression>;
Expression
For example:
multiplicative *, /, %
additive +, -
equality ==, !=
For example:
z = x%2*3+b/4+9-c;