Java supports a number of operators that allows you to perform various operations on the variables. Below are the commonly used operators are given below-
- Bitwise Operator,
- Logical Operator,
- Ternary Operator and
- Assignment Operator.
- Unary Operator,
- Arithmetic Operator,
- Shift Operator,
- Relational Operator,
These operators have precedence over the other and is defined using below 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 tales 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 that are baisc calculation 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 the operands need to be true, if the first operand is false then 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 then 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 is used to assign the values on the right hand of the operand to the variables on the left hand of the operator. 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); } }
People are also reading: