Operators
Operators
Operators
● Operators are symbols used to perform specific operations on one or more
operands to produce a result.
● Types of operators in Java:
Arithmetic Operators
Addition +
Additive
Subtraction -
Multiplication *
Multiplicative Division /
Modulus %
Arithmetic Operators (Continued)
● Addition: Adds two variables/numbers of same datatype
○ Example:
System.out.println(2+3); //Output: 5
● Subtraction: Subtracts the second number from the first one
○ Example:
System.out.println(7-3); //Output: 4
● Multiplication: Multiplies the first operand with the second one
○ Example:
System.out.println(3*3); //Output: 9
Arithmetic Operators (Continued)
● Division: Divides the first operand with the second one
○ Example:
System.out.println(9/2); //Output: 4
System.out.println(9.0/2.0); //Output: 4.5
● Modulus: Finds the remainder after dividing the first number with the second
○ Example:
System.out.println(5%2); //Output: 1
System.out.println(7%9); //Output: 7
Assignment Operators
Name Symbol Identicality
Assignment = a = 5;
Addition += a += 3;
Equals a = a+3;
Subtraction a -= 3;
-=
Equals a = a-3;
Multiplication a *= 2;
*=
Equals a = a*2;
Division a /= 2;
/=
Equals a = a/2;
Modulus a %= 2;
%=
Equals a = a%2;
Assignment Operators (Continued)
● (=): Assigns the value on the right to the variable on the left.
○ Example:
int number = 10;
float value = 2.5f;
● (+=): Adds both the operands on its right and left and assigns the addition to the
operand on its left .
○ Example:
int a = 5, b = 10;
b += a;
System.out.println(b); // Output: 15
Assignment Operators (Continued)
● (-=): Subtracts the right operand from the left operand and assigns the result to the left
operand.
○ Example:
int a = 5, b = 10;
b -= a;
System.out.println(b); // Output: 5
● (*=): Multiplies both the operands and assigns the result to the left operand.
○ Example:
int a = 5, b = 10;
b *= a;
System.out.println(b); // Output: 50
Assignment Operators (Continued)
● (/=): Divides the operand on its left with the operand on its right and assigns the result
to the left operand
○ Example:
int a = 5, b = 10;
b /= a;
System.out.println(b); // Output: 2
● (%=): Divides the left operand by the right operand and assigns the remainder to the
operand on the left.
○ Example:
int a = 5, b = 10;
b %= a;
System.out.println(b); // Output: 0
Relational Operators
Equals ==
Equality
Not Equals !=
Relational Operators (Continued)
● (>): Checks if the left operand is greater than the right operand.
○ Example:
● (<): Checks if the left operand is less than the right operand.
○ Example:
● (>=): Checks if the left operand is greater than or equals to the right operand.
○ Example:
● (!=): Checks if the left operand is not equals to the right operand.
○ Example:
Name Symbol
Logical OR ||
Logical NOT !
Logical Operators (Continued)
● (&&): Denotes true if both the conditions before and after are true, otherwise false.
○ Example:
int a = 5, b = 10, c = 20;
System.out.println(a>b && b<c); // Output: false
● (||): Denotes true if at least one conditions before and after are true, otherwise false.
○ Example:
Negation -
Not !
Prefix
Increment ++
Decrement --
Increment ++
Postfix
Decrement --
Unary Operators (Continued)
● Negation (-): Negates an operand
○ Example:
int a = 5;
int b = -a;
System.out.println(b); // Output: -5
int a = 5;
int y = ++a;
System.out.println(a); // Output: 6
System.out.println(y); // Output: 6
int a = 5;
int y = --a;
System.out.println(a); // Output: 4
System.out.println(y); // Output: 4
Unary Operators (Continued)
● Post-Increment (++): The value is assigned first and then increments the operand by 1.
○ Example:
int a = 5;
int y = a++;
System.out.println(y); // Output: 5
System.out.println(a); // Output: 6
● Post-Decrement (--): The value is assigned first and then decrements the operand by 1.
○ Example:
int a = 5;
int y = a--;
System.out.println(y); // Output: 5
System.out.println(a); // Output: 4
THANK YOU!