If Python strings are immutable, why does it keep the same id if I use += to append to it?

It’s a CPython-specific optimization for the case when the str being appended to happens to have no other living references. The interpreter “cheats” in this case, allowing it to modify the existing string by reallocating (which can be in place, depending on heap layout) and appending the data directly, and often reducing the work significantly … Read more

Is the time-complexity of iterative string append actually O(n^2), or O(n)?

In CPython, the standard implementation of Python, there’s an implementation detail that makes this usually O(n), implemented in the code the bytecode evaluation loop calls for + or += with two string operands. If Python detects that the left argument has no other references, it calls realloc to attempt to avoid a copy by resizing … Read more

How is String concatenation implemented in Java 9?

The “old” way output a bunch of StringBuilder-oriented operations. Consider this program: public class Example { public static void main(String[] args) { String result = args[0] + “-” + args[1] + “-” + args[2]; System.out.println(result); } } If we compile that with JDK 8 or earlier and then use javap -c Example to see the … Read more

Why is string concatenation faster than array join?

Browser string optimizations have changed the string concatenation picture. Firefox was the first browser to optimize string concatenation. Beginning with version 1.0, the array technique is actually slower than using the plus operator in all cases. Other browsers have also optimized string concatenation, so Safari, Opera, Chrome, and Internet Explorer 8 also show better performance … Read more

const char* concatenation

In your example one and two are char pointers, pointing to char constants. You cannot change the char constants pointed to by these pointers. So anything like: strcat(one,two); // append string two to string one. will not work. Instead you should have a separate variable(char array) to hold the result. Something like this: char result[100]; … Read more