Java tricky question

Raven's Blog

Home Page View on GitHub Send Email

Java tricky question

原文:4 out of 5 Java developers failed to solve this question

1. The toughest question of the Java deathmatch

img

正确答案:Compliation fails because no SQLException is thrown

关于java泛型类型擦除的问题。

2. toString(), or not toString(), that is the question

img 类没有实现toString的小陷阱

正确答案:None of the above

3. Google Guava Sets

img

正确答案:Potential out of memory

4. Double brace initialization, lol wut?!

img

正确答案: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

img

正确答案:[] true

Bonus: And the easiest question is…

img

正确答案:C

联系作者:aducode@126.com
更多精彩文章请点击:http://aducode.github.io