Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Jva 8 lambdas!

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Red face Jva 8 lambdas!

    Is anyone else excited about this? I am so excited! I learned the same type of thing in a scripting language that is very similar to Java in a video game, so I'm good to go
    What else about Java 8 seems interesting to you? Can anyone explain what exactly Internet of Things is?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,373
    My Mood
    Mellow
    Thanks
    143
    Thanked 598 Times in 585 Posts

    Default Re: Jva 8 lambdas!

    Please describe why lambda expressions are so exciting to you. I'm not getting it.

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,600
    Thanks
    24
    Thanked 313 Times in 293 Posts

    Default Re: Jva 8 lambdas!

    I get why lambda expressions are nice to have, but I'm skeptical of how big of a problem it really is. Until I see some statistics on how big of an advantage it is for java to have lambda expressions, I won't be convinced. Based on what I've read, it seems to just be a shorthand way of doing functional interfaces. Considering that functional interfaces are really not that verbose already, it seems like an unnecessary addition.

    That being said, I do find myself using the shorthand if statement more than I expected when I learned about it, so maybe I'm just being obtuse.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,373
    My Mood
    Mellow
    Thanks
    143
    Thanked 598 Times in 585 Posts

    Default Re: Jva 8 lambdas!

    Well, when you find a use for a lambda expression that you think is indicative of their advantage, please share it.

    Still waiting to hear why Tea.EarlGrey.Hot is so excited, but I'm patient.

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,600
    Thanks
    24
    Thanked 313 Times in 293 Posts

    Default Re: Jva 8 lambdas!

    I've seen examples of uses for them, but none that scream: this is why lambda expressions are so great.
    It seems all the examples could be done with anonymous classes, which is quite easy to do...

    For example:
    No Lambda:
    btn.setOnAction(new EventHandler<ActionEvent>() {
     
                @Override
                public void handle(ActionEvent event) {
                    System.out.println("Hello World!");
                }
            });
    With Lambda:
    btn.setOnAction(
              event -> System.out.println("Hello World!")
            );
    Less verbose? Sure. Significant advantage? Not in my opinion.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  6. #6
    Junior Member
    Join Date
    Mar 2014
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Jva 8 lambdas!

    Lol it's just because it's a lot more efficient :3 ofc u should understand both ways. But I haven't started with Java 8 yet

  7. #7
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,600
    Thanks
    24
    Thanked 313 Times in 293 Posts

    Default Re: Jva 8 lambdas!

    Efficient for initially coding, but how efficient is it for debugging/code tracing?
    I fear, based on the examples I've seen, that a widespread use of lambdas (as with most shortcuts) will make code less readable (compared to the "old" non-lambda way of doing things), which severely harms long-term efficiency.
    This becomes a huge problem when we start seeing people try to nest shortcuts.

    For example, which one of these would be easier to debug (ignore the stupid logic in these if/else statements, I just wrote a bunch of random crap)?
    Shortcut If/Else:
    int input = 1456;
    int value = input%12==0 ? (input%4==0 ? 4 : (input%3==0 ? 3 : 2)) : (input%15==0 ? (input%5==0 ? 5 : (input%3==0 ? 3 : 15)) : input);
    Normal If/Else:
    int input = 1456;
    int value = input;
    if(input%12==0) {
    	if(input%4==0) {
    		value = 4;
    	}
    	else if(input%3==0) {
    		value = 3;
    	}
    	else {
    		value = 2;
    	}
    }
    else if(input%15==0) {
    	if(input%5==0) {
    		value = 5;
    	}
    	else if(input%3==0) {
    		value = 3;
    	}
    	else {
    		value = 15;
    	}
    }

    Sure, the shorthand way is more efficient and faster to type, but god have mercy on whatever poor SOB has to debug/maintain that code in the future. Ultimately, the "long way" of writing that logic may take much longer, but is FAR easier to manage.

    The same principle applies to the lambda expressions. As with all shortcuts, it must be used responsibly, but probably won't be...
    Last edited by aussiemcgr; April 7th, 2014 at 10:57 PM.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  8. #8
    Junior Member
    Join Date
    Mar 2014
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Jva 8 lambdas!

    Haha, that's a good point :o
    I guess we will see if it causes trouble soon enough.

  9. #9
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,225
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Jva 8 lambdas!

    My 0.02 on lambda's: they add a function programming aspect to Java that has been missed for years, and can make development much easier in situations where function programming may be a better option (and mindset). This may not be in every application, but these situations can be encountered during development when the data model doesn't change relative to the frequency of the operations on the data. A simple example is a calculator - the data model virtually does not change (numbers). One may create a calculator to do basic math. Then, requirements need to be met to add some statistics (mean, standard deviation, etc...). Then requirements come in to do nested (find even numbers then count frequency, 2-level sorting, etc...) or recursive operations (prime numbers, sorting, ...). Taking a functional programming approach, in my view, is a much better approach to tackling this sort of development: quicker, cleaner, and more maintainable - no need to constantly implement an interface or method (and create instances of), the interface that defines behavior may need changing (or a new interface created) to adapt to different ways to manipulate the data (both of which may require full project refactoring), no need to explicitly define Collection loops (Streams do it for you - and in parallel if necessary).

Similar Threads

  1. Jva Game error: Exection in thread "main" java.lang.nullpointerexception
    By Amartin4200 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 14th, 2013, 06:22 AM