Operators in java
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 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
Java Arithmetic Operator Example
Output:
15 5 50 2 0
Java Arithmetic Operator Example: Expression
Output:
21
Java Shift Operator Example: Left Shift
Output:
40 80 80 240
Java Shift Operator Example: Right Shift
Output:
2 5 2
Java Shift Operator Example: >> vs >>>
Output:
5 5 -5 1073741819
Java AND Operator Example: Logical && and Bitwise &
The logical && operator doesn't check second condition if first condition is false. It checks second condition only if first one is true.
The bitwise & operator always checks both conditions whether first condition is true or false.
Output:
false false
Java AND Operator Example: Logical && vs Bitwise &
Output:
false 10 false 11
Java OR Operator Example: Logical || and Bitwise |
The logical || operator doesn't check second condition if first condition is true. It checks second condition only if first one is false.
The bitwise | operator always checks both conditions whether first condition is true or false.
Output:
true true true 10 true 11
Java Ternary Operator Example
Output:
2
Another Example:
Output:
5
Java Assignment Operator Example
Output:
14 16
Java Assignment Operator Example
Output:
13 9 18 9
Java Assignment Operator Example: Adding short
Output:
Compile time error
After type cast:
Output:
20
https://welookups.com/java/Java_Basic_Operators.htm
ReplyDelete