Changes to String.substring in Java 7
Join the DZone community and get the full member experience.Join For Free
it is common knowledge that java optimizes the substring operation for the case where you generate a lot of substrings of the same source string. it does this by using the
(value, offset, count)
way of storing the information. see an example below:
in the above diagram you see the strings “hello” and “world!” derived from “hello world!” and the way they are represented in the heap: there is one character array containing “hello world!” and two references to it. this method of storage is advantageous in some cases, for example for a compiler which tokenizes source files. in other instances it may lead you to an outofmemorerror (if you are routinely reading long strings and only keeping a small part of it – but the above mechanism prevents the gc from collecting the original string buffer). some even
call it a bug
. i wouldn’t go so far, but it’s certainly a leaky abstraction because you were forced to do the following to ensure that a copy was made:
new string(str.substring(5, 6))
this all changed in
may of 2012
or java 7u6. the pendulum is swung back and now full copies are made by default. what does this mean for you?
- for most probably it is just a nice piece of java trivia
- if you are writing parsers and such, you can not rely any more on the implicit caching provided by string. you will need to implement a similar mechanism based on buffering and a custom implementation of charsequence
if you were doing
new string(str.substring)to force a copy of the character buffer, you can stop as soon as you update to the latest java 7 (and you need to do that quite soon since java 6 is being eold as we speak ).
thankfully the development of java is an open process and such information is at the fingertips of everyone!
a couple of more references (since we don’t say pointers in java
) related to strings:
- if you are storing the same string over and over again (maybe you’re parsing messages from a socket for example), you should read up on alternatives to string.intern() (and also consider reading chapter 50 from the second edition of effective java: avoid strings where other types are more appropriate)
look into (and do benchmarks before using them!) options like usecompressedstrings (which
seems to have been removed
), usestringcache and stringcache
hope i didn’t strung you along too much and you found this useful! until next time
- attila balazs
meta: this post is part of the
calendar and is licensed under the creative commons 3.0 attribution license. if you like it, please spread the word by sharing, tweeting, fb, g+ and so on! want to write for the blog? we are looking for contributors to fill all 24 slot and would love to have your contribution! contact attila balazs to contribute!
Published at DZone with permission of Attila-Mihaly Balazs, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Manifold vs. Lombok: Enhancing Java With Property Support
AI and Cybersecurity Protecting Against Emerging Threats
Introduction to Domain-Driven Design