在阅读了c#的隐藏特性之后,我想知道Java的隐藏特性有哪些?
当前回答
一个接口可以扩展多个接口,但一个类只能扩展一个类,这让我感到惊讶。
其他回答
同一个类的实例可以访问其他实例的私有成员:
class Thing {
private int x;
public int addThings(Thing t2) {
return this.x + t2.x; // Can access t2's private value!
}
}
java.util.Arrays中的asList方法允许变量参数、泛型方法和自动装箱的良好组合:
List<Integer> ints = Arrays.asList(1,2,3);
几年前,当我必须做Java (1.4.x)时,我想要一个eval()方法,而太阳javac是(是?)用Java编写的,所以它只是链接tools.jar,并使用它周围的一些胶水代码。
finally块中的控制权转移会丢弃任何异常。下面的代码不会抛出RuntimeException——它丢失了。
public static void doSomething() {
try {
//Normally you would have code that doesn't explicitly appear
//to throw exceptions so it would be harder to see the problem.
throw new RuntimeException();
} finally {
return;
}
}
从http://jamesjava.blogspot.com/2006/03/dont-return-in-finally-clause.html
泛型方法的类型参数可以像这样显式指定:
Collections.<String,Integer>emptyMap()
推荐文章
- 在流中使用Java 8 foreach循环移动到下一项
- 访问限制:'Application'类型不是API(必需库rt.jar的限制)
- 用Java计算两个日期之间的天数
- 如何配置slf4j-simple
- 在Jar文件中运行类
- 带参数的可运行?
- 我如何得到一个字符串的前n个字符而不检查大小或出界?
- 我可以在Java中设置enum起始值吗?
- Java中的回调函数
- c#和Java中的泛型有什么不同?和模板在c++ ?
- 在Java中,流相对于循环的优势是什么?
- Jersey在未找到InjectionManagerFactory时停止工作
- 在Java流是peek真的只是调试?
- Recyclerview不调用onCreateViewHolder
- 将JSON字符串转换为HashMap