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
  • 46,613
  • 43
  • 151
  • 237
Shiladittya Chakraborty
  • 4,270
  • 8
  • 45
  • 94

1 Answers1

1

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

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307