Skip to main content

Operators Summary

Arithmetic Operators

  1. Result of Arithmetic Expression is always int type or wider than int type.

  2. Result of Arithmetic Expression will not be byte, char or short type.

  3. When you want to use two same Unary Arithmetic Operators continuously then you must separate those two operators with parenthesis or space.

  4. When you use two same Unary Arithmetic Operators continuously without parenthesis or space then it will be considered as INCREMENT or DECREMENT operator.

  5. When you want to use two different Unary Arithmetic Operators continuously then parenthesis or space is not required.

  6. When char type operand is used in arithmetic expression then ASCII value will be used while performing the operation.

  7. 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

  8. When the operands are int or lower than int type then result will be int type only.

  9. When any one operand or both operands are wider than int type then result will be wider type.

  10. long value can be stored in float type variable because floating point data type stores the value in exponential representation.

  11. long or wider than long can't be assigned to lower type variable even if it is final.

  12. 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
  1. When you divide any integer value with 0, then it throws java.lang.ArithmeticException at runtime.

  2. Infinity concept is not available with integral types because their range is finite.

  3. Infinity concept is available with floating types because their range is near to infinity.

  4. Operator Precedence (Example)

7 + (6 + 5 * 3) - 4 / 2
= 7 + (6 + 15) - 4 / 2
= 7 + 21 - 4 / 2
= 7 + 21 - 2
= 28 - 2
= 26
  1. Precedence of Arithmetic Operators
  • Unary: +exp, exp
  • Multiplicative: , /, %
  • Additive: +,

Assignment Operators

  1. When multiple operators of same precedence are found in an expression, they are evaluated from left to right.

  2. 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.

  1. Cases where type casting is NOT possible
a) boolean b = (boolean)1;
b) String str = (String)99;
etc.

  1. 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

  1. 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

  1. When you are using Relational Operators with the primitive type operands then the value will be compared, not the data types.

  2. Operands of Comparison Operators must be numeric or char type.

  3. Operands of Equality Operators can be numeric, char, boolean or reference type.

  4. 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.
  1. If you are comparing any value with NaN using == operator then the result will always be false.

  2. If you are comparing any value with NaN using != operator then the result will always be true.

Logical Operators

  1. Priority of Unary operator (NOT etc.) will be higher than binary operator.

  2. In case of Logical AND operator, if the first operand is false then it will not evaluate the next operand.

  3. In case of Logical OR operator, if the first operand is true then it will not evaluate the next operand.

  4. Logical OR and Logical AND are also called as Short Circuit Operators.

instanceof Operator

  1. Syntax for instanceof Operator:
<referenceVariable> instanceof <class Name>
  1. instanceof operator can be used only when reference variable type and the specified class are comparable.

  2. Every class which you are defining is by default subclass of Object class.

  3. When you use instanceof operator with any reference variable which contains null then it always returns false.


Operator Precedence

  1. Operator precedence specifies the order to evaluate the operator in the expression.

  2. 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

OperatorsPrecedence
postfixexpr++, expr--
unary++expr, --expr, +expr, -expr, ~, !
multiplicative*, /, %
additive+, -
shift<<, >>, >>>
relational<, >, <=, >=, instanceof
equality==, !=
bitwise AND&
bitwise exclusive OR^
bitwise inclusive OR&#124;
logical AND&&
logical OR&#124;&#124;
ternary? :
assignment=, +=, -=, *=, /=, %=, &=, ^=, &#124;=, <<=, >>=, >>>=

  1. Parenthesis can be used to change the order of precedence.