2011 September

26 Sep: String – Best Pratices

Never, never, never use the String constructor Whenever you want to instantiate a String object, never use its constructor but always instantiate it directly. For example: //don’t: String slow = new String(“Yet another string object”); //do String fast = “Yet another string object”; From the Javadoc: Unless an explicit copy of original is needed, use of this constructor is unnecessary since Strings are immutable. Pattern and regular expressions For performance reasons, instances of pattern (java.util.regex.Pattern) should be…

08 Sep: HttpClient – Best Pratices

URLConnection -> HttpClient Except to manage specific resources other than http / https (ftp, gopher, file …), use the HttpClient and not the HttpURLConnection, and avoid to use both at the same time. The HttpClient is more mature and has a richer feature set as multi-threaded management, cookies, pipelining, connection pools … Instantiation Instantiating a HttpClient is costly in resources, we must focus on reuse, even singletons, as described in the doc: « Generally it is recommended…