Hacker News new | ask | show | jobs
by eveningcoffee 4189 days ago
Here is an example from Spark Framework (a tiny web framework) written in Java 8.

import static spark.Spark.* ;

public class HelloWorld {

    public static void main(String[] args) {
        get("/hello", (req, res) -> "Hello World");
    }
}

This is more simple as written in previous version and is also consistent with it as it can be written in Java 7 (or earlier) like this.

import static spark.Spark.* ;

public class HelloWorld {

    public static void main(String[] args) {
        get("/hello", new Route() {
	    @Override
	    public Object handle(Request req, Response res) 
                                                throws Exception {
                return "Hello World";
            }
        });
    }
}

You can see how closures in Java actually represent more familiar idea and by omitting unnecessary ceremony do make writing in it more simple.