在阅读了c#的隐藏特性之后,我想知道Java的隐藏特性有哪些?
当前回答
这并不是一个真正的功能,而是我最近在一些网页上发现的一个有趣的技巧:
class Example
{
public static void main(String[] args)
{
System.out.println("Hello World!");
http://Phi.Lho.free.fr
System.exit(0);
}
}
是一个有效的Java程序(尽管它会生成警告)。 如果你不明白为什么,请看格雷戈里的回答!;-)好吧,这里的语法高亮也给了一个提示!
其他回答
我喜欢
Javadoc的taglet和doclet使我们能够自定义Javadoc输出。 JDK工具:jstat, jstack等。
The power you can have over the garbage collector and how it manages object collection is very powerful, especially for long-running and time-sensitive applications. It starts with weak, soft, and phantom references in the java.lang.ref package. Take a look at those, especially for building caches (there is a java.util.WeakHashMap already). Now dig a little deeper into the ReferenceQueue and you'll start having even more control. Finally grab the docs on the garbage collector itself and you'll be able to control how often it runs, sizes of different collection areas, and the types of algorithms used (for Java 5 see http://java.sun.com/docs/hotspot/gc5.0/gc_tuning_5.html).
你可以在局部类的初始化块和方法中访问最终的局部变量和参数。考虑一下:
final String foo = "42";
new Thread() {
public void run() {
dowhatever(foo);
}
}.start();
有点像个了结,不是吗?
同一个类的实例可以访问其他实例的私有成员:
class Thing {
private int x;
public int addThings(Thing t2) {
return this.x + t2.x; // Can access t2's private value!
}
}
strictfp关键字。(但我从未见过它在实际应用中使用:)
你可以使用下面的符号来获取基元类型的类: float.class等等。在做反射的时候非常有用。
Final数组可用于从匿名内部类中“返回”值(警告,下面的示例无用):
final boolean[] result = new boolean[1];
SwingUtilities.invokeAndWait(new Runnable() {
public void run() { result[0] = true; }
});
推荐文章
- 在流中使用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