2024-04-20 09:00:01

Java的隐藏特性

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


当前回答

它并不是完全隐藏的,但反射是非常有用和强大的。使用简单的class . forname("…"). newinstance()是很好的,其中类类型是可配置的。编写这种工厂实现很容易。

其他回答

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

几个月前当我第一次发现它时,双括号初始化让我感到惊讶,以前从未听说过它。

作为存储每个线程状态的一种方式,ThreadLocals通常并不广为人知。

由于JDK 1.5 Java已经有了非常好的实现和健壮的并发工具,而不仅仅是锁,它们存在于Java .util.concurrent中,一个特别有趣的例子是Java .util.concurrent.atomic子包,它包含线程安全的原语,实现比较和交换操作,并可以映射到这些操作的实际本机硬件支持版本。

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.util.Arrays中的asList方法允许变量参数、泛型方法和自动装箱的良好组合:

List<Integer> ints = Arrays.asList(1,2,3);

字符串参数化类工厂。

Class.forName( className ).newInstance();

从部署jar文件中加载资源(属性文件,xml, xslt,图像等)。

this.getClass().getClassLoader().getResourceAsStream( ... ) ;