Operators Summary
Arithmetic Operators
-
Result of Arithmetic Expression is always int type or wider than int type.
-
Result of Arithmetic Expression will not be byte, char or short type.
-
When you want to use two same Unary Arithmetic Operators continuously then you must separate those two operators with parenthesis or space.
-
When you use two same Unary Arithmetic Operators continuously without parenthesis or space then it will be considered as INCREMENT or DECREMENT operator.
-
When you want to use two different Unary Arithmetic Operators continuously then parenthesis or space is not required.
-
When char type operand is used in arithmetic expression then ASCII value will be used while performing the operation.
-
int value can be stored in byte, char or short type variable if it satisfies following rules: a) It must be final b) It must be initialized in the same statement c) It must be in the range of corresponding data type
-
When the operands are int or lower than int type then result will be int type only.
-
When any one operand or both operands are wider than int type then result will be wider type.
-
long value can be stored in float type variable because floating point data type stores the value in exponential representation.
-
long or wider than long can't be assigned to lower type variable even if it is final.
-
Floating Point Expressions (Infinity & NaN)
Following are some expressions having floating point operands which result in Infinity, -Infinity or NaN (Not a Number):
Infinity Cases
a) <numericValue> / 0.0 → Infinity
b) <float ingPointValue> / 0 → Infinity
c) <numericValue> / -0.0 → -Infinity
d) <negativeNumericValue> / 0.0 → -Infinity
e) <negativeFloatingPointValue> / 0 → -Infinity
NaN Cases
f) 0.0 / 0.0 → NaN
g) 0 / 0.0 → NaN
h) -0.0 / 0.0 → NaN
i) 0.0 / 0 → NaN
-
When you divide any integer value with 0, then it throws java.lang.ArithmeticException at runtime.
-
Infinity concept is not available with integral types because their range is finite.
-
Infinity concept is available with floating types because their range is near to infinity.
-
Operator Precedence (Example)
7 + (6 + 5 * 3) - 4 / 2
= 7 + (6 + 15) - 4 / 2
= 7 + 21 - 4 / 2
= 7 + 21 - 2
= 28 - 2
= 26
- Precedence of Arithmetic Operators
- Unary:
+exp,exp - Multiplicative: ,
/,% - Additive:
+,
Assignment Operators
-
When multiple operators of same precedence are found in an expression, they are evaluated from left to right.
-
The variable, value or result of an expression must be assigned to a compatible type.
Type Hierarchy
byte → short → int → long → float → double
↘
char → int
20.Type casting is possible only with compatible data types.
- Cases where type casting is NOT possible
a) boolean b = (boolean)1;
b) String str = (String)99;
etc.
- In the case of explicit casting
- Right side bits will be used as per the number of bits required for new data type.
- Remaining all bits will be discarded.
Example
int a = -1; → int uses 32 bits
Binary (32-bit):
11111111 11111111 11111111 11111111
char ch = (char) a; → char uses 16 bits
Binary (16-bit):
11111111 11111111
Value → 65535
int x = ch; → int uses 32 bits
Binary (32-bit):
00000000 00000000 11111111 11111111
Value → 65535
- Priority of explicit casting is higher than other operators.
Increment / Decrement Operators
int a = 10;
a++;
int temp = a;
a = a + 1;
int a = 10;
int b = a++;
int temp = a;
a = a + 1;
int b = temp;
int a = 10;
int b = ++a;
a = a + 1;
int b = a;
int a = 10;
a = a++;
int temp = a;
a = a + 1;
a = temp;
int a = 10;
int b = a++ + ++a + a--;
- Left to Right (VALID) → 10 + 12 + 12 = 34
- Right to Left (INVALID) → 10 + 10 + 10 = 30
int a = 12;
a += a++;
b = a + a++;
byte b = 90;
a)
b = b + 1; // INVALID
b)
b += 1; // VALID
b = (byte)(b + 1);
c)
b++; // VALID
b = (byte)(b + 1);
Relational Operators
-
When you are using Relational Operators with the primitive type operands then the value will be compared, not the data types.
-
Operands of Comparison Operators must be numeric or char type.
-
Operands of Equality Operators can be numeric, char, boolean or reference type.
-
When you are comparing reference type using Equality operator then following checks will happen:
- a) Compiler verifies that the types are comparable or not (inheritance relationship present between types or not).
- b) If it is comparable then JVM compares the address/reference available in the reference variables.
- c) It won't compare the actual data available in the objects.
-
If you are comparing any value with NaN using
==operator then the result will always be false. -
If you are comparing any value with NaN using
!=operator then the result will always be true.
Logical Operators
-
Priority of Unary operator (NOT etc.) will be higher than binary operator.
-
In case of Logical AND operator, if the first operand is false then it will not evaluate the next operand.
-
In case of Logical OR operator, if the first operand is true then it will not evaluate the next operand.
-
Logical OR and Logical AND are also called as Short Circuit Operators.
instanceof Operator
- Syntax for instanceof Operator:
<referenceVariable> instanceof <class Name>
-
instanceof operator can be used only when reference variable type and the specified class are comparable.
-
Every class which you are defining is by default subclass of Object class.
-
When you use instanceof operator with any reference variable which contains null then it always returns false.
Operator Precedence
-
Operator precedence specifies the order to evaluate the operator in the expression.
-
Operator associativity specifies which operator should be evaluated first when there are operators with the same precedence available in the single expression.
Example 1
7 + 5 - 4 * 2 / 2
= 7 + 5 - 8 / 2
= 7 + 5 - 4
= 12 - 4
= 8
Example 2
7 - 5 + 4 / 2 * 2
= 7 - 5 + 2 * 2
= 7 - 5 + 4
= 2 + 4
= 6
Operator Precedence Table
| Operators | Precedence |
|---|---|
| postfix | expr++, expr-- |
| unary | ++expr, --expr, +expr, -expr, ~, ! |
| multiplicative | *, /, % |
| additive | +, - |
| shift | <<, >>, >>> |
| relational | <, >, <=, >=, instanceof |
| equality | ==, != |
| bitwise AND | & |
| bitwise exclusive OR | ^ |
| bitwise inclusive OR | | |
| logical AND | && |
| logical OR | || |
| ternary | ? : |
| assignment | =, +=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>=, >>>= |
- Parenthesis can be used to change the order of precedence.