top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
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
57 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
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...