I happen to be looking at the String source code and noticed the following:
Java Code:
public String replace(char oldChar, char newChar) {
if (oldChar != newChar) {
int len = value.length;
int i = -1;
char[] val = value; /* avoid getfield opcode */
value is the private character array which backs up the String object. The rest of the method is pretty basic. The interesting thing to me is instead of using value directly to examine the String, the author(s) made a local copy of the array reference. According to the comment, this seems to be a slight optimization. It sort of makes me wonder how many other subtleties there are that are not well advertised (or at least I haven't read about them). Any thoughts? This is from Java 1.8.
Regards,
Jim
No comments:
Post a Comment