aspose file tools*
The moose likes Features new in Java 8 and the fly likes Java 8 Optional 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 8 Optional" Watch "Java 8 Optional" New topic
Author

Java 8 Optional

Sean Corfield
Ranch Hand

Joined: Feb 09, 2011
Posts: 267
    
    5

Richard,

Looking at your book's table of contents, I see you cover the new Optional class briefly under Streams.

Other languages use similar types (Maybe, Option) as a way to program safely without worrying about null pointers. How realistic do you think it is for Java developers to adopt Optional in practice and move away from using null, given all the existing library methods that return null as a matter of course?
Richard Reese
author
Ranch Hand

Joined: Jul 13, 2011
Posts: 84
    
    5

Sean,

I wouldn’t expect existing libraries to be rewritten using the Optional class. It can be used to create better libraries in the future. One of the nice things about using the Optional class is that it will force you to deal with null values. You will have to actively consider the possibility that a null result may be encounter. This will make your code more stable. Of course this comes with a cost: more time spent developing your code. The payback is arguably better code.


Richard Reese
Java 8 New Features: A Practical Heads-Up Guide
Sean Corfield
Ranch Hand

Joined: Feb 09, 2011
Posts: 267
    
    5

As a specific example, when writing new code, do you think it's worth using Optional.ofNullable( legacyLibraryCall( some, args ) ) to wrap old (possibly null-returning) library calls so that your code can be null-free? Or do you think that it's not worth the effort at that level?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java 8 Optional