aspose file tools*
The moose likes Features new in Java 8 and the fly likes Java SE 8 for the Really Impatient: Are the new language features a blessing or a curse? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Features new in Java 8
Bookmark "Java SE 8 for the Really Impatient: Are the new language features a blessing or a curse?" Watch "Java SE 8 for the Really Impatient: Are the new language features a blessing or a curse?" New topic
Author

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

Ralf Pantförder
Greenhorn

Joined: May 10, 2010
Posts: 8
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

Joined: Nov 14, 2004
Posts: 115
    
  11
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 of Java 8 for the Really Impatient
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30516
    
150

Well said!


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

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

Joined: Nov 14, 2004
Posts: 115
    
  11
> 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

Joined: Oct 27, 2005
Posts: 19693
    
  20

Cay Horstmann wrote:

Why apply filter twice? Why not just once:



SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Cay Horstmann
author
Ranch Hand

Joined: Nov 14, 2004
Posts: 115
    
  11
Sure, that's better--it's a bit more efficient.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java SE 8 for the Really Impatient: Are the new language features a blessing or a curse?