Java 8 Lambda的工作不起作用,Java 8中的其他一切都可以工作 [英] Java 8 Lambda's don't work, everything else from Java 8 works though

查看:117
本文介绍了Java 8 Lambda的工作不起作用,Java 8中的其他一切都可以工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现Java 8正式发布。似乎我需要Eclipse Luna 4.4来工作。所以我下载了Luna并安装了它。我还从我的其他Eclipse导入了我的所有项目,一切都按预期工作。当我想尝试Java 8时,我很快发现我需要先安装它。安装完成后,我设法将JRE 8更改为默认值。



我的问题是:为什么要使用新的日期&时间API,但是我不能使用新的Lambda表达式?



可能有用的一些信息:




  • 我正在使用Mac

  • 我非常确定时间API的工作原理(即使是小型的Java文档框也是1.8以来)



可能与我没有使用Java 8安装Eclipse的事实有关吗?

解决方案

首先,您不需要使用Luna - 有一个功能补丁开普勒,其工作正常。



其次,Java编译器对话框中的源兼容性部分必须为1.8。否则,即使您可以使用Java 1.8的功能,您将无法使用语言功能。 (这不只是lambdas - 有方法引用,接口中的静态方法和默认方法)。这里是要查看的内容:





很少有人想要使用1.8的库功能,但是将源/类文件的兼容性保持为1.7或更早版本,但是如果您正在编写需要在各种JRE上运行的代码,那么可能会有用,但是您可以使用一些需要Java的功能实现1.8,只是不会在早期的JRE上使用。


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.

My question is: Why can I use the new date & time API, but I cannot use the new Lambda Expressions?

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)

Could it have something to do with the fact that I didn't install Eclipse with Java 8 included?

解决方案

Firstly, you don't need to use Luna - there's a feature patch for Kepler which works fine.

Secondly, the "source compatibility" part of the Java Compiler dialog has to be 1.8. Otherwise even though you're allowed to use the library features of Java 1.8, you won't be able to use the language features. (It's not just lambdas - there's method references, static methods in interfaces, and default methods for example.) Here's where to look:

It would be rare that you'd want to 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.

这篇关于Java 8 Lambda的工作不起作用,Java 8中的其他一切都可以工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆