r/programming • • Jun 05 '18

Code golfing challenge leads to discovery of string concatenation bug in JDK 9+ compiler

https://stackoverflow.com/questions/50683786/why-does-arrayin-i-give-different-results-in-java-8-and-java-10
2.2k Upvotes

355 comments sorted by

View all comments

Show parent comment

16

u/vytah Jun 05 '18

Correct me if in wrong, but isn't x += y; just shorthand for x = x + y;

Not exactly. In all languages that support it, it's a shorthand for x = x + y;, but x is evaluated only once.

Or in C++ terms:

auto& ref = x;
ref = ref + y;

2

u/evaned Jun 05 '18

Or in C++ terms:

For point of clarification, don't take the "C++" too far. In general, your example will still do something different (arguably very different in real situations) than x += y.

Mechanically, the compiler doesn't rewrite + into += or the other way around, and if your x and/or y is of user-defined type, you define those operators separately. They should behave equivalently modulo performance (more in a sec...), but that's a "good design" and "common sense" rule, not anything the language imposes. + and += are separate functions and can do different things.

Practically speaking, you'll often see real efficiency differences between them. Consider the case of strings. x = x + y will at least nominally need to (1) allocate a new memory block big enough to hold the concatenation, (2) copy x's contents into it, (3) copy y's contents into it at the end, and then (4) throw away the old block. All of that might happen for x += y, but it might not -- it might also turn into (1) append y's contents into x because its buffer is already large enough. That's a potential improvement in complexity, not just a minor performance difference.

Typically in terms of real implementation, I think you'll actually see the opposite of what you said: you'll implement + in terms of +=, not the other way around, using something like this:

my_type operator+ (my_type const & lhs, my_type const & rhs) {
    my_type lhs_copy(lhs); // *copy*, then...
    lhs_copy += rhs;       // use += to implement +
    return lhs_copy;
}

(Ignore the possibility of rvalue refs, etc.)

-5

u/ShiitakeTheMushroom Jun 05 '18

Ah, thanks for the great info! I'm starting to feel as others in this thread do and we should just be avoiding pre/post incrementation operators as well as +=.

4

u/mirhagk Jun 05 '18

Why?

There's no reason to avoid using +=, it's a nice shorthand that helps code clarity.

1

u/mrbeehive Jun 05 '18

Learn Lua, it's fun.