Let’s learn operators in java.
Operators in java
Operator is symbol to perform operations. Here are types of operators in java,
- Arithmetic operators in java
- Unary operators in java
- Logical operator
- Shift operator
- Relational operators in java
- Bitwise operators in java
- Ternary operator in java
- Assignment operator
Arithmetic operators in java
Arithmetic operator is used in mathematical expressions to perform division, addition, subtraction and multiplication.
public class ArithmeticOperatorDemo { public static void main(String[] args) { int i = 25; int j = 5; System.out.println(i + j); System.out.println(i - j); System.out.println(i * j); System.out.println(i / j); System.out.println(i % j); } }
Output:
30
20
125
5
0
Unary operator in java
Unary operator needs only one operand. Using unary operator we can increment, decrement and many more.
Also read – java overview
Unary operators includes logical, arithmetic and other operators that use only single or one operand.
Here are different unary operators,
- Not Operator (!): reverses logical state of operand. Converts false to true and vice versa.
- unary minus (-): converts negative value to positive value.
- increment (++): increments value of an integer. post and pre-increment.
- decrement (–): decrements value of an integer. post and pre-decrement.
- Bitwise complement (~): returns one’s complement. That is it makes every 0 to 1 and every 1 to 0.
Let’s see each java unary operator with example.
Here’s an example on java unary NOT (!) operator.
public class JavaUnaryOperator { public static void main(String[] args) { boolean bool = true; int a = 14, b = 5; System.out.println("Before using java unary ! operator: " + bool); System.out.println("a = " + a); System.out.println("b = " + b); System.out.println("After using java unary ! operator: " + !bool); System.out.println("!(a < b) = " + !(a < b)); System.out.println("!(a > b) = " + !(a > b)); } }
Output:
Before using java unary ! operator: true
a = 14
b = 5
After using java unary ! operator: false
!(a < b) = true
!(a > b) = false
Here’s an example on java unary (-) operator.
public class JavaUnaryOperator { public static void main(String[] args) { int a = 5; System.out.println("Given number = " + a); a = -a; System.out.println("After java unary - operator = " + a); } }
Output:
Given number = 5
After java unary – operator = -5
Increment (++) operator can be used in two ways post-increment and pre-increment.
Post-increment operator (a++): Here value of operand is increased by 1 and previous value is retained (temporary).
Example:
a = 13
a++ = 14
Pre-increment operator (++a):
Here pre-increment operator is placed before variable name and value is increased by 1 instantaneously.
Example:
a = 13
++a = 14
Let’s see an example on java unary increment (++) operator.
public class JavaUnaryOperator { public static void main(String[] args) { int a = 23; // 23 gets printed and increment to 24 System.out.println("Java post-increment = " + a++); // a value was 24, incremented to 25 System.out.println("Java pre-increment = " + ++a); } }
Output:
Java post-increment = 23
Java pre-increment = 25
Decrement (–): operator can be used in two ways post-decrement and pre-decrement.
Post-decrement operator (a–): Here value of operand is decreased by 1 and previous value is retained (temporary).
Pre-decrement operator (–a): Here pre-decrement operator is placed before variable name and value is decreased by 1 instantaneously.
Let’s see an example on java unary decrement (–) operator.
public class JavaUnaryOperator { public static void main(String[] args) { int a = 23; // 23 gets printed and decremented to 22 System.out.println("Java post-decrement = " + a--); System.out.println("a = " + a); // a value was 22, decremented to 21 System.out.println("Java pre-decrement = " + --a); } }
Output:
Java post-decrement = 23
a = 22
Java pre-decrement = 21
Let’s see an example on java unary (~) operator.
public class JavaUnaryOperator { public static void main(String[] args) { int a = 8, b = -4; System.out.println("a = " + a); System.out.println("b = " + b); System.out.println(a + " java bitwise complement is = " + ~a); System.out.println(b + " java bitwise complement ia = " + ~b); } }
Output:
a = 8
b = -4
8 java bitwise complement is = -9
-4 java bitwise complement ia = 3
Logical operator in java
Logical operator helps us understand the result value of two operands X and Y for making a decision. Below are logical operators,
- && – logical AND
- || – logical OR
- ! logical NOT
Now let’s see logical operators with examples,
Logical AND(&&) operator
If both operands holds non-zero then result will be true and if even one operand is false then the result is false.
Logical OR(||)
If any of the operands holds non-zero then result will be true and if even one operand is true then the result is true.
Logical NOT(!)
Logical NOT(!) operator reverses the result. For example, returns true if the condition is false and returns false if condition is true.
public class LogicalOperatorDemo { public static void main(String[] args) { System.out.println("Logical AND Operator: "); int num1 = 20; int num2 = 30; int num3 = 40; boolean output1 = num2 > num1 && num2 < num3; System.out.println(output1); boolean output2 = num2 < num1 && num2 < num3; System.out.println(output1); boolean output3 = num2 > num1 && num2 > num3; System.out.println(output3); boolean output4 = num2 < num1 && num2 > num3; System.out.println(output4); System.out.println("Logical OR Operator: "); output1 = num2 > num1 || num2 < num3; System.out.println(output1); output2 = num2 < num1 || num2 < num3; System.out.println(output2); output3 = num2 > num1 || num2 > num3; System.out.println(output3); output4 = num2 < num1 || num2 > num3; System.out.println(output4); System.out.println("Logical NOT Operator: "); boolean temp = false ; boolean temp1 = !temp; System.out.println("Temp: " + temp); System.out.println("Logical not: " + temp1); int n1 = 30; int n2 = 45; int n3 = 50; boolean result1 = n1 < n2; boolean result2 = !(n1 < n2); System.out.println(result1); System.out.println(result2); boolean result3 = n1 < n2 && n2 < n3; boolean result4 = !(n1 < n2 && n2 < n3); System.out.println(result3); System.out.println(result4); } }
Output:
Logical AND Operator:
true
true
false
false
Logical OR Operator:
true
true
true
false
Logical NOT Operator:
Temp: false
Logical not: true
true
false
true
false
Shift operator in java
- left shift operator (<<)
- signed right shift operator (>>)
- Unsigned Right shift operator (>>>)
- Unsigned Left shift operator (<<<)
left shift operator (<<)
In << left shift operator bits of the number is shifted to the left and fills 0 on void places.
public class LeftShiftOperatorDemo { public static void main(String[] args) { int a = 6; int b = -20; System.out.println(b << 2); System.out.println(a << 2); } }
Output:
-80
24
>> signed right shift operator
In >> right shift operator bits of the number is shifted to right and fills 0 on void places.
public class RightShiftOperatorDemo { public static void main(String[] args) { int a = 6; int b = -20; System.out.println(b >> 1); System.out.println(a >> 1); } }
Output:
-10
3
Unsigned Right shift operator (>>>)
In unsigned right shift operator (>>>) bits of the number is shifted to the right and fills 0 on void.
Here leftmost bit is set to zero and will extend sign bit.
public class UnsignedRightShiftDemo { public static void main(String[] args) { int a = 6; int b = -20; System.out.println(b >>> 1); System.out.println(a >>> 1); } }
Output:
2147483638
3
Unsigned Left shift operator (<<<): there is no unsigned left shift operator (<<<) in java.
Because logical << and <<< left shift operation are same. Let’s see an example.
public class LeftShiftOperatorDemo { public static void main(String[] args) { int a = 6; int b = -20; System.out.println("a << 2 = " + (a << 2)); System.out.println("b >> 2 = " + (b >> 2)); System.out.println("b >>> 2 = " + (b >>> 2)); } }
Output:
a << 2 = 24
b >> 2 = -5
b >>> 2 = 1073741819
Relational operator in java
Relational operators give us the ability to compare two operands for equality, greater than, less than etc., and return boolean output. There are six relational operators,
- equal to (==): check whether two given operands are equal or not.
- less than (<): check whether first operand is less than second operand.
- greater than (>): check whether first operand is greater than second operand.
- not equal to (!=): function of this operator is opposite of equal-to operator.
- greater than or equal to (≥): checks whether first operand is greater than or equal to second operator or not.
- less than or equal to (≤): checks whether first operand is lesser than or equal to second operator or not.
The output of relational operator is boolean value, that is, true or false.
public class RelationalOperatorDemo { public static void main(String[] args) { boolean bool1 = 10 < 6; System.out.println(bool1); boolean bool2 = 10 > 6; System.out.println(bool2); boolean bool3 = 23.5 != 15; System.out.println(bool3); boolean bool4 = 14 * 5.5 >= 67.0 - 42; System.out.println(bool4); boolean bool5 = 6.8 * 54 <= 654; System.out.println(bool5); boolean bool6 = 5 * 6 == 6 * 9; System.out.println(bool6); boolean bool7 = 3 * 2 <= 2 * 6; System.out.println(bool7); boolean bool8 = 2 * 6 < 6 * 6; System.out.println(bool8); } }
Output:
false
true
true
true
true
false
true
true
instanceof operator
This is an operator through which we can test whether an object is an instance of class type, subclass type or interface type at runtime.
instanceof operator is also known for casting object at runtime.
It returns boolean value “true” if reference is of specified type, else returns “false”.
class Demo { public static void main(String[] args) { Demo obj = new Demo(); System.out.println(obj instanceof Demo); } }
Output:
true
Bitwise operators in java
Bitwise operator works on bits and executes bit by bit of a number.
Bitwise operator can be used on datatypes like int, short, char etc.
Bitwise operator is similar to logic gate. Here are bitwise operation java operators.
- Bitwise AND (&) – if both bits are 1, it gives 1, else 0.
- Bitwise OR (|) – if either of the bits is 1, it gives 1, else 0.
- Bitwise XOR (^) – if corresponding bits are different, it gives 1, else 0.
- Bitwise COMPLIMENT(~) – return’s complement, that is, it makes every 0 to 1, and every 1 to 0.
public class BitwiseOperatorDemo { public static void main(String[] args) { int x = 5; int y = 7; // 0101 & 0111 is 0101 = 5 in decimal System.out.println("x & y : " + (x & y)); // 0101 | 0111 is 0111 = 7 in decimal System.out.println("x | y : " + (x | y)); // 0101 ^ 0111 is 0010 = 2 in decimal System.out.println("x ^ y : " + (x ^ y)); // ~0101 compliment is 1010 = -6 System.out.println("~x : " + (~x)); } }
Output:
x & y : 5
x | y : 7
x ^ y : 2
~x : -6
Ternary operator in java
Ternary operator (conditional operator) decides which value must be assigned to variable based on condition or expression.
This operator is shorthand for if-then-else statement.
Syntax:
variable = (condition/expression1) ? expression2 if true : expression3 if false
Let’s see an example on ternary operator in java.
public class TernaryOperatorDemo { public static void main(String[] args) { int a = 50, b = 500, bigger; System.out.println("First number: " + a); System.out.println("Second number: " + b); bigger = (a > b) ? a : b; System.out.println("Bigger number is = " + bigger); } }
Output:
First number: 50
Second number: 500
Maximum is = 500
Assignment operators in java
Assignment operators in java are used to assign some value to variable. It has a combination of left and right.
That is, value on left hand side of assignment operator is assigned to variable on right hence left hand side value should be declared before using it.
Here is the list of assignment operators,
- (+=) : add left operand with right operand and assign it to variable on left. For example, a+= 1 is equal to a = a + 1.
- (-=) : subtract left operand with right operand and assign it to variable on left. For example, a-= 1 is equal to a = a – 1.
- (*=) : multiply left operand with right operand and assign it to variable on left. For example, a*= 1 is equal to a = a * 1.
- (/=) : divide left operand with right operand and assign it to variable on left. For example, a/= 1 is equal to a = a / 1.
- (%=) : assign modulo of left operand with right operand then assign to variable on left. For example, a%= 1 is equal to a = a % 1.
Let’s see java assignment operator with examples.
public class AssignmentOperatorDemo { public static void main(String[] args) { int numOne = 60; int numTwo = 30; numTwo += numOne; System.out.println("(+=) : " + numTwo); numTwo -= numOne; System.out.println("(-=) : " + numTwo); numTwo *= numOne; System.out.println("(*=) : " + numTwo); numTwo /= numOne; System.out.println("(/=) : " + numTwo); numTwo %= numOne; System.out.println("(%=) : " + numTwo); } }
Output:
(+=) : 90
(-=) : 30
(*=) : 1800
(/=) : 30
(%=) : 30
Now let’s see precedence of above discussed operators in java.
