• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Java SE 8 for the Really Impatient: Are the new language features a blessing or a curse?

 
Greenhorn
Posts: 11
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Cay,

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?

Thanks and regards
Ralf
 
author
Posts: 284
35
  • Likes 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Cheers,

Cay
 
author & internet detective
Posts: 42056
926
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well said!
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Cay Horstmann
author
Posts: 284
35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> 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 :-)
 
Sheriff
Posts: 22818
132
Eclipse IDE Spring Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Cay Horstmann wrote:


Why apply filter twice? Why not just once:

 
Cay Horstmann
author
Posts: 284
35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure, that's better--it's a bit more efficient.
 
Would anybody like some fudge? I made it an hour ago. And it goes well with a tiny ad ...
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic