Skip to main content

Posts

Showing posts from February, 2010

Java Puzzler

I bought the book Java Puzzlers by Josh Bloch and Neal Gafter a few years ago and enjoyed it. If you aren't familiar with it, it covers rare, odd and usually counter-intuitive edge cases of Java in a brain-teaser style of presentation. For both a Java user and an analytical mind its a fascinating book. Well today I stumbled onto a puzzler of my own and thought I'd share. Can you tell me what the main method of this class will output? 1 : public class Parser { 2 : 3 : private static <T> T parse(String s, Class<T> type) { 4 : // Simple implementation only supports long primitive 5 : if(type == long.class) { 6 : return (T) Long.parseLong(s); 7 : } 8 : throw new UnsupportedOperationException( 9 : "parse() only supports long right now!"); 10: } 11: 12: public static void main(String[] args) { 13: System.out.println(parse("1234", long.class).equals(1234)); 14: } 15: } So what gets written to standard output?