You are on page 1of 6

JAVA OPERATORS

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 INCREMENT AND DECREMENT OPERATOR


Increment and decrement operators are unary operators. The java unary operators require only one
operand.

The operand requires should be a variable that is not constant, as we wouldn’t be able to modify its
value.
THE INCREMENT OPERATOR

INCREMENT OPERATOR Can be placed before the variable (pre-


Used to increase the value of variable by 1. increment).
++ Ex: ++i
Increments the value of i by 1 and uses the
new value in the statement

Can be placed after the variable (post-


increment).
Ex: i++
Increments the value of i by 1 and uses the
Original value in the statement
THE DECREMENT OPERATOR

DECREMENT OPERATOR Can be placed before the variable (pre-


Used to decrease the value of variable by 1. decrement).
++ Ex: ++i
decrements the value of i by 1 and uses the
new value in the statement

Can be placed after the variable (post-


decrement).
Ex: i++
decrements the value of i by 1 and uses the
Original value in the statement
LOGICAL OPERATORS
Used to construct complex condition.
These operators are used between Boolean values, and the result is also Boolean value.

AND Operator && condition_1 && condition_2 &&


condition_n
→ True only if all conditions are true
OR Operator || condition_1 || condition_2 || condition_n
→ True if at least one conditions is true
NOT Operator ! → negates a Boolean value
!true = false
!false = true
Examples:

You might also like