04_Handout_1(114)
04_Handout_1(114)
Modulus and Divides the left operand by the right operand and
%= 𝑥% = 𝑧
Assignment stores the remainder in the left operand
Table 3. Assignment operators.
Shift Operators
These operators work on bits of data. It involves moving the bit pattern to the left or right. These operators
can only be used with integer data types.
Name Operator Description Example
The bits of the left operand are shifted right by the
Right Shift >> 𝑥=9≫2
number specified as the value for the second operand.
Left Shift << Shifts bits to the left, filling zeros at the right. 𝑥=9≪2
Right Unsigned Works the same as the right shift, but 0 is used and
>>> 𝑥 = −10 ≫> 3
Shift replaces the bits that are shifted.
Table 6. Shift operators.
When used with Boolean, x and y are integers and can be replaced with expressions that give a true or false
result such as when both the expressions evaluate to true, the result of using & is true. Otherwise, false.
Logical Operators
These are used to combine the results of Boolean expressions. These operators share a likeness with bitwise
operators, but logical operators are limited to Boolean expressions only.
Name Operator Description Example
Logical If both the operands are non-zero, the expression
&& 𝑥 > 4&&𝑦 < 8
AND returns true; otherwise, it returns false.
If one or both the operands are non-zero, the expression
Logical OR || 𝑥 > 4||𝑦 < 8
returns true; otherwise, it returns FALSE.
Table 8. Logical operators.
For the example of Logical AND, the result is true if the first condition (x>4) and the second condition (y>8)
are both true. If any of the conditions are false, the result is false.
Conditional Operator
This operator is used to control the flow of the program. The ternary operator is used mainly in loop
statements.
Name Operator Description Example
(condition)? Evaluates val1 if the condition returns true and val2
Ternary 𝑥 = (𝑦 > 𝑧)? 𝑦: 𝑧
val1:val2 if the condition returns false.
Table 9. Conditional operators.
To explain the example, x is assigned the value of y if it is greater than z; otherwise, x is assigned the value of
z.
Order of Precedence of Operators
Order of Precedence shows the hierarchy wherein an operation with high precedence is performed before
the others. The table shows those with the same level of precedence are listed in the same row.
Category Operators
High Precedence [], ()
Unary +, -, ~, !, ++, --
Multiplicative *, /, %
Additive +, -
Shift <<, >>, >>>
Relational <, <=, >=, >
Equality =, ==, !=
Bitwise &, ^, |
Logical &&, ||
Conditional ?:
Assignment =, +=, -=, *=, !=, %=
Table 10. Order of precedence.
Assignment Expression
It is an expression that involves assigning a value to a variable. For example:
int a = 8; The last line is an assignment expression that adds 4 to the value of a and
a = a + 4; assigns the result back to a.
Relational Expression
It is an expression that compares values using relational operators. For example:
int x = 6;
The last line is a relation expression comparing the values of x and y and
int y = 8;
boolean result = x < y; assigning the result to the variable result.
Logical Expression
It is an expression involving logical operators. For example:
int j = 5; The last line is a logical expression that checks whether j is less
int k = 4; than k and whether k is less than 4. The result is assigned to the
boolean result = (j<k) && (k<4); result.
Conditional Expression
It is an expression involving the use of the ternary operator (?:) to assign a value based on a condition. For
example:
int l = 7;
The last line is a conditional expression that checks whether l is less than
int m = 3;
int n = (l < m) ? 2 : 3; m. If true, 2 is assigned to the variable of n; otherwise, 3 is assigned.
References:
Agarwal, S. & Bansal, H. (2023). Java in depth. BPB Publications.
Burk, S. (2023). Java programming: The complete beginner’s guide. Orchid Publishing.
Nawghare, A. (2023). Java expressions: An introduction with examples. [Web Article]. Retrieved on March 14, 2024, from
https://codegym.cc/groups/posts/java-expressions-an-introduction-with-examples