• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Ralf Pantförder
Greenhorn
Posts: 8
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
 
Cay Horstmann
author
Ranch Hand
Posts: 172
15
  • Likes 4
  • Mark post as helpful
  • send pies
  • 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
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34851
369
Eclipse IDE Java VI Editor
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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
Ranch Hand
Posts: 172
15
  • Mark post as helpful
  • send pies
  • 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 :-)
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cay Horstmann wrote:

Why apply filter twice? Why not just once:

 
Cay Horstmann
author
Ranch Hand
Posts: 172
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure, that's better--it's a bit more efficient.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic