09 Sep 6
JAVA Substring Bug!
Sometimes, when we are working with string in java, we are always tempted to use the substring method for extracting part of a string. It’s neat and efficient when you are working with small program or little string processing. However, this method is evil. It is actually causing you whole lots of problem. The substring returns the part of the original string rather than a new one. Thus, GC wont collect the original string. So when the file or string get larger and larger, it’s actually taking up more and more memory, and thus OutOfMemory Error. To solve this, there’s nice workaround which is simple.
String newString = new String( oldString.subString(0, 5) );
Simple, Right?!