Operators in java
Operator:-
Operator in Java is a symbol that 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 --
- public class OperatorExample{
- public static void main(String args[]){
- int x=10;
- System.out.println(x++);//10 (11)
- System.out.println(++x);//12
- System.out.println(x--);//12 (11)
- System.out.println(--x);//10
- }}
Output:
10
12
12
10
0 Comments