2024-04-20 09:00:01

Java的隐藏特性

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


当前回答

我喜欢

Javadoc的taglet和doclet使我们能够自定义Javadoc输出。 JDK工具:jstat, jstack等。

其他回答

泛型方法的类型参数可以像这样显式指定:

Collections.<String,Integer>emptyMap()

我个人很晚才发现java.lang.Void——结合泛型提高了代码的可读性,例如Callable<Void>

真正让我吃惊的是自定义序列化机制。

而这些方法是私有的!!,它们会在对象序列化期间被JVM“神秘地”调用。

private void writeObject(ObjectOutputStream out) throws IOException;
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException;

通过这种方式,您可以创建自己的自定义序列化,使其更加“随便”(安全、快速、罕见、简单等)。

如果大量信息必须通过节点传递,这是真正应该考虑的事情。序列化机制可以更改为发送数据的一半。很多时候,瓶颈并不在平台上,而是在通过线路发送的数量上,这可能会为您节省数千个硬件dll。

这是一篇文章。 http://java.sun.com/developer/technicalArticles/Programming/serialization/

我认为java的另一个“被忽视”的特性是JVM本身。它可能是可用的最好的VM。它支持许多有趣和有用的语言(Jython, JRuby, Scala, Groovy)。所有这些语言都可以轻松无缝地合作。

如果你设计了一种新的语言(就像在scala的例子中),你会立刻拥有所有现有的库,因此你的语言从一开始就是“有用的”。

所有这些语言都利用了HotSpot优化。VM可以很好地监控和调试。

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