Java tricky question
原文:4 out of 5 Java developers failed to solve this question
1. The toughest question of the Java deathmatch
正确答案:Compliation fails because no SQLException is thrown
关于java泛型类型擦除的问题。
2. toString(), or not toString(), that is the question
类没有实现toString的小陷阱
正确答案:None of the above
3. Google Guava Sets
正确答案:Potential out of memory
4. Double brace initialization, lol wut?!
正确答案:Potential null access
参考Double Brace Initialization,集合{{}}的形式相当于匿名内部类,NAMES.add的时候还没实例化完成。。
正确的写法:
private static final List NAMES = new ArrayList(){{
add("Hello");
this.add("World");
System.out.println(this); //这里用NAMES的话是null
}};
5. The curious case of the map at runtime
正确答案:[] true
Bonus: And the easiest question is…
正确答案:C