Wednesday, February 06, 2008

Tuesday auto-boxing nugget

What does the following print?

public static void main(String[] _args) {
System.out.println(boolean.class.isAssignableFrom(Boolean.class));
System.out.println(Boolean.class.isAssignableFrom(boolean.class));
System.out.println(Boolean.class.isInstance(true));
System.out.println(boolean.class.isInstance(true));
}

The answer:
false
false
true
false

This makes sense when you think about it, but it can definitely trip you up when you've become accustomed to auto-boxing. Incidentally, this is my first true "gotcha" w/ auto-boxing in the 2.5 years I've been using it.








No comments: