I have a question about the operators += and =.
In the following code you can clearly see that there is a difference between the operations
sum+=n
and
sum=sum+n
Which are known to be equivalent.
This doesn't compile without casting
this.mix = mix + col;
This compiles. Why?
this.mix += col;
Please refer to the addToMix method:
public class Color {
byte r;
byte g;
byte b;
byte mid;
byte mix;
public Color (byte r, byte g, byte b) {
this.r = r;
this.g = g;
this.b = b;
createMid(r, g);
}
private void createMid(byte r, byte g) {
this.mid = (byte)(r + g);
createMix(r,g);
}
private void createMix (byte mid, byte b) {
this.mix = (byte)(mid + b);
}
public void addToMix (byte col) {
//this.mix = mix + col; // not compiled without casting obviously
this.mix += col; // compiled. why?
}
public byte getMid() {
return this.mid;
}
public byte getMix() {
return this.mix;
}
}