Different types of Operators available in Java:
Operator Types | Precedence |
---|---|
Unary Operator | x++ x-- ++x --x ~ ! |
Arithmetic Operator | * / % + - |
Shift Operator | << >> >>> |
Relational Operator | < > <= >= instanceof == != |
Bitwise Operator | & ^ | |
Logical Operator | && || |
Ternary Operator | ? : |
Assignment Operator | = += -= *= /= %= &= ^= |= <<= >>= >>>= |
- Unary Operator: Unary operators require only one operand to perform operations.
Example:
public class Main
{
public static void main(String[] args) {
int x = 10;
int y = 20;
boolean b = false;
System.out.println(x++); //post increment
System.out.println(x--); //post decrement
System.out.println(++x); //pre increment
System.out.println(--x); //pre decrement
System.out.println(~y); //minus of total positive value which starts from 0
System.out.println(!b); //true
}
}
Output:
10 11 11 10 -21 true
- Arithmetic Operator: Arithmetic operators are used to performing addition, subtraction, multiplication, division, and modulo(remainder) operations.
Example:
public class Main
{
public static void main(String[] args) {
int x = 10;
int y = 20;
System.out.println(x+y);
System.out.println(y-x);
System.out.println(x*y);
System.out.println(y/x);
System.out.println(y%x);
}
}
Output:
30 10 200 2 0
- Shift Operator: Shift operators are used to shifting all the bits in a value to the left or right side of a specified number of times based on the type of shift operation like left shift or right shift.
Example:
public class Main
{
public static void main(String[] args) {
int x = 10;
int y = 2;
System.out.println(x<>y); //Right Shift Operator 10/2^2 = 10/4 = 2
}
}
Output:
40 2
- Relational Operator: Relational operators are used to define some kind of relation between two entities. There are two types of relational operators in Java, comparison operator and equality operator.
Example:
public class Main
{
public static void main(String[] args) {
int x = 10;
int y = 5;
int a = 10;
int b = 5;
Main m = new Main();
System.out.println(x < y);
System.out.println(y < x);
System.out.println(y > x);
System.out.println(x > y);
System.out.println(x >= a);
System.out.println(y <= b);
System.out.println(x == a);
System.out.println(x != y);
System.out.println(m instanceof Main);
}
}
Output:
false true false true true true true true true
- Bitwise Operator: There are three types of bitwise operators, bitwise AND (&) operator, bitwise inclusive (|) OR, and bitwise exclusive OR(XOR) (^).
Example:
public class Main
{
public static void main(String[] args) {
int a = 14;
System.out.println("Bitwise AND:"+(a&a));
System.out.println("Bitwise inclusive OR:"+(a|a));
System.out.println("Bitwise exclusive OR(XOR):"+(a^a));
}
}
Output:
Bitwise AND:14 Bitwise inclusive OR:14 Bitwise exclusive OR(XOR):0
- Logical Operator: There are two types of logical operators, logical AND(&&) which return true when both the condition is true, and logical OR(||) which return true even if any one condition is true (it doesn't check the second condition if the first is true) operators.
Example:
public class Main
{
public static void main(String[] args) {
int x = 10;
int y = 20;
int z = 30;
System.out.println(x < y && x < z);
System.out.println(x < y && x > z);
System.out.println(x < y || x > z);
System.out.println(x > y || x < z);
System.out.println(x > y || x > z);
}
}
Output:
true false true true false
- Ternary Operator: Ternary operator in Java can work as a replacement to an if-else statement and is very useful in Java programming. It is called ternary because it takes only three operands.
Example:
public class Main
{
public static void main(String[] args) {
int x = 10;
int y = 20;
int max = x < y ? y:x;
System.out.println("Maximum: "+max);
}
}
Output:
Maximum: 20
- Assignment Operator: Assignment operators are used to assigning the value present on the right side to the operand present on the left side.
Example:
public class Main
{
public static void main(String[] args) {
int x = 10;
System.out.println(x);
x += 5; //x = x+5
System.out.println(x);
x -= 5; //x = x-5
System.out.println(x);
x *= 5; //x = x*5
System.out.println(x);
x /= 5; //x = x/5
System.out.println(x);
x %= 5; //x = x%5
System.out.println(x);
x &= 5; //x = x&5
System.out.println(x);
x |= 5; //x = x | 5
System.out.println(x);
x ^= 5; //x = x^5
System.out.println(x);
x>>=4; //x = x>>4
System.out.println(x);
}
}
Output:
10 15 10 50 10 0 0 5 0 0
Precedence and Associativity in Java.
Precedence and associativity are used when we have to deal with more than one operator in a single equation. Precedence is used to determine the priority of the operators in an equation. If two operators have equal priority in the expression is evaluated through associativity.
Category | Operator | Associativity |
---|---|---|
Postfix | () [] .(dot operator) | Left to Right |
Unary | ++ -- ! ~ | Right to Left |
Multiplicative | * / % | Left to Right |
Additive | + - | Left to Right |
Shift | >> >>> << | Left to Right |
Relational | >>= < <= | Left to Right |
Equality | == != | Left to Right |
Bitwise AND | & | Left to Right |
Bitwise XOR | ^ | Left to Right |
Bitwise OR | | | Left to Right |
Logical AND | && | Left to Right |
Logical OR | || | Left to Right |
Conditional | ?: | Right to Left |
Assignment | = += -= /= %= >= <= &= ^= |= | Right to Left |
Comma | , | Left to Right |
No comments:
Post a Comment