Operators | Operations | Functionality |
+ | Addition | Add numbers |
– | Subtraction | Subtract number |
/ | Division | Divide number |
x | Multiplication | Multiply number |
% | Modulus | Get Remainder from division operation |
++ | Increment | Increment a number by 1 |
— | Decrement | Decrement a number by 1 |
+= | Addition Assignment | Add a number to a number at left side of assignment operator |
-= | Subtraction Assignment | Subtract a number to a number at left side of assignment operator |
/= | Division Assignment | Divide a number to a number at left side of assignment operator |
x= | Multiplication Assignment | Multiply a number to a number at left side of assignment operator |
%= | Modulus Assignment | Find remainder of division operation with a number to a number at left side of assignment operator |
public class ArithmeticOperator { public static void main(String[] args) { int a = 10; int b = 5; System.out.println(" Addition = " + (a + b)); System.out.println(" Subtraction = " + (a - b)); System.out.println(" Division = " + (a / b)); System.out.println(" Multiplication = " + (a * b)); System.out.println(" Remainder = " + (a % b)); a++; System.out.println(" Increment = " + a); a--; System.out.println(" Decrement = " + a); System.out.println(" Addition assignment = " + (a += 6)); System.out.println(" Subtraction assignment = " + (a -= 6)); System.out.println(" Division assignment = " + (a /= 2)); System.out.println(" Multiplication assignment = " + (a *= 2)); System.out.println(" Remainder assignment = " + (a %= 5)); } }
Addition = 15 Subtraction = 5 Division = 2 Multiplication = 50 Remainder = 0 Increment = 11 Decrement = 10 Addition assignment = 16 Subtraction assignment = 10 Division assignment = 5 Multiplication assignment = 10 Remainder assignment = 0
Operator | Operations | Functionality | Example |
& | Bitwise AND | It performs AND operations among binary number. It is represented by & | 4 = 100 5= 101 4 :- 100 5 :-. &101 Output :- 100 |
| | Bitwise OR | It performs OR operations among binary number. It is represented by | | 4 = 100 5= 101 4 :- 100 5 :-. |101 Output :- 101 |
~ | Bitwise Unary not | It performs ones complement on binary number. It is represented by tilt sign (~) | 4 = 0000 …….100 4 :- 0000 …….100 ~ 1st complement = 0000….100 + 1 0000….101 Output = -5 |
^ | Bitwise exclusive OR | It performs exclusive OR operations on binary number. It is represented by ^ | 4 = 100 5= 101 4 :- 100 5 :-. ^101 Output :- 001 |
>> | Shift right | It shifts bits of binary number to the right as per the given bits that represents how much to shift | 4 = 00100 4 :- 00100 >> 1bit Output :- 00010 |
<< | Shift left | It shifts bits of binary number to the left as per the given bits that represents how much to shift | 4 = 00100 4 :- 00100 << 1bit Output :- 01000 |
>>> | Shift right Zero fill | It fills 0’s in a binary number from left in order to make a number positive or unsigned | 4 = 00100 4 :- 00100 >>> 1bit Output :- 00010 |
&= | Bitwise AND Assignment | Perform AND operation to a number with the number left side of assignment operator | Similar AND |
|= | Bitwise OR Assignment | Perform OR operation to a number with the number left side of assignment operator | Similar OR |
>>= | Shift right Assignment | Perform right shift operation mentioned at right side on a number at left side of assignment operator | Similar Shift right |
>>>= | Shift right Zero fill Assignment | Perform unsigned right shift operation and fills 0’s in most significant bits mentioned at right side on a given number at left side of assignment operator | Similar Shift right Zero fill |
<<= | Shift left assignment | Perform left shift operation mentioned at right side on a number at left side of assignment operator | Similar Shift left |
public class BitwiseOperator { public static void main(String[] args) { int a = 4; int b = 5; System.out.println(" AND = " + (a & b)); System.out.println(" OR = " + (a | b)); System.out.println(" Complement = " + (~a)); System.out.println(" XOR = " + (a ^ b)); System.out.println(" Right Shift = " + (a >> 1)); a = 4; System.out.println(" Left Shift = " + (a << 1)); a = 4; System.out.println(" Unsigned Right Shift = " + (a >>> 1)); } }
AND = 4 OR = 5 Complement = -5 XOR = 1 Right Shift = 2 Left Shift = 8 Unsigned Right Shift = 2
Operator | Operations |
== | Equality check |
!= | Inequality check |
>= | Greater than equal to check |
<= | Smaller than equal to check |
> | Greater than to check |
< | Smaller than to check |
public class RelationalOperator { public static void main(String[] args) { int x = 0; boolean result; result = x==0; System.out.println("Equality Operator = "+result); result = x!=1; System.out.println("Inequality Operator = "+result); result = x>=0; System.out.println("Greater than equal to Operator = "+result); result = x<=0; System.out.println("Smaller than equal to Operator = "+result); result = x<1; System.out.println("Smaller Operator = "+result); result = x>-1; System.out.println("Greater Operator = "+result); } }
Equality Operator = true Inequality Operator = true Greater than equal to Operator = true Smaller than equal to Operator = true Smaller Operator = true Greater Operator = true
Operator | Operations | Functionality |
& | Logical AND | If condition A and B are evaluated as true then only it returns true |
| | Logical OR | Either A or B is evaluated as true then it would return true |
^ | Logical XOR ( Exclusive OR) | If condition A evaluates different from B them it would return true. In other words, if A condition returns true then B should be false in order to evaluate true |
|| | Short-circuit OR | It only evaluates first or left expression, if it returns true then it would return true, without evaluating second or right expression |
?: | Ternary if then else | If a condition is evaluated as true, then it would return first statement followed by ? Symbol. Otherwise, second statement would be returned |
&& | Short-circuit AND | It evaluates first expression, if it returns true then it would evaluate second condition if it returns true, then final outcome will be true |
! | Logical Unary not | If condition A is evaluated as false then it would return true |
public class LogicalOperator { public static void main(String[] args) { int x = 10, y = 20; System.out.println("AND operator = " + ((x > 9) & (y > 9))); // 10>9 & 20>9, since both are true result is true System.out.println("OR operator = " + ((x < 9) | (y > 9))); // 10<9 | 20>9, since left one is true result is true System.out.println("XOR operator = " + ((x < 9) ^ (y > 9))); // 10<9 ^ 20>9, since both are different one is false and other one is true result is true System.out.println("Short-Circuit AND operator = " + ((x < 9) && (y > 9))); //10<9 && 20>9, only left one is evaluated which is false System.out.println("Short-Circuit OR operator = " + ((x < 9) && (y > 9))); //10<9 && 20>9, only left one is evaluated which is false System.out.println("Ternary Operator AND operator = " + ((x > y) ? 1 : 0)); //10>20? 1:0, expression before ? is false, which would return second statement after ? that is 0 System.out.println("Not Equal operator = " + (!(x > y))); // 10>20, since it is false and when this evaluated false again complemented it returns true } }
Output:
AND operator = true OR operator = true XOR operator = true Short-Circuit AND operator = false Short-Circuit OR operator = false Ternary Operator AND operator = 0 Not Equal operator = true
int a =10 ;
Operators | Operations | Example |
+ | Unary Plus provides positive value for the arithmetic operand | +x |
– | Unary Minus provides negative value for the arithmetic operand | -x |
++ | Increment Operator increases the operand value by 1 | x= x+1 (pre-increment) : ++x; (post-increment) : x++; |
— | Decrement Operator decreases the operand’s value by 1 | x=x-1 (pre-decrement) : —x; (post-decrement) : x–; |
! | Logical Unary Not negates inverse the operand’s logical value | !x |
~ | Bitwise Unary Not performs one’s complement on binary number | ~x |
public class UnaryOperator { public static void main(String[] args) { int x = 4; int result; result= -x; System.out.println("Unary Minus operator :: "+ result); result = +x; System.out.println("Unary Plus operator :: "+ result); System.out.println("Increment operator :: "+ ++x); System.out.println("Decrement operator :: "+ --x); System.out.println("Logical Not operator :: "+ !(x < 10)); System.out.println("Bitwise Not operator :: "+ ~(x)); } }
Output:
Unary Minus operator :: -4 Unary Plus operator :: 4 Increment operator :: 5 Decrement operator :: 4 Logical Not operator :: false Bitwise Not operator :: -5
expression1 ? statement 1 : statement 2
public class TernaryOperator { public static void main(String[] args) { Scanner inputReader = new Scanner(System.in); System.out.print("Please enter your age : "); int voterAge = inputReader.nextInt(); String msg = voterAge < 18 ? "you are not eligible" : "You can vote"; System.out.println(msg); } }
public class InstanceOfOperator { public static void main(String[] args) { Integer x = 10; if(x instanceof Number){ System.out.println("Integer is Subclass of Number class"); } } }
Output :
Integer is Subclass of Number class
This post was last modified on July 4, 2022