Monday, May 17, 2010

Behind the scenes of Arrays.asList(T… a)

We all know that the method public static List asList(T... a) in Arrays class returns an ArrayList.

But if you try to add or remove an element on this returned arrayList, you get an UnsupportedOperationException. Why?

This is because the returned ArrayList is not of type java.util.ArrayList, instead, it is java.util.Arrays$ArrayList class. And this java.util.Arrays$ArrayList class does not override all the methods of AbstractList class.

That means Arrays.asList() returns you a fixed size list.

Refer this link http://tinyurl.com/26mxkxm for complete article.

Monday, May 10, 2010

Java’s toLowerCase() has got a surprise for you!

We generally use toLowerCase() on strings and write logic to perform some operations. But String.toLowerCase() has a catch. This method is Locale-specific.

If you do not need internationalization, to make it safer, you may use another method toLowerCase(Locale.English) and override the locale to English always.

Read this article on javapapers.com for complete details.