Operators In Java | Java Tutorial
In the previous post you have learned variables and data types. Now lets learn what we can do with them.
Operators in Java are the special symbols that perform specific operations and then return a result.
Types of Operators in Java are
1. Arithmetic Operators
2. Assignment Operators
3. Auto-increment Operator and Auto-decrement Operators
4. Logical Operators
5. Comparison (relational) Operators
6. Bitwise Operators
7. Ternary Operator
Arithmetic Operators:
Arithmetic operators are +, -, *, /, %
+ is Addition
– is Subtraction
* is Multiplication
/ is Division
% is Modulus operator – it returns the remainder of an integer division
package classOperators; public class ArthimeticOperators { public static void main(String[] args) { int num1 = 100; int num2 = 50; System.out.println("Addition of two numbers is "+ (num1 + num2)); System.out.println("Subtraction of two numbers is "+ (num1 - num2)); System.out.println("Multiplication of two numbers is "+ (num1 * num2)); System.out.println("Division of two numbers is "+ (num1 / num2)); System.out.println("Modulus of two numbers is "+ (num1 % num2)); } }
Assignment Operators:
Assignments operators in java are: =, +=, -=, *=, /=, %=
package classOperators; public class AssignmentOperators { public static void main(String[] args) { int num1 = 100; int num2 = 50; // Assigning value of variable num2 to the variable num1 num1 = num2; System.out.println("Output of num1 = num2 is "+ num1); // num1 = num1+num2 num1 += num2; System.out.println("Output of num1 = num1+num2 is "+ num1); // num1 = num1-num2 num1 -= num2; System.out.println("Output of num1 = num1-num2 is "+ num1); // num1 = num1*num2 num1 *= num2; System.out.println("Output of num1 = num1*num2 is "+ num1); // num1 = num1/num2 num1 /= num2; System.out.println("Output of num1 = num1/num2 is "+ num1); // num1 = num1%num2 num1 %= num2; System.out.println("Output of num1 = num1%num2 is "+ num1); } }
Auto-increment and Auto-decrement Operators:
Auto-increment in Java is ++
Auto-decrement in Java is –
package classOperators; public class AutoIncrementDecrementOperators { public static void main(String[] args) { int num1 = 100; int num2 = 50; // num1 = num1 + 1 num1++; // num2 = num2 - 1 num2--; System.out.println("Output of num1 + 1 is "+ num1); System.out.println("Output of num2 - 1 is "+ num2); } }
Logical Operators:
Logical operators in Java are &&, ||, !
Assume we have two boolean variables booleanVal1, booleanVal2
booleanVal1&&booleanVal2:
If booleanVal1 and booleanVal2 are true then output will be true else output will be false
booleanVal1||booleanVal2:
If booleanVal1 and booleanVal2 are false then output will be false else output will be true. Which means one of the value is true then it returns true.
!booleanVal1:
If the value of booleanVal1 is true then it returns false.
package classOperators; public class LogicalOperators { public static void main(String[] args) { boolean booleanVal1 = true; boolean booleanVal2 = false; // && - AND System.out.println("Output of AND is "+ (booleanVal1&&booleanVal2)); // || - OR System.out.println("Output of OR is "+ (booleanVal1||booleanVal2)); // ! - NOT System.out.println("Output of NOT is "+ (!booleanVal1)); } }
Comparison (relational) Operators:
Relational operators in Java are ==, !=, >, <, >=, <=
== equal to – it returns true if both the left side and right side are equal
!= not equal to – it returns true if left side is not equal to the right side of operator
> greater than – it returns true if left side is greater than right
>= greater than or equal to – it returns true if left side is greater than or equal to right side
< less than – it returns true if left side is less than right side
<= less than or equal to – it returns true if left side is less than or equal to right side
package classOperators; public class RelationalOperators { public static void main(String[] args) { int num1 = 100; int num2 = 50; // if num1 is equal to num2 then it returns true System.out.println("num1 == num2 = " + (num1 == num2) ); // if num1 is not equal to num2 then it returns true System.out.println("num1 != num2 = " + (num1 != num2) ); // if num1 is greater than num2 then it returns true System.out.println("num1 > num2 = " + (num1 > num2) ); // if num1 is less than num2 then it returns true System.out.println("num1 < num2 = " + (num1 < num2) ); // if num1 is greater than or equal to num2 then it returns true System.out.println("num1 >= num2 = " + (num1 >= num2) ); // if num1 is less than or equal to num2 then it returns true System.out.println("num1 <= num2 = " + (num1 <= num2) ); } }
Bitwise Operators:
Bitwise Operators in Java are &, |, ^, ~, <<, >>
The bitwise & operator performs a bitwise AND operation.
The bitwise ^ operator performs a bitwise exclusive OR operation.
The bitwise | operator performs a bitwise inclusive OR operation.
The unary bitwise complement operator “~” inverts a bit pattern; it can be applied to any of the integral types, making every “0” a “1” and every “1” a “0”. For example, a byte contains 8 bits; applying this operator to a value whose bit pattern is “00000000” would change its pattern to “11111111”.
The signed left shift operator “<<” shifts a bit pattern to the left, and the signed right shift operator “>>” shifts a bit pattern to the right. The bit pattern is given by the left-hand operand, and the number of positions to shift by the right-hand operand. The unsigned right shift operator “>>>” shifts a zero into the leftmost position, while the leftmost position after “>>” depends on sign extension.
package classOperators; public class BitwiseOperators { public static void main(String[] args) { //64 32 16 8 4 2 1 int num1 = 11; // 0 0 0 1 0 1 1 int num2 = 21; // 0 0 1 0 1 0 1 /*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.*/ // 1 if both bits are equal else 0 // 0 0 0 0 0 0 1 == output will be 1 System.out.println(num1 & num2); /*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*/ // 1 if either bit is 1 else 0 // 0 0 1 1 1 1 1 == output will be 31 System.out.println(num1 | num2); /*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*/ // 1 if both are not equal else 0 // 0 0 1 1 1 1 0 == output will be 30 System.out.println(num1 ^ num2); /*~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*/ // 0 to 1 and 1 to 0 // 1 1 1 0 1 0 0 == output will be 1 System.out.println(~num1); /*As 2's complement of any number we can calculate by inverting all 1s to 0's and vice-versa than we add 1 to it.. Here N= ~N produce results -(N+1) always. Because system store data in form of 2's complement which means it stores ~N like this. ~N = -(~(~N)+1) =-(N+1). */ /*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*/ // 0 1 0 1 0 1 0 == output will be 1 System.out.println(num1 << 2); /*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*/ // 0 0 0 1 0 1 0 == output will be 1 System.out.println(num1 >> 2); } }
Ternary Operators:
Ternary operator evaluates a boolean expression and assign the value based on the result.
Syntax:
Variable num1 = (expression) ? value if expression is true : value if expression is false
package classOperators; public class TernaryOperators { public static void main(String[] args) { int num1 = 100; int num2 = 50; int val1=(num1>num2)?num1:num2; System.out.println(val1); int val2=(num1<num2)?num1:num2; System.out.println(val2); } }
Must Read: Java Tutorial