原文地址:http://stackoverflow.com/questions/15496/hidden-features-of-java
我选择几个有趣的:
1. Double Brace Initialization
private static final Set<String> VALID_CODES = new HashSet<String>() {{
add("XZ13s");
add("AB21/X");
add("YYLEX");
add("AR2D");
}};
2. 泛型参数可以这么用
public static <A, B extends Collection<A> & Comparable<B>> boolean foo(B b1, B b2, A a) {
return (b1.compareTo(b2) == 0) || b1.contains(a) || b2.contains(a);
}
3. Check null is not necessary
if( null != aObject && aObject instanceof String )
{
// not necessary to check != null
}
just use:
if( aObject instanceof String )
{
...
}
4. Allowing methods and constructors in enums
enum Cats {
FELIX(2), SHEEBA(3), RUFUS(7);
private int mAge;
Cats(int age) {
mAge = age;
}
public int getAge() {
return mAge;
}
}
5. 自Java 6开始在声明类路径时可以使用通配符
java -classpath ./lib/* so.Main
6. Assert
assert object != null;
posted on 2011-10-12 13:11
West Farmer 阅读(361)
评论(2) 编辑 收藏 所属分类:
Java