In Java whenever you are trying to add higher data type object to lower data type object, the lower data type object is casted to higher data type object and the end result is always summation of higher data type object. You can’t implicitly cast the higher data type object to lower data type object. It’ll cause incompatible type casting.
In this code,
int i=5, long j=8; i+=j;
// Here “+=” does type casting this way i.e. by compiler implicitly uppercasting is done in this manner
i=(int)((long) i+j);
Whereas, in case of i=i+j; you need to cast explicitly long into int in this way:
i=i+(int)j;
Are you interested in learning Java from scratch! Refer to this video on Java provided by Intellipaat:
So, ideally in Java, type conversion is done to the type of the right variable from the type of left variable as shown below.
Byte-> Short->Int->Long->float->Double