• Post Reply Bookmark Topic Watch Topic
  • New Topic

java.util.Optional requires discipline to avoid nulls

 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I find java.util.Optional to be a poor man's Maybe type from Haskell. NullPointerException are still possible with it because nulls is a first class feature of the JVM (Unlike in haskell).
So:



The above code still throws a NPE.
Does findbugs detect this kind of problems ?
 
Campbell Ritchie
Marshal
Posts: 52549
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you will write code breaching the principles you find in Effective Java™ by Joshua Bloch, for example, what do you expect?
 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have a point but I'm just pointing out that because Java has null references, we need static code analysis to detect & flag code that returns null for a type wrapper with Optional. Without it, Optional does not prevent NPE.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!