This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I'd like to know your opinion about the benefit of the new language features of Java 8, like lambda expressions and the :: operator.
New Java language features of course make code more concise and coding more handy, but on the other hand they may considerably increase effort in (1) learning the language, (2) understanding code, (3) understanding what the compiler does, and, quite often, (4) debugging! Think of weird phenomena like a line of code as simple as i++; that triggers a NullPointerException due to autoboxing/-unboxing.
I'm personally not yet sure whether or not I like lambda expressions and the :: operator under these aspects. How do you feel about this?
There are two reasons why I think that these particular language features are worth learning. (1) They let you express what you want to do more simply (2) They make it easier to parallelize your work.
Here is an example of the former. In my CS1 course, I give simple loop problems, such as "find all words that are long (> 10 characters) and end in the letter y." So, students have to write a loop with an if and a &&, and then, right then and there, they must decide where to put the result. Should they be printed? Collected in an array list?
With streams, you just say
and you get another stream back. You can print, collect into an array list, or pass it along somewhere else.
And of course, if you wanted to parallelize the computation, it's easy to do:
I agree with you that autoboxing was a much more dubious language feature. But lambdas are worthwhile.
Lambdas don't bring in anything what couldn't be - in principle - done using interfaces and anonymous classes (the control is passed back and forth just the same), so if you can understand and debug those, you shouldn't have problems with lambdas. They're just much more concise.
If I may chime in - recently I had to learn Ruby. I have mixed feelings towards that language. The syntax is terribly chaotic (though every Ruby tutorial I've seen says otherwise, so perhaps the problem is on my side ), but it has lambdas (called 'blocks' in Ruby). It does take some time to get used to them, especially if they get a little bit more convoluted, but they are definitely worth it.
Lambdas are used very extensively in Ruby. Lots of your code actually sits in lambdas there. For example, when you want to process a file (or any other resource), you call a function which accepts a lambda. The function opens the file, calls your lambda (which includes your processing) and then closes the file, so you don't have to think about it at all. I find this very handy, however Java already has the try-with-resources, so I wouldn't say this construct will get widespread in the Java world.
Joined: Nov 14, 2004
> The syntax is terribly chaotic (though every Ruby tutorial I've seen says otherwise)
A key difference between Ruby and Java is the user population. In Ruby, a certain level of adulation is expected. And it's not just Ruby. Many other languages and technologies are replete with blogs and tutorials that breathlessly proclaim their virtues, often on shallow grounds, whereas the Java world is filled with curmudgeons :-)