且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

String.substring()制作底层char []值的副本

更新时间:2022-06-22 02:42:21

String 的实际表示是一个内部实现细节,所以你永远无法确定。然而,根据甲骨文工程师的公开谈话(最着名的是 @shipilev ),它不太可能被改变。这样做不仅可以解决可能的内存泄漏,还可以简化String内部。使用更简单的字符串,可以更轻松地实现许多优化技术,如字符串重复数据删除 Compact Strings

The actual representation of the String is an internal implementation detail, so you can never be sure. However according to public talks of Oracle engineers (most notably @shipilev) it's very unlikely that it will be changed back. This was done not only to fight with possible memory leak, but also to simplify the String internals. With simpler strings it's easier to implement many optimization techniques like String deduplication or Compact Strings.