Let’s learn operators in java.
Operators in java
Operator is a symbol used to perform operations on variables. Here are types of operators,
- 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
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. Here are various unary operators.
- Unary minus (-)
- NOT Operator (!)
- Increment (++)
- Decrement (- -)
- Bitwise complement (~)
Here’s an example on ‘NOT’ (!) operator.
public class JavaUnaryOperator { public static void main(String[] args) { boolean bool = true; int a = 14, b = 5; System.out.println("Before using NOT operator(!): " + bool); System.out.println("a = " + a); System.out.println("b = " + b); System.out.println("After using using NOT operator(!): " + !bool); System.out.println("!(a < b) = " + !(a < b)); System.out.println("!(a > b) = " + !(a > b)); } }
Output:
Before using NOT operator(!): true
a = 14
b = 5
After using using NOT operator(!): false
!(a < b) = true !(a > b) = false
Here’s an example on unary minus 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 using unary minus operator(-) : " + a); } }
Output:
Given number : 5
After using unary minus operator(-) : -5
Let’s see an example on unary increment (++) operator.
public class JavaUnaryOperator { public static void main(String[] args) { int a = 7; System.out.println("post-increment = " + a++); System.out.println("pre-increment = " + ++a); } }
Output:
post-increment = 7
pre-increment = 9
Let’s see an example on unary decrement (- -) operator.
public class JavaUnaryOperator { public static void main(String[] args) { int a = 7; System.out.println("post-decrement = " + a--); System.out.println("a = " + a); System.out.println("pre-decrement = " + --a); } }
Output:
post-decrement = 7
a = 6
pre-decrement = 5
Ternary operator in java
Ternary operator in java is an alternative to assigning one of two values to variable depending on given condition. Ternary operator is shorthand for if-else statement.
For example:
int studentAge = 20;
boolean isEighteenOver = studentAge == 20 ? true : false;
In the above statement; operand one – studentAge == 20 in this case is the condition we are checking. It needs to return true or false. Meanwhile operand two – true here is the value to assign to the variable isEighteenOver if the condition above is true.
Finally operand three – false here is the value to assign to the variable isEighteenOver if the condition above was false.
In this case, isEighteenOver is assigned the value true because studentAge has the value 20 and our condition for operand one studentAge is equal to 20 returns true.
Let’s see ternary operator in java example.
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
Bigger number is = 500
Let’s see another example on ternary operator in java.
public class TernaryOperatorDemo { public static void main(String[] args) { int a = 50, b = 700, output; System.out.println("First number: " + a); System.out.println("Second number: " + b); output = (a > b) ? (a + b) : (a - b); System.out.println("Output is = " + output); } }
Output:
First number: 50
Second number: 700
Output is = -650
Operator precedence in java:
Operator | Type | Associativity |
++ — | Unary post-increment(expr++) Unary post-decrement(expr–) | Left to Right |
++ — + – ! ~ | Unary pre-increment(++expr) Unary pre-decrement(–expr) Unary plus Unary minus Unary logical negation Unary bitwise complement | Right to left |
* / % | Multiplication Division Modulus | Left to right |
+ – | Addition Subtraction | Left to right |
<< >> >>> | Bitwise left shift Bitwise right shift with sign extension Bitwise right shift with zero extension | Left to right |
< <= > >= instanceof | Relational less than Relational less than or equal Relational greater than Relational greater than or equal Type comparison (only objects) | Left to right |
= = != | Relational is equal to Relational is not equal to | Left to right |
& | Bitwise AND | Left to right |
^ | Bitwise exclusive OR | Left to right |
| | Bitwise inclusive OR | Left to right |
&& | Logical AND | Left to right |
|| | Logical OR | Left to right |
? : | Ternary conditional | Right to left |
= += -= *= /= %= | Assignment Addition assignment Subtraction assignment Multiplication assignment Division assignment Modulus assignment | Right to left |