Java supports a number of operators that allows you to perform various operations on the variables.
The following are the commonly used operators:
- Bitwise Operator
- Logical Operator
- Ternary Operator
- Assignment Operator
- Unary Operator
- Arithmetic Operator
- Shift Operator
- Relational Operator
These operators have precedence over the others and are defined using the following table:
Operator Type | Category | Precedence |
Unary | postfix | expr++ expr-- |
prefix | ++expr --expr +expr -expr ~ ! | |
Arithmetic | multiplicative | * / % |
additive | + - | |
Shift | shift | << >> >>> |
Bitwise | bitwise AND | & |
bitwise exclusive OR | ^ | |
bitwise inclusive OR | | | |
Logical | logical AND | && |
logical OR | || | |
Ternary | ternary | ? : |
Assignment | assignment | = += -= *= /= %= &= ^= |= <<= >>= >>>= |
Java Operators
Below are the examples of the Java Operators to explain their working -
1. Java Unary Operator
This operator takes only one operand to perform various operations.
Example
class OperatorExample{
public static void main(String args[]){
int x=10;
System.out.println(x++);//10 (11)
System.out.println(++x);//12
System.out.println(x--);//12 (11)
System.out.println(--x);//10
int b=10;
System.out.println(x++ + ++x);//10+12=22
System.out.println(b++ + b++);//10+11=21
boolean c=true;
boolean d=false;
System.out.println(~b);//20 (positive of total minus, positive starts from 0)
System.out.println(!c);
System.out.println(!d);//true
}}
2. Java Arithmetic Operator
This operator will allow you to perform operations like addition, subtraction, multiplication, and division, which are basic calculations performed on the number variables.
Example
class demo{
public static void main(String args[]){
int a=2;
int b=6;
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/b);
System.out.println(a%b);
}}
3. Java Left Shift Operator
This operator will shift the bits in the value to the left side, which is done in a specified number of times given.
Example
class OperatorExample{
public static void main(String args[]){
System.out.println(10<<2);//10*2^2=10*4=40
System.out.println(10<<3);//10*2^3=10*8=80
}}
4. Java Right Shift Operator
This operator will shift the value of the left operand to the right by the specified number of times mentioned in the right operand.
Example
class OperatorExample{
public static void main(String args[]){
System.out.println(10>>2);//10/2^2=10/4=2
System.out.println(20>>2);//20/2^2=20/4=5
}}
5. Java AND Operator
For this operand, both operands need to be true. If the first operand is false, it will not check the second operand.
Example
class OperatorExample{
public static void main(String args[]){
int a=10;
int b=5;
int c=20;
System.out.println(a<b&&a<c);//false && true = false
System.out.println(a<b&&a++<c);//false && true = false
System.out.println(a);//10 because second condition is not checked
}}
6. Java OR Operator
For this operator, any of the operands should be true. If the first operand is true, it will not check for the second operand.
Example
class OperatorExample{
public static void main(String args[]){
int a=10;
int b=5;
int c=20;
System.out.println(a>b||a<c);//true || true = true
System.out.println(a>b|a<c);//true | true = true
}}
7. Java Ternary Operator
Java Ternary operator is the short notation for the if-then-else statement and is commonly used in Java programming. This operator takes three operands.
Example
class demo{
public static void main(String args[]){
int a=7;
int b=9;
int min=(a<b)?a:b;
System.out.println(min);
}}
8. Java Assignment Operator
This operator assigns the values on the right hand of the operand to the variables on the operator's left hand.
Example
class OperatorExample{
public static void main(String args[]){
int a=10;
int b=20;
a+=2;
b-=3;
System.out.println(a);
System.out.println(b);
}
}