Most other high-level languages would let you copy the substring even if you don't intend to modify it.
Most other high-level languages would let you copy the substring even if you don't intend to modify it.
C++, std::string yes. But for example Java, C# and Go (slices) substring doesn't copy data.
Like C, any dev that makes language assumptions based on the installed compiler, is bound to get burned.
So the thing about advanced compilers in the java world is that they use defined behaviour(results) to do optimisations. And the best optimisations are about not doing work you can avoid. i.e. if you can avoid doing an allocation at all because the (sub)string you just created is only used locally, the object might not be allocated at all (heap|stack) but just registers are reassigned with temporary values needed to calculate the function. So its already common to have O(n) semantics turn into O(1) or even better O(0) ;).
In the java world results are specced, not how you need to get them. (Exceptions to this rule exist).
The implementation changing to be asymptotically worse is not fine.
Also they could have changed the garbage collector to make both cases perform well.
Edit: And the comments you linked have a good point made in them, if you don't assume some bounds on how long a function is going to take, you can't call it ever. substr is not a function that's supposed to be forbidden.
edit: I believe String is overused in java land and we should have more CharSequence implementations. https://docs.oracle.com/javase/8/docs/api/java/lang/CharSequ...
If you're stating that C# Substring doesn't copy the string, that is incorrect; .NET always makes a copy: http://stackoverflow.com/questions/6742923/if-strings-are-im...