Java 8 Lambdas 不起作用,但 Java 8 中的其他一切都可以工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22544064/
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
Java 8 Lambdas don't work, everything else from Java 8 works though
提问by Kametrixom
I found out that Java 8 is officially released now. It seems that I need Eclipse Luna 4.4 for it to work. So I downloaded Luna and installed it. I also imported all my projects from my other Eclipse, everything worked as expected. When I wanted to try Java 8, I quickly found out I needed to install it first. After I installed it I managed to change JRE 8 to the default.
我发现Java 8现在正式发布了。看来我需要 Eclipse Luna 4.4 才能工作。所以我下载了Luna并安装了它。我还从我的另一个 Eclipse 中导入了我的所有项目,一切都按预期进行。当我想尝试 Java 8 时,我很快发现我需要先安装它。安装后,我设法将 JRE 8 更改为默认值。
My question is: Why can I use the new date & time API, but I cannot use the new Lambda Expressions?
我的问题是:为什么我可以使用新的日期和时间 API,但我不能使用新的 Lambda 表达式?
Some info that might be useful:
一些可能有用的信息:
- I'm using a Mac
- I'm very certain that the time API works (Even the small Java Doc Box says it's since 1.8)
- 我正在使用 Mac
- 我非常确定时间 API 有效(即使是小的 Java Doc Box 也说它是从 1.8 开始的)
Could it have something to do with the fact that I didn't install Eclipse with Java 8 included?
这可能与我没有安装包含 Java 8 的 Eclipse 的事实有关吗?
采纳答案by Jon Skeet
Firstly, you don't need to use Luna - there's a feature patch for Keplerwhich works fine.
首先,您不需要使用 Luna - Kepler有一个功能补丁可以正常工作。
Secondly, the "source compatibility" part of the Java Compiler dialog has to be 1.8. Otherwise even though you're allowed to use the libraryfeatures of Java 1.8, you won't be able to use the languagefeatures. (It's not just lambdas - there's method references, static methods in interfaces, and default methods for example.) Here's where to look:
其次,Java Compiler 对话框的“源代码兼容性”部分必须是 1.8。否则,即使您被允许使用Java 1.8的库功能,您也将无法使用语言功能。(这不仅仅是 lambdas - 例如,还有方法引用、接口中的静态方法和默认方法。)这里是查看的位置:
It would be rare that you'd wantto use library features from 1.8 but keep source/classfile compatibility with 1.7 or earlier, but I guess it could be useful if you were writing code that needed to run on various JREs, but you could have some feature implementations which required Java 1.8 and just wouldn't be used on earlier JREs.
您很少想使用 1.8 中的库功能,但要保持与 1.7 或更早版本的源/类文件兼容性,但我想如果您正在编写需要在各种 JRE 上运行的代码,这可能很有用,但您可以一些需要 Java 1.8 的功能实现,并且不会在早期的 JRE 上使用。