0

I have below code

int i = 5;
long j = 5;

1. i = i + j; // Throwing an exception "Type mismatch: cannot convert from long to int"
2. i += j; // This working fine

As you can see 1st case throwing an exception but 2nd case working fine.

Why 2nd case working fine without throwing an any exception?

Raedwald
  • 43,666
  • 36
  • 142
  • 227
Shiladittya Chakraborty
  • 4,138
  • 7
  • 42
  • 83

1 Answers1

1

+= is a compound statement and Compiler internally casts it. Where as in first case direct statement and compiler cries.

Suresh Atta
  • 118,038
  • 37
  • 189
  • 297