Java Operators: Comprehensive Guide and Examples
Table of Content:
Operator in Java
Java provides a rich operator environment. An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation. Operators are used in the program to manipulate data and variables. They usually form a part of the mathematical or logical expression.
Java operators can be divided into following categories:
- Arithmetic Operators
- Relational Operators
- Bitwise Operators
- Logical Operators
- Assignment Operators
- conditional operator
- Misc Operators
Arithmetic Operators
- The basic arithmetic operations in Java Programming are addition, subtraction, multiplication, and division.
- Arithmetic Operations are operated on Numeric Data Types as expected.
- Arithmetic Operators can be Overloaded.
- Arithmetic Operators are “Binary” Operators i.e they operate on two operands. These operators are used in mathematical expressions in the same way that they are used in algebra.
Operator | Description | Example |
---|---|---|
+ (Addition) | Adds two operands | 5 + 10 =15 |
- (Subtraction) | Subtract second operands from first. Also used to Concatenate two strings | 10 - 5 =5 |
* (Multiplication) | Multiplies values on either side of the operator. | 10 * 5 =50 |
/ (Division) | Divides left-hand operand by right-hand operand. | 10 / 5 =2 |
% (Modulus) | Divides left-hand operand by right-hand operand and returns remainder. | 5 % 2 =1 |
++ (Increment) | Increases the value of operand by 1. | 2++ gives 3 |
-- (Decrement) | Decreases the value of operand by 1. | 3-- gives 2 |
Relational Operators
- Relational Operators are used to checking relation between two variables or numbers.
- Relational Operators are Binary Operators.
- Relational Operators returns “Boolean” value .i.e it will return true or false.
- Most of the relational operators are used in “If statement” and inside Looping statement in order to check truthiness or falseness of condition.
Operators | Descriptions | Examples |
---|---|---|
== (equal to) | This operator checks the value of two operands, if both are equal, then it returns true otherwise false. | (2 == 3) is not true. |
!= (not equal to) | This operator checks the value of two operands, if both are not equal, then it returns true otherwise false. | (4 != 5) is true. |
> (greater than) | This operator checks the value of two operands, if the left side of the operator is greater, then it returns true otherwise false. | (5 > 56) is not true. |
< (less than) | This operator checks the value of two operands if the left side of the operator is less, then it returns true otherwise false. | (2 < 5) is true. |
>= (greater than or equal to) | This operator checks the value of two operands if the left side of the operator is greater or equal, then it returns true otherwise false. | (12 >= 45) is not true. |
<= (less than or equal to) | This operator checks the value of two operands if the left side of the operator is less or equal, then it returns true otherwise false. | (43 <= 43) is true. |
The Bitwise Operators
Java defines several bitwise operators, which can be applied to the integer types, long, int, short, char, and byte.
Operator | Description | Example |
---|---|---|
& (bitwise and) | Bitwise AND operator give true result if both operands are true. otherwise, it gives a false result. | (R & S) will give 12 which is 0000 1100 |
| (bitwise or) | Bitwise OR operator give true result if any of the operands is true. | (R | S) will give 61 which is 0011 1101 |
^ (bitwise XOR) | Bitwise Exclusive-OR Operator returns a true result if both the operands are different. otherwise, it returns a false result. | (R ^ S) will give 49 which is 0011 0001 |
~ (bitwise compliment) | Bitwise One's Complement Operator is unary Operator and it gives the result as an opposite bit. | (~R ) will give -61 which is 1100 0011 in 2's complement form due to a signed binary number. |
<< (left shift) | Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. | R << 2 will give 240 which is 1111 0000 |
>> (right shift) | Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. | R >> 2 will give 15 which is 1111 |
>>> (zero fill right shift) | Shift right zero fill operator. The left operands value is moved right by the number of bits specified by the right operand and shifted values are filled up with zeros. | R >>>2 will give 15 which is 0000 1111 |
AND gate and truth table:
Bitwise AND Operator and operation in memory:
Example:
class BitwiseAndOperator { public static void main(String[] args){ int A = 10; int B = 3; int Y; Y = A & B; System.out.println(Y); } }Output:
2 Press any key to continue . . .
OR gate and truth table:
Bitwise OR Operator and operation in memory:
Example:
class BitwiseOrOperator { public static void main(String[] args){ int A = 10; int B = 3; int Y; Y = A | B; System.out.println(Y); } }Output:
11 Press any key to continue . . .
XOR gate and truth table:
Bitwise XOR Operator and operation in memory:
Example:
class BitwiseXOROperator { public static void main(String[] args){ int A = 10; int B = 3; int Y; Y = A ^ B; System.out.println(Y); } }Output:
9 Press any key to continue . . .
Now lets see truth table for bitwise &
, |
and ^
A | B | A & B | A | B | A ^ B |
---|---|---|---|---|
0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 0 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 |
Bitwise complement operator
If a = 10
then What will be the output of ~ a
?
a = 10 ( here no sign)
a = +10 (no sing means it is positive)
a = 0 1010 (in binary Sign and Magnitude)
a = 0 1010 (in two’s complement form)
a = 0 1010 (in two’s complement form) b = ~a = 1 0101 ( Here we performed Bitwise Complement operation) Now b = - 5 ( But this is not our Result)
So What Will be the Solution
We have to convert the b = 1 0101 in two’s complement form because here the sign bit is 1 that means it is negative.
b = 1 0101 Now two’s complement of b means (1’s complement 0f 0101 + 1), here no need to consider sign bit.
- 0101 one’s complement result will be 1010
- 1010 +1 = 1011 (two’s complement of b)
- So our final result will be b = 1 1011
- b = 1 1011
- b = - 11
a = -10
then What will be the output of ~ a
?
a = -10 ( Here sign exist ) a = 1 1010 (in binary Sign and Magnitude) a = 1 0110 (in two’s complement form)
a = 1 0110 (in two’s complement form) b = ~ a = 0 1001 ( Here we performed Bitwise Complement operation) Now, b = +9 ( Our Result )
Overview:
Examples of Bitwise OperatorLogical Operators
Java supports following 3 logical operator.Operator | Description | Example |
---|---|---|
&& (logical and) | If both the operands are non-zero, then the condition becomes true. | (0 && 1) is false |
|| (logical or) | If any of the two operands are non-zero, then the condition becomes true. | (0 || 1) is true |
! (logical not) | Logical NOT Operator Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. | !(0 && 1) is true |
The Assignment Operators
Assignment operator supported by Java are as follows
operator | description | example |
---|---|---|
= | assigns values from right side operands to left side operand | A=B |
+= | adds right operand to the left operand and assign the result to left | A+=B is same as A=A+B |
-= | subtracts right operand from the left operand and assign the result to left operand | A-=B is same as A=A-B |
*= | mutiply left operand with the right operand and assign the result to left operand | A*=B is same as A=A*B |
/= | divides left operand with the right operand and assign the result to left operand | A/=B is same as A=A/B |
%= | calculate modulus using two operands and assign the result to left operand | A%=B is same as A=A%B |
<<= | Left shift AND assignment operator. | A <<= 2 is same as A = A<< 2 |
>>= | Right shift AND assignment operator. | A >>= 2 is same as A = A >> 2 |
&= | Bitwise AND assignment operator. | A &= 2 is same as A = A & 2 |
^= | bitwise exclusive OR and assignment operator. | A ^= 2 is same as A = A ^ 2 |
|= | bitwise inclusive OR and assignment operator. | A |= 2 is same as A = A | 2 |
Miscellaneous Operators
There are few other operators supported by Java Language.
conditional Operator / Ternary Operator
Java includes a one of the most special ternary (three-way) operator that can replace certain types of if-then-else statements. This operator is the ?
. It can seem somewhat confusing at first, but the ?
can be
used very effectively once mastered. The ?
has this general form:
Expression1 ? Expression2 : Expression3
Expression ? value if true : value if false
Here, Expression1 can be any expression(a>b) which evaluates to a boolean value (true or false). If Expression1 is
true, then expression2 will work; otherwise, expression3 is evaluated. The result of the ?
operation is that of the expression evaluated. Both Expression2 and Expression3 are required
to return the same type, which can’t be void.
Example of conditional operator
public class ConditionalOperator { public static void main(String args[]) { int a, b; a = 20; b = (a == 1) ? 10: 25; System.out.println( "Value of b is : " + b ); b = (a == 20) ? 20: 30; System.out.println( "Value of b is : " + b ); } }Output
Value of b is : 25 Value of b is : 20 Press any key to continue . . .Examples of Conditional Operator
instanceof Operator
The java instanceof operator is used to test whether the object is an instance of the specified type (class or subclass or interface) i.e fobject reference variables
( Object reference variable ) instanceof (class/interface type)
Example of instanceof operator
public class ConditionalOperator { public static void main(String args[]) { String village = "kusumgram"; // following will return true since village is type of String boolean result = village instanceof String; System.out.println( result ); } }Output
true Press any key to continue . . .Examples of instanceof Operator