Operators
An operator is simply a symbol that is used to perform a specific operation on operands. There are many types of operators in Java as given below:
- Unary Operators
- Arithmetic Operators
- Comparison Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
Unary Operators
- The Java unary operators require only one operand.
- Unary operators are used to perform various operations i.e.
- incrementing / decrementing a value by one
- negating an expression
- inverting the value of a Boolean
Output :
Minus operator: -18
NOT operator: false
Increased but previous value is retained by Post-Increment Operator: 7
Instantly increase by Pre-Increment Operator: 9
Decreased but previous value is retained by Post-Decrement Operator: 18
Instantly decrease by Pre-Decrement Operator: 16
One's Complement by bitwise operator: -17
Note: Post-Increment or Post-Decrement operators first uses the value and then increments or decrements respectively. While Pre-Increment or Pre-Decrement operators first increments or decrements the value, then uses the incremented or decremented value.
Arithmetic Operators
- Arithmetic operators are used to perform mathematical operations such as addition, multiplication etc. on expressions.
- Arithmetic operators cannot work with Booleans.
- Modulus (%) can work on floats and doubles.
Output :
Addition: 10.0
Subtraction: 2.0
Multiplication: 24.0
Division: 1.5
Modulus: 2.0
Increment: 5.0
Decrement: 3.0
Comparison Operators
- Comparison operators are used to compare two operands.
- These operators returns a Boolean value.
Output :
Is a equals to 6: true
Is a equals to 10: false
Is a not equals to 6: false
Is a not equals to 10: true
Is a greater than 2: true
Is a less than 2: false
Is a greater than or equals to 6: true
Is a less than or equals to 2: false
Logical Operators
- These operators determine the logic in an expression containing two or more variables.
- These operators returns a Boolean value and is performed on Boolean expressions.
Output :
For Logical AND...
false AND false = false
false AND true = false
true AND false = false
true AND true = true
For Logical OR...
false OR false = false
false OR true = true
true OR false = true
true OR true = true
For Logical NOT...
NOT(false) = true
NOT(true) = false
Bitwise Operators
- These operators perform the operations on every bit of a number.
Output :
For Bitwise AND...
0 AND 0: 0
0 AND 1: 0
1 AND 0: 0
1 AND 1: 1
For Bitwise OR...
0 OR 0: 0
0 OR 1: 1
1 OR 0: 1
1 OR 1: 1
For Bitwise Ex-OR...
0 Ex-OR 0: 0
0 Ex-OR 1: 1
1 Ex-OR 0: 1
1 Ex-OR 1: 0
Assignment Operators
- These operators assign the result of operation (if any) to the left operand.
Output :
Addition AND Assignment: 10.0
Subtraction AND Assignment: 6.0
Multiplication AND Assignment: 24.0
Division AND Assignment: 6.0
Modulus AND Assignment: 2.0
Precedence and Associativity of Operators in Java
The precedence of operator specifies that which operator will be evaluated first and next.
The associativity specifies the operator direction to be evaluated; it may be left to right or right to left.
Category | Operators | Associativity |
---|---|---|
Postfix | () [] {} | Left to Right |
Multiplicative | * / % | Left to Right |
Additive | + - | Left to Right |
Shift | << >> | Left to Right |
Relational | < <= > >= | Left to Right |
Equality | == != | Left to Right |
Bitwise AND | & | Left to Right |
Bitwise XOR | ^ | Left to Right |
Bitwise OR | | | Left to Right |
Logical AND | && | Left to Right |
Logical OR | || | Left to Right |
Assignment | = += -= *= /= %= >>= <<= &= ^= |= | Right to Left |
The + and - are equal in precedence, as *, /, and %.
The *, /, and % are performed first in order from left to right and then + and -, also in order left to right.
We can change the order of operations by using parenthesis () to indicate which operations are to be performed first.
Comments
Post a Comment