This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Features new in Java 8 and the fly likes What is meant by Lambda Expressions? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Features new in Java 8
Bookmark "What is meant by Lambda Expressions?" Watch "What is meant by Lambda Expressions?" New topic
Author

What is meant by Lambda Expressions?

Palak Mathur
Ranch Hand

Joined: Jan 29, 2007
Posts: 313

Hi,

I will admit that I have not read the JSR335. Just want to have a quick introduction of Lambda Expressions. Is it something similar to or based on Lambda Calculus?


Palak Mathur | My Blog | TechJaunt | What is JavaRanch? | List of All FAQs
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
I think it is supposed to be similar, yes.
Rick Goff
Ranch Hand

Joined: Nov 14, 2006
Posts: 54

The term does come from the Lambda Calculus, which was the inspiration for LISP. A lambda expression is an anonymous function. Think of a case when you created an anonymous class in order to perform a method contained in it.

If you're familiar with Delegates in C#, then you can think of this JSR as Java's initiative to do likewise.

OCPJP 6
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

Wikipedia wrote: Lambda calculus (also written as λ-calculus or called "the lambda calculus") is a formal system in mathematical logic for expressing computation by way of variable binding and substitution.


On the similar lines Lambda Expressions in Java allow you to capture variables declared outside the scope of the lambda expression assuming the variable is eventually final i.e though the variable is not declared as final its value is not updated before its used in the Lambda expression- this is also called as a closure.
There is a problem associated with Anonymous Inner classes that they can only refer to variables outside its scope which are declared as final. The Lambda expressions can be used to overcome this restriction.

All those anonymous inner class instantiation of Single Abstract Method (SAM) classes which would be called as Functional Interfaces in JSR-335 can be replaced with Lambda expressions. The type inference mechanism would convert the corresponding Lambda expression into an instance of that SAM class.

For Example:

but with JSR 335 in Java 8 one could write the above code as:


This is just scratching the surface of JSR 335. The collections API is being enhanced with new APIs which allows you to perform operations like: arrayList.filter(lambda1).map(lambda2).into(newArrayList).


Mohamed Sanaulla | My Blog
Claude Moore
Ranch Hand

Joined: Jun 24, 2005
Posts: 430
    
    1

Hi Mohamed,

your example is very clear, thanks for that.
Stas Sokolov
Ranch Hand

Joined: Apr 13, 2004
Posts: 117

Is there any Java framework / JVM extension that already in one way or another mimics this syntax ?


Good luck for yourself.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

Stas Sokolov wrote:Is there any Java framework / JVM extension that already in one way or another mimics this syntax ?

You can have a look at the JVM based language called Scala.
Stas Sokolov
Ranch Hand

Joined: Apr 13, 2004
Posts: 117

Mohamed Sanaulla wrote:
Stas Sokolov wrote:Is there any Java framework / JVM extension that already in one way or another mimics this syntax ?

You can have a look at the JVM based language called Scala.


It looks not so good. Syntax is unreadable. Somebody who decided to use a class body to define lines of constructor between different methods was just insane. This one Kotlin is a little bit easier and structural but I still like to stick with all good Java just using the same nice things that were defined in Project Coin and then Oracle took them off.

Rick Goff
Ranch Hand

Joined: Nov 14, 2006
Posts: 54

I'm not sure it's due diligence for you to glance at a language Martin Odersky and others took years to produce, and declare it unreadable. What else is unreadable to you? Russian? Mandarin?

Sorry if that's snarky. More constructively, I suggest you post a sample of "unreadable" Scala and see if we can work out what it means.
Palak Mathur
Ranch Hand

Joined: Jan 29, 2007
Posts: 313

Stas Sokolov wrote:
Mohamed Sanaulla wrote:
Stas Sokolov wrote:Is there any Java framework / JVM extension that already in one way or another mimics this syntax ?

You can have a look at the JVM based language called Scala.


It looks not so good. Syntax is unreadable. Somebody who decided to use a class body to define lines of constructor between different methods was just insane. This one Kotlin is a little bit easier and structural but I still like to stick with all good Java just using the same nice things that were defined in Project Coin and then Oracle took them off.



Stas,

You are making up your mind just after a glance. Just give sometime to understand the philosophy behind the language, try to build few things with it and then you would be in a better position to judge a language.
Stas Sokolov
Ranch Hand

Joined: Apr 13, 2004
Posts: 117

Palak Mathur wrote:
Stas Sokolov wrote:
Mohamed Sanaulla wrote:
Stas Sokolov wrote:Is there any Java framework / JVM extension that already in one way or another mimics this syntax ?

You can have a look at the JVM based language called Scala.


It looks not so good. Syntax is unreadable. Somebody who decided to use a class body to define lines of constructor between different methods was just insane. This one Kotlin is a little bit easier and structural but I still like to stick with all good Java just using the same nice things that were defined in Project Coin and then Oracle took them off.



Stas,

You are making up your mind just after a glance. Just give sometime to understand the philosophy behind the language, try to build few things with it and then you would be in a better position to judge a language.


I did my research comparing Java and Scala a while ago and I even wrote some performance tests comparing Java and Scala. Java wins because it support primitive arrays when in Scala every primitive type is an object.
Stas Sokolov
Ranch Hand

Joined: Apr 13, 2004
Posts: 117

Rick Goff wrote:I'm not sure it's due diligence for you to glance at a language Martin Odersky and others took years to produce, and declare it unreadable. What else is unreadable to you? Russian? Mandarin?

Sorry if that's snarky. More constructively, I suggest you post a sample of "unreadable" Scala and see if we can work out what it means.


Any Scala code premier looks like an ugly PERL script. It might be shorter than Java in some way, but I would give +1 for this and -10 for readability.
Rick Goff
Ranch Hand

Joined: Nov 14, 2006
Posts: 54

Stas Sokolov wrote:
Rick Goff wrote:I'm not sure it's due diligence for you to glance at a language Martin Odersky and others took years to produce, and declare it unreadable. What else is unreadable to you? Russian? Mandarin?

Sorry if that's snarky. More constructively, I suggest you post a sample of "unreadable" Scala and see if we can work out what it means.


Any Scala code premier looks like an ugly PERL script. It might be shorter than Java in some way, but I would give +1 for this and -10 for readability.


2. The "object" keyword creates a singleton, which is appropriate to this case. "primes" is the name of this object. "extends Application" is a poorly chosen but valid way to mark this object as runnable.
3. def means "I'm defining a function." (n: Int) means "take an argument which must be of Int type." (2 until n) should be (2 until n/2) but in any case it means "start at 2 and proceed as far as n. "
forall means "if the predicate on my right is true for every item (n) in the iterator on my left."
(n % _ != 0) means "n divided by anything does not have a remainder of 0."
isPrime(n) returns the boolean resut of the forall test.
4. for every number between 1 and 100, test it using the function we defined on line 2. If true, print the value and a carriage return.



Milan Sutaria
Ranch Hand

Joined: Jul 10, 2008
Posts: 118
wow that was good to know upcoming features in Java. Looking forward to give a SCJP 8 sample test before go live.


SCJP 6 83%
Working on SCWCD/OCPJWCD
Hendra Kurniawan
Ranch Hand

Joined: Jan 31, 2011
Posts: 239
AFAIK, lambda expressions exist only in .Net technology. Does this mean Java will also introduce this feature in the next release?
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

Hendra Kurniawan wrote:AFAIK, lambda expressions exist only in .Net technology. Does this mean Java will also introduce this feature in the next release?


Yes, they are being added as part of Java 8 - Project Lambda
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1621
    
  13

Hendra Kurniawan wrote:AFAIK, lambda expressions exist only in .Net technology.

Actually, lambda expressions (or anonymous functions) have been around in a lot of other languages for quite a long time. Java is still playing catch-up.


No more Blub for me, thank you, Vicar.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
chris webster wrote:
Hendra Kurniawan wrote:AFAIK, lambda expressions exist only in .Net technology.

Actually, lambda expressions (or anonymous functions) have been around in a lot of other languages for quite a long time. Java is still playing catch-up.

Why is this something an object-oriented language should be expected to "catch-up" to?
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2999
    
    9
Dennis Deems wrote:Why is this something an object-oriented language should be expected to "catch-up" to?

Well, everyone can have their own opinion of course. But here's what James Gosling had to say about it:
Closures were left out of Java initially more because of time pressures than anything else. Closures, as a concept, are tried and true - well past the days of being PhD topics. The arguments are in the details, not the broad concepts. In the early days of Java the lack of closures was pretty painful, and so inner classes were born: an uncomfortable compromise that attempted to avoid a number of hard issues. But as is normal in so many design issues, the simplifications didn't really solve any problems, they just moved them. We should have gone all the way back then

Here he's talking about closures rather than lambda expressions, but the two are pretty similar. A true closure would also be able to access local non-final variables, which lambdas cannot. A few years ago many people in the Java community were using "closure" indiscriminately for things that weren't really closures; nowadays "lambda expression" is more common.

Speaking for myself, I want lambdas in an object-oriented language like Java because:

* They are visually cleaner and more concise than anonymous classes.

* Rather than saying we need to create an object to contain a function, we take the attitude that a function is an object.

* They allow us to reduce a lot of boilerplate code in Java through library additions rather than requiring language changes. E.g. the enhanced for loop and try-with-resources would have been easily implemented with library code, not language changes, if we'd had lambdas in the first place.

* Most of the other newer languages that compete with Java have lambdas (or something like them) already: C#, Ruby, Groovy, Scala, Clojure, Kotlin, Ceylon, and more
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Oracle's bimonthly "Java Magazine" just came out the other day, and it has an article explaining lambda expressions.

Article here: Explaining Lambda Expressions for the Java Language and the JVM.

I found it was a very readable article and the examples were short and to the point. Check it out if you haven't already seen it.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2999
    
    9
Nice. But can we really trust an article co-written by that Verburg guy?

The home page at http://www.oraclejavamagazine-digital.com/ seems currently unable to do anything useful unless you're an oracle employee. However here is a useful link for others interested in subscribing: http://www.oracle.com/technetwork/java/javamagazine/index.html
Anton Spektorov
Greenhorn

Joined: May 07, 2013
Posts: 1

So, to summarize, does it mean that Java is going to adopt Groovy syntax?


SCJP, SCJD, SCEA
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2999
    
    9
Sort of. The idea of lambdas is present in a bunch of other languages, not just Groovy. And there's no indication Java is adopting any other Groovy syntax. But adding lambdas to Java can be seen as a big move in the direction of Groovy (and Ruby and Scala and C# and JavaScript and heaps of others).
Rick Goff
Ranch Hand

Joined: Nov 14, 2006
Posts: 54

It's tempting to someone who has always thought of programming from an "object-oriented" perspective to conquer their dissidence about new features by analogizing them to tools they recognize. If you're really interested in the value of Lambda expressions, you need to stop thinking like a Java programmer (exclusively), and start thinking like a problem solver who values concurrency and parallelism.

Ask yourself this: why has Lisp survived the entire first 55 year period of the history of programming? Maybe read a short intro to Lisp or Haskell, and see if you can find the appeal.

As Java programmers, we have conceded the inevitability of shared mutable state. What if we didn't?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Stas Sokolov wrote:... in Scala every primitive type is an object.

That is not really true. In Scala, things like Int look and feel like an object, but the compiler translates them to primitives at the JVM level. So they're not really objects under the covers, for efficiency.

The way Scala does it is better than how it works in Java. Primitive types are really a low-level concept that doesn't need to be exposed to the programmer directly. Ofcourse the compiler should use them under the covers, but there's really no reason why the programmer should need to deal with primitive types directly.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is meant by Lambda Expressions?
 
Similar Threads
Is there a clear list and description of new Java 7 features?
Is this a closure?
Lambdas == closures == anonymous functions ?
why do University graduates have a tough time finding work
Does groovy break Java (philosophically)?