2

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;
    }   
}
J. Steen
  • 15,470
  • 15
  • 56
  • 63
theexplorer
  • 359
  • 1
  • 5
  • 21
  • 5
    I'm pretty sure this is a dup. Basically `+=` is like addition and assignment *with a cast*. See JLS section 15.26.2. – Jon Skeet Jun 11 '14 at 05:55
  • Like I said before I deleted my comment and edited your question: Please don't put questions deeply embedded in swaths of code. It's merely a suggestion for you to avoid people like me giving useless comments about not finding a question. =) – J. Steen Jun 11 '14 at 05:58
  • The "swaths of code" part is particularly relevant here. There's no need for more than two or three lines of code here. Or as a short but complete program, about 6 lines of code. Always reduce a question down to its bare essentials. – Jon Skeet Jun 11 '14 at 06:04
  • No, it's really not that hard to ask a good question. It's not that hard to work out whether a short but complete program is required - when it is, trim down the code to *just* what is required. If you'd posted a short but complete program (6 or 7 lines would do it) that would have been fine - but in this case just a snippet would have been fine. There's no need for any methods, or any concept of red, green and blue values etc. They're simply irrelevant. Before posting, try to read your question as if you were trying to answer it - and see if it's ideal. See http://tinyurl.com/so-hints – Jon Skeet Jun 11 '14 at 06:13
  • @theexplorer: "What are you trying to do" isn't a matter of including all the code - it's a matter of giving context. If you provide a short but *complete* example which demonstrates the problem, I wouldn't expect you to get asked for "the rest of the code" because there doesn't need to be any. You need to include everything that's relevant but *only* what's relevant. I don't think *anyone* would have objected to a few lines of code showing two local variables being declared: `byte x = 5; byte y = 10; x += y; x = x + y;` with an explanation that the final line doesn't compile but the 3rd does. – Jon Skeet Jun 11 '14 at 06:24
  • The answer is obvious : *this.mix* and *mix* are not the same. – Lorenz Meyer Jun 11 '14 at 06:27

0 Answers0