Week 3
Week 3
Operators are mainly used to control, modify and compare data in a Java Language environment
OPERATORS
1. Arithmetic
Are used to perform addition, subtraction, multiplication, and division. They act as basic
mathematical operations.
2. Increment and Decrement
These operators increase or decrease the value of the variable by 1.
3. Assignment
Are used in Java to assign values to variables.
4. Relational
Are used to check the relationship between two operands.
5. Logical
Are used to check whether an expression is true or false.
ARITHMETIC OPERATOR
OPERATOR OPERATION
+ Addition Used to add two or more values
- Subtraction Used to subtract two or more values
* Multiplication Used to multiply two or more values
/ Division Used to divide two or more values
% Modulo Remainder of division
OPERATION PRECEDENCE
• 2 * (1 + 5) – 12 / (4 + 2)
All Operators are done from left to right in the following order: • 2 * 6 – 12 / 6
1. The operations between the parenthesis • 12 – 2
2. The multiplication and the division • 10
3. The addition and the subtraction
ASSIGNMENT OPERATORS
OPERATOR EXAMPLE EQUIVALENT TO
= a = b; a = b;
+= a += b; a = a + b;
-= a -= b; a = a - b;
*= a *= b; a = a * b;
/= a /= b; a = a / b;
%= a %= b; a = a % b;
For example:
int age;
age = 5
Here, = is the assignment operator. It assigns the value on its right to the
variable on its left. That is, 5 assigned to the variable age.
RELATIONAL OPERATORS
Operator Description Example
== Is Equal To 3 == 5 returns false
!= Not Equal To 3 != 5 returns true
> Greater Than 3 > 5 returns false
< Less Than 3 < 5 returns true
>= Greater Than or Equal To 3 >= 5 returns false
<= Less Than or Equal To 3 <= 5 returns true
For example:
//check is a is less than b
a<b;
here, > operator is the relational operator. It checks if a is less than b or
not. It returns either true or false.
The operand requires should be a variable that is not constant, as we wouldn’t be able to modify its
value.
THE INCREMENT OPERATOR