Operators are symbols used to perform some actions on operands.
Operators: Indicates what type of operation to perform
Example:
'+' is a operator
Operand: Indicates on which data/variables action will be performed
Example:
2+3, here 2,3 are operands
Types of Operators:
There are different types of operators in Java, In this article, we will be discussing
- Arithmetic operator
- Assignment operator
- Relational operator
- Unary operator
- Logical operator
- Ternary operator
- Bitwise operators
- Instance of operator
Let’s look one by one
Arithmetic operator:
It is used to perform some mathematical operations like Addition, Multiplication, etc.. on operands.
+ → Addition - → Subtraction * → Multiplication / → Division ( Gives quotient ) % → Modulo (Gives remainder)
Code:
Java Code
public class TUF {
public static void main(String[] args) {
//Arithmetic operators
// operands
int x = 20, y = 10;
//Performing Addition operation on operands x,y
System.out.println("Addition: " + (x + y));
//Subtraction
System.out.println("Subtraction: " + (x - y));
//Multiplication
System.out.println("Multiplication: " + (x * y));
//Division
System.out.println("Division: " + (x / y));
//Modulo
System.out.println("Modulo: " + (x % y));
}
}
Output:
Addition: 30
Subtraction: 10
Multiplication: 200
Division: 2
Modulo: 0
Assignment operator:
Used to assign the right-side value to the left-side value/variable.
- x = y, assigning the value of y to x.
- x+=y, it is nothing but x=x+y, first, x+y is performed and then x+y(Right-hand side) will be assigned to x (Left-hand side).
- x-=y → x=x-y, first x-y is performed and then x-y will be assigned to x
- x*=y → x=x*y, first x*y is performed and then x*y will be assigned to x
- x/=y, → x=x/y, first x/y is performed and then x/y will be assigned to x
- x%=y → x=x%y, first x%y is performed and then x%y will be assigned to x
Code:
Java Code
public class TUF {
public static void main(String[] args) {
//Assignment operators
int x = 10;
int y = 2;
//Simple assignment
x = 5;
System.out.println("x after assignment: " + x);
//x+=y --> x = x+y
x += y;
System.out.println("x after performing x+=y: " + x);
//x*=y --> x = x*y
x *= y;
System.out.println("x after performing x*=y: " + x);
}
}
Output:
x after assignment: 5
x after performing x+=y: 7
x after performing x*=y: 14
Relational Operators:
Used to determine the relationship between two operands. Relational operators return either True/False.
Example:
What is the relation between 100 and 200?
100 < 200, Here less than is the relation between both operands and
< is the relational operator
Important Note:
= is used to assign values == is used to check equality between values
- == is equal to → If both the values are equal then the condition will be true
- >= greater than or equal to → If the left-side value is greater than or equal to the right-side value then the condition will be true otherwise false.
- <= less than or equal to → If the left-side value is less than or equal to the right-side value then the condition will be true otherwise false.
- > Greater than → if the left-side value is strictly greater than right side value then the Condition will be true otherwise false
- < Less than → If the left-side value is strictly less than right side value then the Condition will be true otherwise false
- != Not equal → If both values are not equal then the condition becomes true.
Code:
Java Code
public class TUF {
public static void main(String[] args) {
//Relational operators
int x = 10;
int y = 14;
// ==
System.out.println("x equal to y?: " + (x == y)); /* not equal, So false */
// >=
System.out.println("x greater than or equal to y?: " + (x >= y)); //false
// <=
System.out.println("x greater less or equal to y?: " + (x <= y)); //true
// <
System.out.println("x less than y?: " + (x < y)); //true
// !=
System.out.println("x is not equal to y?: " + (x != y)); //true
}
}
Output:
x equal to y?: false
x greater than or equal to y?: false
x greater less or equal to y?: true
x less than y?: true
x is not equal to y?: true
Unary Operator:
It is performed on only one operand, for example ++7, which means incrementing 7 by 1
- – Minus → indicates operand is negative
- + Plus → In java by default the operands are +, so no need to specify plus sign
- ++ Increment → used to increment value by 1, there are 2 types of increment
Post increment (x++): The current value of the operand is used for computing the result and then the value is incremented by 1
Pre increment (++x): The current value of the operand is first incremented by 1, and then used for computing - – – Decrement → used to decrement the value by 1, there are 2 types of decrement
Post decrement (x- -) : The current value of operand is used for computing result and then the value is decrement by 1.
Pre decrement (- -x) : The current value of operand is first decrement by 1, and then used for computing - ! Logical Not → Used to invert boolean value
Code:
Java Code
public class TUF {
public static void main(String[] args) {
//Unary operators
int x = 5;
// -
System.out.println(-x);
// x++
/* First the Current value of x is printed and then x is incremented by 1 */
System.out.println(x++); // x = 5
System.out.println(x); // x = 6
//++x
/* First the Current value of x is incremented and then prints */
System.out.println(++x); // x = 7
// !
/* !true = false
!false = true*/
System.out.println(!true);
}
}
Output:
-5
5
6
7
false
Logical operator:
Used to perform “Logical AND”, “Logical OR” and “Logical NOT” on expressions.
&&, Logical AND, returns true if all the expressions are true ||, Logical OR, returns true if atleast one expression is true !, Logical NOT, returns false if the condition is true and vice versa.
Code:
Java Code
public class TUF {
public static void main(String[] args) {
//Logical operators
int x = 10;
int y = 20;
System.out.println(x != y && x < y && x > y);
//first 2 conditions are true but not 3rd so false
System.out.println(x == y || x < y); // false || true --> true
System.out.println(!(x == y)); // !(false) = true
}
}
Output:
false
true
true
Ternary operator:
Shortened if-else statement
Syntax:
ans = expression ? assign1 : assign2 If expression is true update ans as assign1 else update ans as assign2
Code:
Java Code
public class TUF {
public static void main(String[] args) {
//Ternary operators
int x = 10;
int y = 20;
int ans = x < y ? x : y;
System.out.println(ans);
}
}
output: 10
Bitwise operators:
Used for manipulating data at the bit level, these operators are used to perform manipulation on individual bits of a number.
Refer to this article for a better understanding of Bitwise Operators in Java
Instanceof operator:
To know whether an object is related to a particular class or not, this operator is useful.
Code:
Java Code
public class TUF {
public static void main(String[] args) {
// instanceof operators
Integer x = 5;
// checks if str is an instance of
// the String class
boolean ans = x instanceof Integer;
System.out.println("x is instanceof Integer class ? " + ans);
}
}
Output: x is instanceof Integer class ? true
Special thanks to Sai bargav Nellepalli for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article