top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Java: Why a += b does not give any compile error while a = a + b does when a is int and b is double?

+3 votes
444 views

In Java
I am using statement a += b where a is int and b is double, when I use a += b, it won't give error but in case of a = a + b it gives error, why ?

posted Dec 15, 2015 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

When You use a += b in Java, the interpreter changes your statement a += b to
a = (instanceof a) (a + b) i.e. it automatically typecasts your assignment so no error came but when you write a = a + b; it needs an explicit typecast.

so if you write,

int a = 3;
a += 32.5;    //    it actually a = (int)(a + 32.5); 
answer Jan 4, 2016 by Karthick.c
...