Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 Lambdas don't work, everything else from Java 8 works though

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?

like image 682
Kametrixom Avatar asked Mar 20 '14 20:03

Kametrixom


People also ask

Which of following is correct about Java 8 lambda expression?

Explanation. Both of the above options are correct. Q 5 - Which of the following is correct about Java 8 lambda expression? A - Lambda expressions are used primarily to define inline implementation of a functional interface.

Does Java 1.8 support lambda expressions?

Lambda Expressions were added in Java 8.

Why are lambdas expression used in Java 8?

Lambda expression is a new and important feature of Java which was included in Java SE 8. It provides a clear and concise way to represent one method interface using an expression. It is very useful in collection library. It helps to iterate, filter and extract data from collection.

How do you handle if else in Java 8?

One Line if-else Statement Using filter in Java 8The streams filter method takes a Predicate and behaves like if-else in Java language. The above program instantiates a list using Arrays. asList() method.


1 Answers

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:

Java 1.8 compiler settings

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.

like image 159
Jon Skeet Avatar answered Sep 30 '22 11:09

Jon Skeet