3 ms·
>You can see why Java needs these types. With permanent default interning, any sort of sequence involving character-level appends, such as reading the contents
by wtetzner 11y ago
>You can see why Java needs these types. With permanent default interning, any sort of sequence involving character-level appends, such as reading the contents of a book from a file, would result in a preposterous O(n²) version of an otherwise trivial technique.
I don't think this is true. It looks like Java only interns string literals by default. [1] If you get a string another way (user input, for example), it's not interned unless you call the String.intern() method.
[1] https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#intern-- https://docs.oracle.com/javase/8/docs/api/java/lang/String.h...
- richardwhiuk 11y agoYeah, the explanation in this article about Java's string interning made be lose confidence that the author knew what they were talking about. If you compare Strings, you should use .equals() or you are risking a logic error if you aren't very careful. Indeed, you can deliberately un-intern a string by doing new String("my string") (with little realistic value).