In this page, we'll learn about different types of operators in Java, their syntax and how to use them with the help of examples.
Operators are symbols that perform operations on variables and values. For example, + is an operator used for addition, - is an operator used for subtraction, while / is also an operator used for division.
Arithmetic operators are used to perform arithmetic operations on variables and data. They act as basic mathematical operations.
Operator | Operation |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulo Operation (Remainder after division) |
class OperatorsTest{
public static void main(String args[]){
int a = 10;
int b = 4;
System.out.println("a + b = " + (a + b)); //addition operator
System.out.println("a - b = " + (a - b)); //subtraction operator
System.out.println("a * b = " + (a * b)); //Multiplication operator
System.out.println("a / b = " + (a / b)); //division operator
System.out.println("a % b = " + (a % b)); //Modulo operator
}
}
Output:
a + b = 14
a - b = 6
a * b = 40
a / b = 2
a % b = 2
In the above example, we have used +, -, *, / and % operators to compute addition, subtraction, multiplication division and Modulo operations.
If we use the division operator with two integers, then the resulting quotient will also be an integer. And, if one of the operands is a floating-point number, we will get the result will also be in floating-point.
The modulo operator % computes the remainder. When a = 10 is divided by b = 4, the remainder is 2.
Note:The % operator is mainly used with integers.
Unary operators are used with only one operand. For example, -- is a unary operator that decreases the value of a variable by 1. That is, --5 will return 4
There are many types of unary operators are:
Operator | Meaning |
---|---|
+ | Unary plus: not necessary to use since numbers are positive without using it |
- | Unary minus: inverts the sign of an expression |
-- | Decrement operator: decrements value by 1 |
++ | Increment operator: increments value by 1 |
! | Logical complement operator: inverts the value of a boolean |
Java also provides increment and decrement operators: ++ and -- respectively. ++ increases the value of the operand by 1, while -- decrease it by 1.
class OperatorsTest{
public static void main(String args[]){
int x = 2;
int y = 6;
int a, b;
System.out.println("Original value of x is: " + x);
a = ++x; //increment operator
System.out.println("After increment: " + a);
System.out.println("Original value of y is: " + y);
b = --y; //decrement operator
System.out.println("After decrement: " + b);
}
}
Output:
Original value of x is: 2
After increment: 3
Original value of y is: 6
After decrement: 5
In the above program, we have used the ++ and -- operator as prefixes (++a, --b). We can also use these operators as postfix (a++, b++).
There is a slight difference when these operators are used as prefix versus when they are used as a postfix.
Java assignment operator is one of the most common operator. It is used to assign the value on its right to the operand on its left.
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; |
class OperatorsTest{
public static void main(String args[]){
int x = 20;
int a;
a = x; //assign value using =
System.out.println("Value of a: " + a);
a -= x; //assign value using -=
System.out.println("Value of a: " + a);
a *= x; //assign value using *=
System.out.println("Value of a: " + a);
}
}
Output:
Value of a: 20
Value of a: 0
Value of a: 0
Relational operators are used to check the relationship between two operands.
Operator | Description |
---|---|
== | Is Equal To |
!= | Not Equal To |
> | Greater Than |
< | Less Than |
>= | Greater Than or Equal To |
<= | Less Than or Equal To |
class OperatorsTest{
public static void main(String args[]){
int a = 10;
int b = 20;
System.out.println("Value of a is: " + a + "and b is: "+ b);
System.out.println(a == b); //using == operator
System.out.println(a != b); //using != operator
System.out.println(a > b); //using > operator
System.out.println(a < b); //using < operator
System.out.println(a >= b); //using >= operator
System.out.println(a <= b); //using <= operator
}
}
Output:
false
true
false
true
false
true
Note: Relational operators are used in decision making and loops.
Bitwise operators in Java are used to perform operations on individual bits.
Operator | Description |
---|---|
~ | Bitwise Complement |
>> | Right Shift |
<< | Left Shift |
>>> | Unsigned Right Shift |
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise exclusive OR |
Logical operators are used to check whether an expression is true or false. They are used in decision making.
Operator | Description |
---|---|
|| | Logical OR |
&& | Logical AND |
! | Logical NOT |
class OperatorsTest{
public static void main(String args[]){
int a = 10;
int b = 6;
int c = 23;
System.out.println((a > b) || (c < a); //using || operator
System.out.println((a < b) || (c > a);
System.out.println((a < b) || (c < a);
System.out.println((a > b) && (c > a); //using && operator
System.out.println((a > b) && (c < a);
System.out.println(a == b); //using ! operator
System.out.println(a > b);
}
}
Output:
true
true
false
true
false
true
false