Friday, June 17, 2005

java.lang.String

String text = "1234";
text.substring(4).startsWith("5"); // is false but works.
text.charAt(4) == '5'; // NullPointerException
I came across this suttle difference in the String class when chasing a bug in Bodington where we were getting a NullPointerException exception from attempts to split up a URL (don't ask). I'd guess that the charAt is faster (substring creates a new string object) but unless your software is doing 1000s of tests like this it probably doesn't matter.