Java Operators:
- An operator is used to perform an operation over one or more operands.
- An operator is a character that represents an action, for example + is an arithmetic operator that represents addition.
- Java operators use in within class.
Types of Operator in Java
- Basic Arithmetic Operators
- Assignment Operators
- Auto-increment and Auto-decrement Operators
- Logical Java Operators
- Comparison (relational) operators
- Bitwise Operators
Basic Arithmetic Operators
Basic arithmetic operators in java are: +, -, *, /, %
+ is for addition.
– is for subtraction.
* is for multiplication.
/ is for division.
% is for modulo
Note: Modulo operator returns remainder, for example 10 % 5 would return 0
Example of Arithmetic Operators:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | package package_Java; public class ArithmeticOperator_Java { public static void main(String args[]) { int num1 = 10; int num2 = 5; 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:
1 2 3 4 5 | num1 + num2: 15 num1 - num2: 5 num1 * num2: 50 num1 / num2: 2 num1 % num2: 0 |
Assignment Operators
Assignments java operators in java are: =, +=, -=, *=, /=, %=
num2 = num1 would assign value of variable num1 to the variable.
num2 += num1 is equal to num2 = num2 + num1
num2 -= num1 is equal to num2 = num2 – num1
num2 *= num1 is equal to num2 = num2 * num1
num2 /= num1 is equal to num2 = num2 / num1
num2 %= num1 is equal to num2 = num2 % num1
Example of Assignment Operators:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | package package_Java; public class AssignmentOperator_Java { public static void main(String args[]) { int num1 = 10; int num2 = 20; 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:
1 2 3 4 5 6 | = Output: 10 += Output: 20 -= Output: 10 *= Output: 100 /= Output: 10 %= Output: 0 |
Auto-increment and Auto-decrements Operators
Auto-increment and Auto-decrement java Operators in java are: ++ and —
num ++ is equivalent to num=num+1;
num –- is equivalent to num=num-1;
Example of Auto-increment and Auto-decrement Operators
1 2 3 4 5 6 7 8 9 10 11 12 | package package_Java; public class AutoOperator_Java { public static void main(String args[]) { int num1 = 100; int num2 = 200; num1++; num2--; System.out.println("num1++ is: " + num1); System.out.println("num2-- is: " + num2); } } |
Output:
1 2 | num1++ is: 101 num2-- is: 199 |
Logical Operators
Logical operators in java are: &&, ||, !
- Logical Operators are used with binary variables.
- They are mainly used in conditional statements and loops for evaluating a condition.
- b1 && b2 will return true if both b1 and b2 are true else it would return false.
- b1 || b2 will return false if both b1 or b2 are false else it would return true.
- !b1 would return the opposite of b1, that means it would be true if b1 is false and it would return false if b1 is true.
Example of Logical Operators
1 2 3 4 5 6 7 8 9 10 11 12 | package package_Java; public class LogicalOperator_Java { 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:
1 2 3 | b1 && b2: false b1 || b2: true !(b1 && b2): true |
Comparison(Relational) operators
Relational operators in Java: ==, !=, >, <, >=, <=
== returns true if both the left side and right side are equal
!= returns true if left side is not equal to the right side of operator.
> returns true if left side is greater than right.
< returns true if left side is less than right side.
>= returns true if left side is greater than or equal to right side.
<= returns true if left side is less than or equal to right side.
Example of Relational operators
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | package package_Java; public class RelationalOperator_Java { public static void main(String args[]) { int num1 = 10; int num2 = 50; if (num1 == num2) { System.out.println("num1 and num2 are equal"); } else { System.out.println("num1 and num2 are not equal"); } 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 greater than or equal to num2"); } else { System.out.println("num1 is less than num2"); } if (num1 < num2) { System.out.println("num1 is less than num2"); } else { System.out.println("num1 is not less than num2"); } if (num1 <= num2) { System.out.println("num1 is less than or equal to num2"); } else { System.out.println("num1 is greater than num2"); } } } |
Output:
1 2 3 4 5 6 | num1 and num2 are not equal num1 and num2 are not equal num1 is not greater than num2 num1 is less than num2 num1 is less than num2 num1 is less than or equal to num2 |
Bitwise Operators
Bitwise operators in java: &, |, ^, ~, <<, >>
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise unary NOT
<< Shift left
>> Shift right
Example of Bitwise operators
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | public class Bitwise_Java { public static void main(String args[]) { int a = 60; /* 60 = 0011 1100 */ int b = 13; /* 13 = 0000 1101 */ int c = 0; c = a & b; /* 12 = 0000 1100 */ System.out.println("a & b = " + c); c = a | b; /* 61 = 0011 1101 */ System.out.println("a | b = " + c); c = a ^ b; /* 49 = 0011 0001 */ System.out.println("a ^ b = " + c); c = ~a; /*-61 = 1100 0011 */ System.out.println("~a = " + c); c = a << 2; /* 240 = 1111 0000 */ System.out.println("a << 2 = " + c); c = a >> 2; /* 15 = 1111 */ System.out.println("a >> 2 = " + c); } } |
Output:
1 2 3 4 5 6 | a & b = 12 a | b = 61 a ^ b = 49 ~a = -61 a << 2 = 240 a >> 2 = 15 |