Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lambda expression in java not working

Tags:

java

lambda

I'm tryig to make a lambda expresion for an ActionListener, but it's giving me an IllegalStart of expression, what i'm trying to run so far looks like this:

    JFrame frame = new JFrame();
    JButton boton = new JButton("Lambda Button");
    boton.addActionListener(event -> System.out.println("Hello World!"));
    frame.add(boton);

    frame.setVisible(true);

On the other hand, when i use this code instead:

JFrame frame = new JFrame();
    JButton boton = new JButton("Lambda Button");
    boton.addActionListener( new ActionListener() {
                                public void actionPerformed(ActionEvent e)
                                {
                                    System.out.println("Hello World!");
                                }
                                }   );
    frame.add(boton);
    frame.setVisible(true);

It works perfectly fine,

Initially i tought the issue could be the version of java i'm running, but i just updated and keeps doing the same, when i do a java -version i gives me the following:

java -version java version "1.8.0_45" Java(TM) SE Runtime Environment (build 1.8.0_45-b14) Java HotSpot(TM) Client VM (build 25.45-b02, mixed mode)

So, as far as i know it have a version compatible with lambda expression but not succed on making them work, any ideas or suggestion of what could he happening ?

EDIT: When i try to compile i get this:

Prueba.java:57: error: illegal start of expression
            boton.addActionListener(event -> System.out.println("Hello World !"));
                                           ^1 error

EDIT2: I'm not using any IDE, im compiling from the command line

like image 292
Jose Elias Vargas Lebreault Avatar asked Apr 15 '15 15:04

Jose Elias Vargas Lebreault


People also ask

Does Java 1.8 support lambda expressions?

Lambda expressions are a new and important feature included in Java SE 8. They provide a clear and concise way to represent one method interface using an expression. Lambda expressions also improve the Collection libraries making it easier to iterate through, filter, and extract data from a Collection .

Does Java 11 support lambda expressions?

Java 11 allows to use var in a lambda expression and it can be used to apply modifiers to local variables.

Does Java 7 support lambda expressions?

Retrolambda provides a backport of Lambda expressions for Java 5, 6 and 7. From the Retrolambda documentation: Retrolambda lets you run Java 8 code with lambda expressions and method references on Java 7 or lower. It does this by transforming your Java 8 compiled bytecode so that it can run on a Java 7 runtime.


3 Answers

Edit

As per your comments, you are not using any IDE and your javac version is 1.7. You need to compile with java 8, else the lambda won't be recognized.


I will keep this part in my answer as it may solve the problem for someone else, even if this was not the solution in this case.

Your lambda is perfectly fine. The error is probably due to a wrong compiler compliance level setting.

If you use eclipse, refer to this post for how to change the compiler compliance level :

How to change JDK version for an Eclipse project

If you use Netbeans, remember (this is something many people forget) that you have to set both the source and libraries target to 8 if you want it to function properly.


Source enter image description here


Libraries

enter image description here

Calling java -version from the command-line does not mean that the proper library is set in the IDE, so you have to verify this.

If you use any other IDE then google "How to change jdk version on YourIDE".

like image 110
Jean-François Savard Avatar answered Oct 21 '22 12:10

Jean-François Savard


And if you are using intellij by any chance, please make sure Project language level is set to 8 - Lambdas..

enter image description here

like image 5
Mukesh Kumar Avatar answered Oct 21 '22 12:10

Mukesh Kumar


boton.addActionListener(event -> System.out.println("Hello World!")); - Lambda Expression is perfectly fine. You are passing event to actionPerformed method of ActionListener functional interface which will be mapped while compilation of code using java 8 compiler.

Please check your java version.

like image 2
kavita Avatar answered Oct 21 '22 12:10

kavita