Results 1 to 4 of 4
  1. #1
    Iskildur is offline Member
    Join Date
    Oct 2016
    Posts
    1
    Rep Power
    0

    Default Pondering about Lambda expressions

    I'm currently taking two online courses on Java and I'm getting into the more advanced features I guess. One of the courses uses an example code like this for lambda expressions.

    Java Code:
    interface Ball {
        public void kickTheBall();
    }
    
    class KickIt {
        public void kick(Ball b) {
            System.out.println("Kicking the ball");
            b.kickTheBall();
        }
    }
    
    public class LambdaTest {
            public static void main(String[] args) {
                    KickIt Kicker = new KickIt();
    
                    // Verbose way.
                    Kicker.kick(new Ball() {
                            public void kickTheBall() {
                                    System.out.println("Went far!\n");
                            }
                    });
    
                    // Lambda way.
                    Kicker.kick(() -> System.out.println("Went far!"));
            }
    }
    So, what I'm stuck at a little is the level of implicity and/or interpretation of the Lambda expression. Kicker.kick() explicitly expects a Ball() object, with an inner public void kickTheBall() method to be executed. And all the Lambda expression provides is a blank () object and a value. It seems like an extreme amount of inferring.

    Lambda: "Hey there Kicker! Here's an anonymous class and a value for your kick(), you'll know what to do! Kind regards, Mighty Lambda!"

    Kicker: "Uhm.. I guess I'll just dynamically adapt the () object to Ball then and Uhm you're supposed to give me a kickTheBall method to fire as well but... I guess I'll just make one for you and put the value in there."

    I like things strongly typed, explicit. I guess that's why I'm having some difficulty with this. Anyone experienced that feels the same way, or is it just newbness and inexperience of me to think so? The reason I'm going for Java is Android apps. I grasped the simpler parts of the language quickly. But the closest I've come to Lambdas before is ternary if/else operations. I've had minimal experience with LINQ and lambdas in C# before for simple array operations where less code made more sense, but I don't think I ever grasped them fully.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Pondering about Lambda expressions

    The interface with a single abstract method is known as a functional interface. The compiler
    finds the interface, extrapolates the appropriate types and generates the code. Pretty much
    like you said. The compiler also does some rather interesting things when you employ method
    references too (which you will find out when you get to that topic in your courses).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    399
    Rep Power
    6

    Default Re: Pondering about Lambda expressions

    There is nothing wrong with functional programming the "old school" way. You will start to appreciate lambda expressions after you have typed out the original (pre-Java8) way 20+ times.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Pondering about Lambda expressions

    You're not wrong.
    Swing ActionListeners come to mind here...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Help with lambda expressions
    By Sam_JavaTheHut5580 in forum New To Java
    Replies: 9
    Last Post: 09-08-2016, 08:42 PM
  2. Regular Expressions Help
    By Death Sickle in forum New To Java
    Replies: 4
    Last Post: 04-04-2011, 04:21 AM
  3. Help: Mathematical Expressions
    By JohnAtick in forum New To Java
    Replies: 3
    Last Post: 03-09-2009, 06:02 PM
  4. JSP Expressions
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-26-2007, 10:13 AM
  5. JSP Expressions
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-02-2007, 09:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •