Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between double a = a + int b and int a += double b?

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.

like image 866
rawnd Avatar asked Mar 03 '09 23:03

rawnd


People also ask

Which is bigger double or int?

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.

Should I use int or double in Java?

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.


1 Answers

In Java += operator has an implicit cast to the left hand type. This goes for all composed operators.

like image 141
DefLog Avatar answered Oct 05 '22 22:10

DefLog