Operator is a symbol that instructs the compiler to perform a specific action. For example, a “+” operator instructs the compiler to perform addition, a “>” operator instructs the compiler to perform comparison, “=” for assignment and so on. In this guide, we will discuss operations in java with the help of examples.
Operator and Operand:
In any operation, there is an operator and operands. For example: In a+b, the “+” symbol is the operator and a & b are operands.
Types of Operator in Java
Operators in java are classified in following eight categories:
1) Arithmetic Operators
2) Assignment Operators
3) Unary Operators
4) Logical Operators
5) Relational operators
6) Bitwise Operators
7) Ternary Operator
8) Shift Operators
1) Arithmetic Operators
Basic arithmetic operators are: +, -, *, /, %
Note: Division (/) operator returns quotient while modulo operator(%) returns remainder. For example 10 % 5 would return 0 while 10/5 would return 2.
Example of Arithmetic Operators
public class JavaExample { public static void main(String args[]) { int num1 = 100; int num2 = 20; System.out.println("num1+num2: " + (num1 + num2) ); System.out.println("num1-num2: " + (num1 - num2) ); System.out.println("num1*num2: " + (num1 * num2) ); System.out.println("num1/num2: " + (num1 / num2) ); System.out.println("num1%num2: " + (num1 % num2) ); } }
Output:
2) Assignment Operators
Assignments operators in java are: =, +=, -=, *=, /=, %=
Example of Assignment Operators
public class JavaExample { public static void main(String args[]) { int num1 = 10, num2; num2 = num1; System.out.println("= Output: "+num2); num2 += num1; System.out.println("+= Output: "+num2); num2 -= num1; System.out.println("-= Output: "+num2); num2 *= num1; System.out.println("*= Output: "+num2); num2 /= num1; System.out.println("/= Output: "+num2); num2 %= num1; System.out.println("%= Output: "+num2); } }
Output:
3) Unary Operators
As the name suggests, The Unary operators in Java involve single operand. Java supports following unary operators:
- Unary minus(-)
- Increment(++)
- Decrement(- -)
- NOT(!)
- Bitwise Complement(~)
num++ is equivalent to num=num+1;
num–- is equivalent to num=num-1;
Example of Unary Operators
public class JavaExample { public static void main(String args[]){ int num1=100; int num2=200; //minus(-) unary operator int inverseNum = -num1; System.out.println("Opposite of num1: "+inverseNum); //increment num1++; //decrement num2--; System.out.println("num1++ is: "+num1); System.out.println("num2-- is: "+num2); } }
Output:
Opposite of num1: -100 num1++ is: 101 num2-- is: 199
The NOT(!) Operator reverses the logical state (true or false) of an operand. If an operand or condition is true, then the Logical NOT operator will make it false and vice-versa.
If value of a boolean variable 'bool' is true, then the value of !bool is false If the value of 'bool' is false, then the value of !bool is true
4) Logical Operators
Logical Operators are used to evaluate the outcome of conditions. There are three logical operators: AND (&&), OR (||) and NOT (!). The AND and OR operators are used when multiple conditions are combined and we need to evaluate the outcome as a whole.
Example of Logical Operators
In this example, we are performing logical operations on boolean variables. However in practical scenarios, these operators are used to combine the multiple conditions (or expressions), which we have covered in the separate tutorial (link is at the end of the following program).
public class LogicalOperatorDemo { public static void main(String args[]) { boolean b1 = true; boolean b2 = false; System.out.println("b1 && b2: " + (b1&&b2)); System.out.println("b1 || b2: " + (b1||b2)); System.out.println("!(b1 && b2): " + !(b1&&b2)); } }
Output:
b1 && b2: false b1 || b2: true !(b1 && b2): true
5) Comparison(Relational) operators
Relational operators are used to compare two operands. In java, we have following relational operators:
Example of Relational operators
Note: This example is using if-else statement which is our next tutorial, if you are finding it difficult to understand then refer if-else in Java.
public class JavaExample { public static void main(String args[]) { int num1 = 10; int num2 = 50; if( num1 != num2 ){ System.out.println("num1 and num2 are not equal"); } else{ System.out.println("num1 and num2 are equal"); } if( num1 > num2 ){ System.out.println("num1 is greater than num2"); } else{ System.out.println("num1 is not greater than num2"); } if( num1 < num2 ){ System.out.println("num1 is less than num2"); } else{ System.out.println("num1 is not less than num2"); } } }
Output:
6) Bitwise Operators
Bitwise operators are used to perform bit-level operations. Let’s say you are performing an AND operation on two numbers (a & b), then these numbers are converted into binary numbers and then the AND operation is performed. Finally, the compiler returns decimal equivalent of the output binary number.
num1 = 11; /* equal to 00001011*/
num2 = 22; /* equal to 00010110 */
Bitwise operator performs bit by bit processing.
num1 & num2 compares corresponding bits of num1 and num2 and generates 1 if both bits are equal, else it returns 0. In our case it would return: 2 which is 00000010 because in the binary form of num1 and num2 only second last bits are matching.
num1 | num2 compares corresponding bits of num1 and num2 and generates 1 if either bit is 1, else it returns 0. In our case it would return 31 which is 00011111
num1 ^ num2 compares corresponding bits of num1 and num2 and generates 1 if they are not equal, else it returns 0. In our example it would return 29 which is equivalent to 00011101
~num1 is a complement operator that just changes the bit from 0 to 1 and 1 to 0. In our example it would return -12 which is signed 8 bit equivalent to 11110100
num1 << 2 is left shift operator that moves the bits to the left, discards the far left bit, and assigns the rightmost bit a value of 0. In our case output is 44 which is equivalent to 00101100
Note: In the example below we are providing 2 at the right side of this shift operator that is the reason bits are moving two places to the left side. We can change this number and bits would be moved by the number of bits specified on the right side of the operator. Same applies to the right side operator.
num1 >> 2 is right shift operator that moves the bits to the right, discards the far right bit, and assigns the leftmost bit a value of 0. In our case output is 2 which is equivalent to 00000010
Example of Bitwise Operators
public class BitwiseOperatorDemo { public static void main(String args[]) { int num1 = 11; /* 11 = 00001011 */ int num2 = 22; /* 22 = 00010110 */ int result = 0; result = num1 & num2; System.out.println("num1 & num2: "+result); result = num1 | num2; System.out.println("num1 | num2: "+result); result = num1 ^ num2; System.out.println("num1 ^ num2: "+result); result = ~num1; System.out.println("~num1: "+result); result = num1 << 2; System.out.println("num1 << 2: "+result); result = num1 >> 2; System.out.println("num1 >> 2: "+result); } }
Output:
num1 & num2: 2 num1 | num2: 31 num1 ^ num2: 29 ~num1: -12 num1 << 2: 44 num1 >> 2: 2
7) Ternary Operator
Ternary operator is the only operator in java that takes three operands. This operator is frequently used as a one line replacement for if…else statement.
Syntax:
variable = Condition ? Expression1: Expression2
If condition is true, the Expression1 executes.
If condition is false, the Expression2 executes.
If the condition returns true, the result of Expression1 is assigned to variable else result of Expression2 is assigned to variable.
Example of Ternary Operator
public class TernaryOperatorDemo { public static void main(String args[]) { int num1, num2; num1 = 25; /* num1 is not equal to 10 that's why * the second value after colon is assigned * to the variable num2 */ num2 = (num1 == 10) ? 100: 200; System.out.println( "num2: "+num2); /* num1 is equal to 25 that's why * the first value is assigned * to the variable num2 */ num2 = (num1 == 25) ? 100: 200; System.out.println( "num2: "+num2); } }
Output:
num2: 200 num2: 100
8) Shift Operators
Shift operators are used to perform bit manipulation. In this guide, we will discuss various shift operators supported in java with the help of examples. Java supports following shift operators:
Shift operator example
public class JavaExample { public static void main(String[] args) { int num = 24; //Left shift System.out.println("num<<2: "+(num<<2)); //Right shift System.out.println("num>>2: "+(num>>2)); } }
Output:
num<<2: 96 num>>2: 6
Operator Precedence in Java
This determines which operator needs to be evaluated first if an expression has more than one operator. Operator with higher precedence at the top and lower precedence at the bottom.
Operators | Precedence |
Unary Operators | ++ – – ! ~ |
Multiplicative | * / % |
Additive | + – |
Shift | << >> >>> |
Relational | > >= < <= |
Equality | == != |
Bitwise AND | & |
Bitwise XOR | ^ |
Bitwise OR | | |
Logical AND | && |
Logical OR | || |
Ternary | ?: |
Assignment | = += -= *= /= %= > >= < <= &= ^= |= |
Conclusion
In this tutorial, we learned various types of Operators in Java with examples. If you want to practice the programs head over to our java programs section. To learn more such in-depth guides and tutorials refer java tutorial for beginners.
Leave a Reply