Operators in Java
Operator in Java is a symbol which is used to perform operations. For example: +, -, *, / etc.
There are many types of operators in Java which are given below:
- Unary Operator,
- Arithmetic Operator,
- Shift Operator,
- Relational Operator,
- Bitwise Operator,
- Logical Operator,
- Ternary Operator and
- Assignment Operator.
Java Operator Precedence
Operator Type | Category | Precedence |
---|---|---|
Unary | postfix | expr++ expr-- |
prefix | ++expr --expr +expr -expr ~ ! | |
Arithmetic | multiplicative | * / % |
additive | + - | |
Shift | shift | << >> >>> |
Relational | comparison | < > <= >= instanceof |
equality | == != | |
Bitwise | bitwise AND | & |
bitwise exclusive OR | ^ | |
bitwise inclusive OR | | | |
Logical | logical AND | && |
logical OR | || | |
Ternary | ternary | ? : |
Assignment | assignment | = += -= *= /= %= &= ^= |= <<= >>= >>>= |
Java Unary Operator
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
Java Unary Operator Example: ++ and --
Output:
10 12 12 10
Java Unary Operator Example 2: ++ and --
Output:
22 21
Java Unary Operator Example: ~ and !
Output:
-11 9 false true