Java 的隐藏特性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hidden Features of Java
提问by grom
After reading Hidden Features of C#I wondered, What are some of the hidden features of Java?
阅读C# 的隐藏特性后,我想知道 Java 的一些隐藏特性是什么?
回答by Mark Cidade
Language-level assertkeyword.
语言级断言关键字。
回答by Mo.
I really like the rewritten Threading API from Java 1.6. Callables are great. They are basically threads with a return value.
我真的很喜欢从 Java 1.6 重写的线程 API。Callables 很棒。它们基本上是具有返回值的线程。
回答by Michael Neale
static imports to "enhance" the language, so you can do nice literal things in type safe ways:
静态导入以“增强”语言,因此您可以以类型安全的方式做一些不错的文字操作:
List<String> ls = List("a", "b", "c");
(can also do with maps, arrays, sets).
(也可以使用地图、数组、集合)。
http://gleichmann.wordpress.com/2008/01/13/building-your-own-literals-in-java-lists-and-arrays/
http://gleichmann.wordpress.com/2008/01/13/building-your-own-literals-in-java-lists-and-arrays/
Taking it further:
更进一步:
List<Map<String, String>> data = List(Map( o("name", "michael"), o("sex", "male")));
回答by Mo.
I think another "overlooked" feature of java is the JVM itself. It is probably the best VM available. And it supports lots of interesting and useful languages (Jython, JRuby, Scala, Groovy). All those languages can easily and seamlessly cooperate.
我认为 Java 的另一个“被忽视”的特性是 JVM 本身。它可能是最好的 VM。它支持许多有趣和有用的语言(Jython、JRuby、Scala、Groovy)。所有这些语言都可以轻松无缝地协作。
If you design a new language (like in the scala-case) you immediately have all the existing libraries available and your language is therefore "useful" from the very beginning.
如果您设计一种新语言(例如在 scala 案例中),您将立即拥有所有可用的库,因此您的语言从一开始就“有用”。
All those languages make use of the HotSpot optimizations. The VM is very well monitor and debuggable.
所有这些语言都使用了 HotSpot 优化。VM 是非常好的监控和调试。
回答by Boris Terzic
Double Brace Initializationtook me by surprise a few months ago when I first discovered it, never heard of it before.
几个月前,当我第一次发现双括号初始化时,我感到很惊讶,以前从未听说过。
ThreadLocalsare typically not so widely known as a way to store per-thread state.
ThreadLocals通常作为一种存储每个线程状态的方式并不那么广为人知。
Since JDK 1.5 Java has had extremely well implemented and robust concurrency tools beyond just locks, they live in java.util.concurrentand a specifically interesting example is the java.util.concurrent.atomicsubpackage that contains thread-safe primitives that implement the compare-and-swapoperation and can map to actual native hardware-supported versions of these operations.
由于 JDK 1.5 Java 除了锁之外,还有非常好的实现和健壮的并发工具,它们存在于java.util.concurrent 中,一个特别有趣的例子是java.util.concurrent.atomic子包,它包含实现比较的线程安全原语-and-swap操作,并且可以映射到这些操作的实际本机硬件支持版本。
回答by serg10
Not really a feature, but it makes me chuckle that goto
is a reserved word that does nothing except prompting javac to poke you in the eye. Just to remind you that you are in OO-land now.
不是一个真正的功能,但它让我发笑,这goto
是一个保留字,除了提示 javac 戳你的眼睛之外什么都不做。只是为了提醒您您现在处于面向对象的领域。
回答by PPS
As a starter I really appreciate the JConsole monitoring software in Java 6, it has solved a couple of problems for me already and I keep on finding new uses for it.
作为初学者,我非常欣赏 Java 6 中的 JConsole 监控软件,它已经为我解决了一些问题,我一直在寻找它的新用途。
Apparently the JConsole was there already in Java 5 but I reckon it is improved now and at least working much more stable as of now.
显然 JConsole 在 Java 5 中已经存在,但我认为它现在得到了改进,至少现在工作更加稳定。
JConsole in Java 5: JConsole in Java 5
Java 5 中的JConsole:Java 5 中的 JConsole
JConsole in Java 6: JConsole in Java 6
Java 6 中的JConsole:Java 6 中的 JConsole
And while you are at it, have a good look at the other tools in the series: Java 6 troubleshooting tools
当您在使用它时,请仔细查看本系列中的其他工具: Java 6 故障排除工具
回答by John Meagher
It's not exactly hidden, but reflection is incredibly useful and powerful. It is great to use a simple Class.forName("...").newInstance() where the class type is configurable. It's easy to write this sort of factory implementation.
它并没有完全隐藏,但反射非常有用和强大。使用一个简单的 Class.forName("...").newInstance() 是很好的,其中类类型是可配置的。编写这种工厂实现很容易。
回答by serg10
How about covariant return typeswhich have been in place since JDK 1.5? It is pretty poorly publicised, as it is an unsexy addition, but as I understand it, is absolutely necessary for generics to work.
自 JDK 1.5 以来已经存在的协变返回类型怎么样?它的宣传很差,因为它是一个不合时宜的添加,但据我所知,对于泛型工作是绝对必要的。
Essentially, the compiler now allows a subclass to narrow the return type of an overridden method to be a subclass of the original method's return type. So this is allowed:
本质上,编译器现在允许子类将重写方法的返回类型缩小为原始方法返回类型的子类。所以这是允许的:
class Souper {
Collection<String> values() {
...
}
}
class ThreadSafeSortedSub extends Souper {
@Override
ConcurrentSkipListSet<String> values() {
...
}
}
You can call the subclass's values
method and obtain a sorted thread safe Set
of String
s without having to down castto the ConcurrentSkipListSet
.
您可以调用子类的values
方法并获得s的排序线程安全Set
,而无需向下强制转换为.String
ConcurrentSkipListSet
回答by Brad Barker
Functors are pretty cool. They are pretty close to a function pointer, which everyone is usually quick to say is impossible in Java.
函子很酷。它们非常接近函数指针,每个人通常很快就会说这在 Java 中是不可能的。