Why does:
public class Addition { public static void main() { int a = 0; double b = 1.0; a = a + b; System.out.println(a); } }
not compile but:
public class Addition { public static void main() { int a = 0; double b = 1.0; a += b; System.out.println(a); } }
compiles.
Double is bigger than int as it requires more storage space; hence, int values are implicitly converted to double by Java. Here, d = variable to store double value after conversion to double data type. i = integer value to be converted to double data type.
The main difference between int and double is that int is used to store 32 bit two's complement integer while double is used to store 64 bit double precision floating point value. In brief, double takes twice memory space than int to store data.
In Java += operator has an implicit cast to the left hand type. This goes for all composed operators.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With