2024-04-20 09:00:01

Java的隐藏特性

在阅读了c#的隐藏特性之后,我想知道Java的隐藏特性有哪些?


当前回答

他们花了足够长的时间来增加支持,

系统托盘

其他回答

最终初始化可以推迟。

它确保即使使用复杂的逻辑流也始终设置返回值。很容易错过一个case并意外返回null。它并不是不可能返回null,只是很明显它是故意的:

public Object getElementAt(int index) {
    final Object element;
    if (index == 0) {
         element = "Result 1";
    } else if (index == 1) {
         element = "Result 2";
    } else {
         element = "Result 3";
    }
    return element;
}

语言级assert关键字。

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).

我真的很喜欢从Java 1.6重写的线程API。可调用对象很棒。它们基本上是带有返回值的线程。

您选择的编码中的属性文件如何?过去,当你加载你的属性,你提供了一个InputStream和load()方法解码为ISO-8859-1。你实际上可以用其他编码来存储文件,但你必须在加载后使用像这样令人作呕的黑客来正确解码数据:

String realProp = new String(prop.getBytes("ISO-8859-1"), "UTF-8");

但是,从JDK 1.6开始,有一个load()方法接受Reader而不是InputStream,这意味着您可以从一开始就使用正确的编码(还有一个store()方法接受Writer)。对我来说,这似乎是一件相当大的事情,但它似乎是悄无声息地潜入JDK的。我几周前才偶然发现它,快速搜索谷歌,只发现了一个提及。