• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Java 8 Optional

 
Ranch Hand
Posts: 403
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prior to Java 8, we used to write the code:



And, using Optional, we write :




In both the cases, we have to put the if conditions, so, what is the advantage of optional actually? There, we used to check if obj is not null and here we call ifPresent method, so,what is the difference?
 
Master Rancher
Posts: 172
7
IntelliJ IDE Spring Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the big advantages of Optionals is that it allows to avoid NullPointerExceptions in a more elegant way. For example, in case a variable has a value of null, you can specify an alternative action, e.g. returning a default value.

I am also just learning about Optionals and I found this page very useful.
 
Vaibhav Gargs
Ranch Hand
Posts: 403
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, is it safe to say that using Optional also we can't avoid null pointer exceptions. In null handling code, we need to check for not null and in optional we need to check for ifpresent.

Hence, there doesn't seem any difference between the two. Please let me know if I am missing anything.
 
Saloon Keeper
Posts: 10494
224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The difference is that Optional forces the programmer to think about it, because you can't use an Optional<Foo> where a Foo is expected, while you CAN use null where a Foo is expected. Additionally, Optional provides methods to perform transformations and actions optionally, i.e. only when there is a value present, without calling isPresent().
 
Marshal
Posts: 65365
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Vaibhav Gargs wrote:. . . . Please let me know if I am missing anything.

Yes, you are missing the orElse()/orElseSomething() methods. Those methods allow you to return some sort of other value if the Optional is empty. Did you read the Baeldung link or Urma's article? If you have any possible risk of a null, writeAnother advantage of the orElseGet() method is that in the likely instance of the Optional not being empty, the code used as an argument to orElseGet() isn't executed, which may cause faster execution.
Avoid the of() and get() methods if possible, because they will throw an exception immediately if presented with a null.
 
What's that smell? I think this tiny ad may have stepped in something.
Enterprise-grade Excel API for Java
https://products.aspose.com/cells/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!