Hands on Optional value
Optional is in air due to coronavirus, everything is becoming optional like optional public gathering , optional work from home, optional travel etc.

I though it is good time to talk about real “Optional” in software engineering that deals with NULL reference.
Tony Hoare confessed that he made billion dollar mistake by inventing Null. If you have not seen his talk then i will suggest to have look at Null-References-The-Billion-Dollar-Mistake.
I will share some of the anti pattern with null and how it can be solved using abstraction like Optional or MayBe.
For this example we will use simple value object that can have some null values.
1 2 3 4 5 6 7 | public class Person { final String firstName; final String lastName; final String email; // This can be null final String phone; //This can be null } |
This value object can have null value for email & phone number.
Scenario: Contact Person on both email and phone number
Not using optional
First attempt will be based on checking null like below
1 2 3 4 5 6 7 8 | //Not using optional if (p.email != null ) { System.out.println( "Sending email to " + p.email); } if (p.phone != null ) { System.out.println( "Calling " + p.phone); } |
This is how it has been done for years. One more common pattern with collection result.
1 2 3 4 5 6 7 | List<Person> p = searchPersonById( "100" ); if (p.isEmpty()) { System.out.println( "No result" ); } else { System.out.println( "Person" + p.get( 0 )); } |
Use optional in wrong way
1 2 3 4 5 6 7 8 9 | Optional<String> phone = contactNumber(p); Optional<String> email = email(p); if (phone.isPresent()) { System.out.println( "Calling Phone " + phone.get()); } if (email.isPresent()) { System.out.println( "Sending Email " + email.get()); } |
This is little better but all the goodness of Optional is thrown away by adding if/else block in code.
Always Happy optional
1 2 3 4 5 6 | //Always Happy Optional<String> phone = contactNumber(p); Optional<String> email = email(p); System.out.println( "Calling Phone " + phone.get()); System.out.println( "Sending Email " + email.get()); |
It is good be happy but when you try that with Optional you are making big assumption or you don’t need optional.
Nested property optional
For this scenario we will extend Person object and add Home property. Not everyone can own home so it is good candidate that it will be not available .
Lets see how contacting person scenario work in this case
1 2 3 4 5 6 7 8 9 | //Nested Property if (p.getHome() != null ) { System.out.println( "Sending Postal mail " + p.getHome().address); } if (p.getHome() != null && p.getHome().getInsurance() != null ) { System.out.println( "Sending Notification to insurance " + p.getHome().getInsurance().getAgency()); } |
This is where it start to become worse that code will have tons of nested null checks.
Priority based default
for this scenario we first try to contact person on home address and if it is not available then contact on office address.
01 02 03 04 05 06 07 08 09 10 11 | //Address has priority , first home and then Office if (p.home != null ) { System.out.println( "Contacted at home address " + p.home.address); return ; // Magical return for early exit } if (p.office != null ) { System.out.println( "Contacted at office address " + p.office.address); return ; // Magical return for early exit } |
Such type of scenario require use of advance control flow for early return and makes code hard to understand and maintain.
These are some of the common pattern where optional are not used or used in wrong way.
Optional usage patterns
Lets look at some of good ways of using optional.
Make property optional based on domain knowledge
It is very easy to makes property optional.
1 2 3 4 5 6 7 | public Optional<String> getEmail() { return Optional.ofNullable(email); } public Optional<String> getPhone() { return Optional.ofNullable(phone); } |
Yes it is allowed to make get Optional, no one will hang you for that and feel free to do that without fear. Once that change is done we can write something like below
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | //Use Optional p.getEmail().ifPresent(email -> System.out.println( "Sending email to " + email)); p.getPhone().ifPresent(phone -> System.out.println( "Calling " + phone)); //Optional for Collection or Search type of request Optional<person> person= "persons.stream().findFirst();" person.ifpresent(system.out::println);= "" <= "" pre= "" ><p>It looks neat, first step to code without explicit if else on application layer.</p><p>Use some power of Optional</p><pre class = "brush:java" > //Use IfPresent & other cool things phone .filter(number -> hasOptIn(number)) .ifPresent(number -> System.out.println( "Calling Phone " + number)); email .filter(m -> hasOptIn(m)) .ifPresent(m -> System.out.println( "Sending Email " + m)); </pre><p>Optional is just like stream, we get all functional map,filter etc support. In above example we are checking for OptIn before contacting.</p><p>Always happy optional</p><p>Always happy optional that calls<i> "get" </i> without check will cause runtime error on sunday midnight, so it advised to use ifPresent</p><pre class = "brush:java" > //Don't do this System.out.println( "Calling Phone " + phone.get()); System.out.println( "Sending Email " + email.get()); //Use ifPresent to avoid runtime error phone.ifPresent(contact -> System.out.println( "Sending email to " + contact)); email.ifPresent(contact -> System.out.println( "Calling " + contact)); </pre><p>Nested Optional</p><pre class = "brush:java" >p.getHome().ifPresent(a -> System.out.println( "Sending Postal mail " + a.address)); p.getHome() .flatMap(Person.Home::getInsurance) .ifPresent(a -> System.out.println( "Sending Notification to insurance " + a.agency)); </pre><p>Flatmap does the magic and handles null check for home and convert insurance object also.</p><p>Priority based default </p><pre class = "brush:java" > //Address has priority , first home and then Office Optional<String> address = Stream .of(person.getHome().map(Home::getAddress), person.getOffice().map(Office::getAddress)) .filter(Optional::isPresent) .map(Optional::get) .findFirst(); address .ifPresent(add -> System.out.println( "Contacting at address " + add)); </pre><p>This example is taking both home & office address and pick the first one that has value for sending notification. This particular pattern avoids lots of nested loops.</p><p>Else branch</p><p>Optional has lots of ways to handle else part of the scenario like returning some default value(orElse) , lazy default value (orElseGet) or throw exception(orElseThrow).</p><p>What is not good about optional</p><p>Each design choice has some trade off and optional also has some. It is important to know what are those so that you can make careful decision.</p><p>Memory overhead</p><p>Optional is container that holds value, so extra object is created for every value. Special consideration is required when it holds primitive value. If some performance sensitive code will be impacted by extra object creation via optional then it is better to use null .</p><p>Memory indirection</p><p>As optional is container , so every access to value need extra jump to get real value. Optional is not good choice for element in array or collection.</p><p>No serialization</p><p>I think this is good decision by Jdk team that does not encourage people to make instance variable optional. You can wrap instance variable to Optional at runtime or when required for processing.</p><div class = "attribution" ><table><tbody><tr><td><p>Published on Java Code Geeks with permission by Ashkrit Sharma, partner at our <a href= "//www.javacodegeeks.com/join-us/jcg/" target= "_blank" rel= "noopener noreferrer" >JCG program</a>. See the original article here: <a href= "http://ashkrit.blogspot.com/2020/03/hands-on-optional-value.html" target= "_blank" rel= "noopener noreferrer" >Hands on Optional value</a></p><p>Opinions expressed by Java Code Geeks contributors are their own.</p></td></tr></tbody></table></div><style>.lepopup-progress- 60 div.lepopup-progress-t1>div{background-color:#e0e0e0;}.lepopup-progress- 60 div.lepopup-progress-t1>div>div{background-color:#bd4070;}.lepopup-progress- 60 div.lepopup-progress-t1>div>div{color:#ffffff;}.lepopup-progress- 60 div.lepopup-progress-t1>label{color:# 444444 ;}.lepopup-form- 60 , .lepopup-form- 60 *, .lepopup-progress- 60 {font-size:15px;color:# 444444 ;font-style:normal;text-decoration:none;text-align:left;}.lepopup-form- 60 .lepopup-element div.lepopup-input div.lepopup-signature-box span i{font-family: 'Arial' , 'arial' ;font-size:13px;color:# 555555 ;font-style:normal;text-decoration:none;text-align:left;}.lepopup-form- 60 .lepopup-element div.lepopup-input div.lepopup-signature-box,.lepopup-form- 60 .lepopup-element div.lepopup-input div.lepopup-multiselect,.lepopup-form- 60 .lepopup-element div.lepopup-input input[type= 'text' ],.lepopup-form- 60 .lepopup-element div.lepopup-input input[type= 'email' ],.lepopup-form- 60 .lepopup-element div.lepopup-input input[type= 'password' ],.lepopup-form- 60 .lepopup-element div.lepopup-input select,.lepopup-form- 60 .lepopup-element div.lepopup-input select option,.lepopup-form- 60 .lepopup-element div.lepopup-input textarea{font-family: 'Arial' , 'arial' ;font-size:13px;color:# 555555 ;font-style:normal;text-decoration:none;text-align:left;background-color:rgba( 255 , 255 , 255 , 0.7 );background-image:none;border-width:1px;border-style:solid;border-color:#cccccc;border-radius:0px;box-shadow: inset 0px 0px 15px -7px # 000000 ;}.lepopup-form- 60 .lepopup-element div.lepopup-input ::placeholder{color:# 555555 ; opacity: 0.9 ;} .lepopup-form- 60 .lepopup-element div.lepopup-input ::-ms-input-placeholder{color:# 555555 ; opacity: 0.9 ;}.lepopup-form- 60 .lepopup-element div.lepopup-input div.lepopup-multiselect::-webkit-scrollbar-thumb{background-color:#cccccc;}.lepopup-form- 60 .lepopup-element div.lepopup-input>i.lepopup-icon-left, .lepopup-form- 60 .lepopup-element div.lepopup-input>i.lepopup-icon-right{font-size:20px;color:# 444444 ;border-radius:0px;}.lepopup-form- 60 .lepopup-element .lepopup-button,.lepopup-form- 60 .lepopup-element .lepopup-button:visited{font-family: 'Arial' , 'arial' ;font-size:13px;color:#ffffff;font-weight: 700 ;font-style:normal;text-decoration:none;text-align:center;background-color:# 326693 ;background-image:none;border-width:1px;border-style:solid;border-color:# 326693 ;border-radius:0px;box-shadow:none;}.lepopup-form- 60 .lepopup-element div.lepopup-input .lepopup-imageselect+label{border-width:1px;border-style:solid;border-color:#cccccc;border-radius:0px;box-shadow:none;}.lepopup-form- 60 .lepopup-element div.lepopup-input .lepopup-imageselect+label span.lepopup-imageselect-label{font-size:15px;color:# 444444 ;font-style:normal;text-decoration:none;text-align:left;}.lepopup-form- 60 .lepopup-element div.lepopup-input input[type= 'checkbox' ].lepopup-checkbox-tgl:checked+label:after{background-color:rgba( 255 , 255 , 255 , 0.7 );}.lepopup-form- 60 .lepopup-element div.lepopup-input input[type= 'checkbox' ].lepopup-checkbox-classic+label,.lepopup-form- 60 .lepopup-element div.lepopup-input input[type= 'checkbox' ].lepopup-checkbox-fa-check+label,.lepopup-form- 60 .lepopup-element div.lepopup-input input[type= 'checkbox' ].lepopup-checkbox-square+label,.lepopup-form- 60 .lepopup-element div.lepopup-input input[type= 'checkbox' ].lepopup-checkbox-tgl+label{background-color:rgba( 255 , 255 , 255 , 0.7 );border-color:#cccccc;color:# 555555 ;}.lepopup-form- 60 .lepopup-element div.lepopup-input input[type= 'checkbox' ].lepopup-checkbox-square:checked+label:after{background-color:# 555555 ;}.lepopup-form- 60 .lepopup-element div.lepopup-input input[type= 'checkbox' ].lepopup-checkbox-tgl:checked+label,.lepopup-form- 60 .lepopup-element div.lepopup-input input[type= 'checkbox' ].lepopup-checkbox-tgl+label:after{background-color:# 555555 ;}.lepopup-form- 60 .lepopup-element div.lepopup-input input[type= 'radio' ].lepopup-radio-classic+label,.lepopup-form- 60 .lepopup-element div.lepopup-input input[type= 'radio' ].lepopup-radio-fa-check+label,.lepopup-form- 60 .lepopup-element div.lepopup-input input[type= 'radio' ].lepopup-radio-dot+label{background-color:rgba( 255 , 255 , 255 , 0.7 );border-color:#cccccc;color:# 555555 ;}.lepopup-form- 60 .lepopup-element div.lepopup-input input[type= 'radio' ].lepopup-radio-dot:checked+label:after{background-color:# 555555 ;}.lepopup-form- 60 .lepopup-element div.lepopup-input div.lepopup-multiselect>input[type= 'checkbox' ]+label:hover{background-color:#bd4070;color:#ffffff;}.lepopup-form- 60 .lepopup-element div.lepopup-input div.lepopup-multiselect>input[type= 'checkbox' ]:checked+label{background-color:#a93a65;color:#ffffff;}.lepopup-form- 60 .lepopup-element input[type= 'checkbox' ].lepopup-tile+label, .lepopup-form- 60 .lepopup-element input[type= 'radio' ].lepopup-tile+label {font-size:15px;color:# 444444 ;font-style:normal;text-decoration:none;text-align:center;background-color:#ffffff;background-image:none;border-width:1px;border-style:solid;border-color:#cccccc;border-radius:0px;box-shadow:none;}.lepopup-form- 60 .lepopup-element-error{font-size:15px;color:#ffffff;font-style:normal;text-decoration:none;text-align:left;background-color:#d9534f;background-image:none;}.lepopup-form- 60 .lepopup-element- 2 {background-color:rgba( 226 , 236 , 250 , 1 );background-image:none;border-width:1px;border-style:solid;border-color:rgba( 216 , 216 , 216 , 1 );border-radius:3px;box-shadow: 1px 1px 15px -6px #d7e1eb;}.lepopup-form- 60 .lepopup-element- 3 * {font-family: 'Arial' , 'arial' ;font-size:26px;color:# 333333 ;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;}.lepopup-form- 60 .lepopup-element- 3 {font-family: 'Arial' , 'arial' ;font-size:26px;color:# 333333 ;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;background-color:transparent;background-image:none;border-width:1px;border-style:none;border-color:transparent;border-radius:0px;box-shadow:none;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;}.lepopup-form- 60 .lepopup-element- 3 .lepopup-element-html-content {min-height:73px;}.lepopup-form- 60 .lepopup-element- 4 * {font-family: 'Arial' , 'arial' ;font-size:19px;color:# 333333 ;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;}.lepopup-form- 60 .lepopup-element- 4 {font-family: 'Arial' , 'arial' ;font-size:19px;color:# 333333 ;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;background-color:transparent;background-image:none;border-width:1px;border-style:none;border-color:transparent;border-radius:0px;box-shadow:none;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;}.lepopup-form- 60 .lepopup-element- 4 .lepopup-element-html-content {min-height:23px;}.lepopup-form- 60 .lepopup-element- 5 * {font-family: 'Arial' , 'arial' ;font-size:15px;color:# 333333 ;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;}.lepopup-form- 60 .lepopup-element- 5 {font-family: 'Arial' , 'arial' ;font-size:15px;color:# 333333 ;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;background-color:transparent;background-image:none;border-width:1px;border-style:none;border-color:transparent;border-radius:0px;box-shadow:none;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;}.lepopup-form- 60 .lepopup-element- 5 .lepopup-element-html-content {min-height:24px;}.lepopup-form- 60 .lepopup-element- 6 * {font-family: 'Arial' , 'arial' ;font-size:15px;color:# 333333 ;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;}.lepopup-form- 60 .lepopup-element- 6 {font-family: 'Arial' , 'arial' ;font-size:15px;color:# 333333 ;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;background-color:transparent;background-image:none;border-width:1px;border-style:none;border-color:transparent;border-radius:0px;box-shadow:none;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;}.lepopup-form- 60 .lepopup-element- 6 .lepopup-element-html-content {min-height:18px;}.lepopup-form- 60 .lepopup-element- 7 * {font-family: 'Arial' , 'arial' ;font-size:15px;color:# 333333 ;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;}.lepopup-form- 60 .lepopup-element- 7 {font-family: 'Arial' , 'arial' ;font-size:15px;color:# 333333 ;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;background-color:transparent;background-image:none;border-width:1px;border-style:none;border-color:transparent;border-radius:0px;box-shadow:none;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;}.lepopup-form- 60 .lepopup-element- 7 .lepopup-element-html-content {min-height:18px;}.lepopup-form- 60 .lepopup-element- 8 * {font-family: 'Arial' , 'arial' ;font-size:15px;color:# 333333 ;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;}.lepopup-form- 60 .lepopup-element- 8 {font-family: 'Arial' , 'arial' ;font-size:15px;color:# 333333 ;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;background-color:transparent;background-image:none;border-width:1px;border-style:none;border-color:transparent;border-radius:0px;box-shadow:none;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;}.lepopup-form- 60 .lepopup-element- 8 .lepopup-element-html-content {min-height:18px;}.lepopup-form- 60 .lepopup-element- 9 * {font-family: 'Arial' , 'arial' ;font-size:15px;color:# 333333 ;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;}.lepopup-form- 60 .lepopup-element- 9 {font-family: 'Arial' , 'arial' ;font-size:15px;color:# 333333 ;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;background-color:transparent;background-image:none;border-width:1px;border-style:none;border-color:transparent;border-radius:0px;box-shadow:none;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;}.lepopup-form- 60 .lepopup-element- 9 .lepopup-element-html-content {min-height:18px;}.lepopup-form- 60 .lepopup-element- 10 * {font-family: 'Arial' , 'arial' ;font-size:15px;color:# 333333 ;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;}.lepopup-form- 60 .lepopup-element- 10 {font-family: 'Arial' , 'arial' ;font-size:15px;color:# 333333 ;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;background-color:transparent;background-image:none;border-width:1px;border-style:none;border-color:transparent;border-radius:0px;box-shadow:none;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;}.lepopup-form- 60 .lepopup-element- 10 .lepopup-element-html-content {min-height:18px;}.lepopup-form- 60 .lepopup-element- 11 * {font-family: 'Arial' , 'arial' ;font-size:15px;color:# 333333 ;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;}.lepopup-form- 60 .lepopup-element- 11 {font-family: 'Arial' , 'arial' ;font-size:15px;color:# 333333 ;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;background-color:transparent;background-image:none;border-width:1px;border-style:none;border-color:transparent;border-radius:0px;box-shadow:none;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;}.lepopup-form- 60 .lepopup-element- 11 .lepopup-element-html-content {min-height:18px;}.lepopup-form- 60 .lepopup-element- 12 * {font-family: 'Arial' , 'arial' ;font-size:15px;color:# 333333 ;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;}.lepopup-form- 60 .lepopup-element- 12 {font-family: 'Arial' , 'arial' ;font-size:15px;color:# 333333 ;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;background-color:transparent;background-image:none;border-width:1px;border-style:none;border-color:transparent;border-radius:0px;box-shadow:none;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;}.lepopup-form- 60 .lepopup-element- 12 .lepopup-element-html-content {min-height:18px;}.lepopup-form- 60 .lepopup-element- 13 * {font-family: 'Arial' , 'arial' ;font-size:15px;color:# 333333 ;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;}.lepopup-form- 60 .lepopup-element- 13 {font-family: 'Arial' , 'arial' ;font-size:15px;color:# 333333 ;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;background-color:transparent;background-image:none;border-width:1px;border-style:none;border-color:transparent;border-radius:0px;box-shadow:none;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;}.lepopup-form- 60 .lepopup-element- 13 .lepopup-element-html-content {min-height:18px;}.lepopup-form- 60 .lepopup-element- 14 div.lepopup-input .lepopup-icon-left, .lepopup-form- 60 .lepopup-element- 14 div.lepopup-input .lepopup-icon-right {line-height:36px;}.lepopup-form- 60 .lepopup-element- 15 div.lepopup-input{height:auto;line-height: 1 ;}.lepopup-form- 60 .lepopup-element- 16 * {font-family: 'Arial' , 'arial' ;font-size:14px;color:# 333333 ;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;}.lepopup-form- 60 .lepopup-element- 16 {font-family: 'Arial' , 'arial' ;font-size:14px;color:# 333333 ;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;background-color:transparent;background-image:none;border-width:1px;border-style:none;border-color:transparent;border-radius:0px;box-shadow:none;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;}.lepopup-form- 60 .lepopup-element- 16 .lepopup-element-html-content {min-height:5px;}.lepopup-form- 60 .lepopup-element- 19 * {font-family: 'Arial' , 'arial' ;font-size:13px;color:# 333333 ;font-style:normal;text-decoration:none;text-align:left;}.lepopup-form- 60 .lepopup-element- 19 {font-family: 'Arial' , 'arial' ;font-size:13px;color:# 333333 ;font-style:normal;text-decoration:none;text-align:left;background-color:transparent;background-image:none;border-width:1px;border-style:none;border-color:transparent;border-radius:0px;box-shadow:none;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;}.lepopup-form- 60 .lepopup-element- 19 .lepopup-element-html-content {min-height:363px;}.lepopup-form- 60 .lepopup-element- 0 * {font-size:15px;color:#ffffff;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;}.lepopup-form- 60 .lepopup-element- 0 {font-size:15px;color:#ffffff;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;background-color:#5cb85c;background-image:none;border-width:0px;border-style:solid;border-color:#ccc;border-radius:5px;box-shadow: 1px 1px 15px -6px # 000000 ;padding-top:40px;padding-right:40px;padding-bottom:40px;padding-left:40px;}.lepopup-form- 60 .lepopup-element- 0 .lepopup-element-html-content {min-height:160px;}</style><div class = "lepopup-inline" style= "margin: 0 auto;" ><div class = "lepopup-form lepopup-form-60 lepopup-form-wc8lPeNpZRZ6774S lepopup-form-icon-inside lepopup-form-position-middle-right" data-session= "0" data-id= "wc8lPeNpZRZ6774S" data-form-id= "60" data-slug= "7lQM6oyWL5bTm5lw" data-title= "Under the Post Inline" data-page= "1" data-xd= "off" data-width= "820" data-height= "430" data-position= "middle-right" data-esc= "off" data-enter= "on" data-disable-scrollbar= "off" style= "display:none;width:820px;height:430px;" onclick= "event.stopPropagation();" ><div class = "lepopup-form-inner" style= "width:820px;height:430px;" ><div class = "lepopup-element lepopup-element-2 lepopup-element-rectangle" data-type= "rectangle" data-top= "0" data-left= "0" data-animation-in= "fadeIn" data-animation-out= "fadeOut" style= "animation-duration:0ms;animation-delay:0ms;z-index:501;top:0px;left:0px;width:820px;height:430px;" ></div><div class = "lepopup-element lepopup-element-3 lepopup-element-html" data-type= "html" data-top= "7" data-left= "10" data-animation-in= "bounceInDown" data-animation-out= "fadeOut" style= "animation-duration:0ms;animation-delay:0ms;z-index:502;top:7px;left:10px;width:797px;height:73px;" ><div class = "lepopup-element-html-content" >Do you want to know how to develop your skillset to become a <span style= "color: #CAB43D; text-shadow: 1px 1px #835D5D;" >Java Rockstar?</span></div></div><div class = "lepopup-element lepopup-element-4 lepopup-element-html" data-type= "html" data-top= "83" data-left= "308" data-animation-in= "bounceInDown" data-animation-out= "fadeOut" style= "animation-duration:0ms;animation-delay:0ms;z-index:503;top:83px;left:308px;width:473px;height:23px;" ><div class = "lepopup-element-html-content" >Subscribe to our newsletter to start Rocking <span style= "text-decoration: underline;" >right now!</span></div></div><div class = "lepopup-element lepopup-element-5 lepopup-element-html" data-type= "html" data-top= "107" data-left= "308" data-animation-in= "bounceInDown" data-animation-out= "fadeOut" style= "animation-duration:0ms;animation-delay:0ms;z-index:504;top:107px;left:308px;width:473px;height:24px;" ><div class = "lepopup-element-html-content" >To get you started we give you our best selling eBooks for <span style= "color:#e01404; text-shadow: 1px 1px #C99924; font-size: 15px;" >FREE!</span></div></div><div class = "lepopup-element lepopup-element-6 lepopup-element-html" data-type= "html" data-top= "136" data-left= "308" data-animation-in= "bounceInDown" data-animation-out= "fadeOut" style= "animation-duration:0ms;animation-delay:0ms;z-index:505;top:136px;left:308px;width:473px;height:18px;" ><div class = "lepopup-element-html-content" ><span style= "font-weight: bold;" > 1 .</span> JPA Mini Book</div></div><div class = "lepopup-element lepopup-element-7 lepopup-element-html" data-type= "html" data-top= "156" data-left= "308" data-animation-in= "bounceInDown" data-animation-out= "fadeOut" style= "animation-duration:0ms;animation-delay:0ms;z-index:506;top:156px;left:308px;width:473px;height:18px;" ><div class = "lepopup-element-html-content" ><span style= "font-weight: bold;" > 2 .</span> JVM Troubleshooting Guide</div></div><div class = "lepopup-element lepopup-element-8 lepopup-element-html" data-type= "html" data-top= "176" data-left= "308" data-animation-in= "bounceInDown" data-animation-out= "fadeOut" style= "animation-duration:0ms;animation-delay:0ms;z-index:507;top:176px;left:308px;width:473px;height:18px;" ><div class = "lepopup-element-html-content" ><span style= "font-weight: bold;" > 3 .</span> JUnit Tutorial for Unit Testing</div></div><div class = "lepopup-element lepopup-element-9 lepopup-element-html" data-type= "html" data-top= "196" data-left= "308" data-animation-in= "bounceInDown" data-animation-out= "fadeOut" style= "animation-duration:0ms;animation-delay:0ms;z-index:508;top:196px;left:308px;width:473px;height:18px;" ><div class = "lepopup-element-html-content" ><span style= "font-weight: bold;" > 4 .</span> Java Annotations Tutorial</div></div><div class = "lepopup-element lepopup-element-10 lepopup-element-html" data-type= "html" data-top= "216" data-left= "308" data-animation-in= "bounceInDown" data-animation-out= "fadeOut" style= "animation-duration:0ms;animation-delay:0ms;z-index:509;top:216px;left:308px;width:473px;height:18px;" ><div class = "lepopup-element-html-content" ><span style= "font-weight: bold;" > 5 .</span> Java Interview Questions</div></div><div class = "lepopup-element lepopup-element-11 lepopup-element-html" data-type= "html" data-top= "236" data-left= "308" data-animation-in= "bounceInDown" data-animation-out= "fadeOut" style= "animation-duration:0ms;animation-delay:0ms;z-index:510;top:236px;left:308px;width:473px;height:18px;" ><div class = "lepopup-element-html-content" ><span style= "font-weight: bold;" > 6 .</span> Spring Interview Questions</div></div><div class = "lepopup-element lepopup-element-12 lepopup-element-html" data-type= "html" data-top= "256" data-left= "308" data-animation-in= "bounceInDown" data-animation-out= "fadeOut" style= "animation-duration:0ms;animation-delay:0ms;z-index:511;top:256px;left:308px;width:473px;height:18px;" ><div class = "lepopup-element-html-content" ><span style= "font-weight: bold;" > 7 .</span> Android UI Design</div></div><div class = "lepopup-element lepopup-element-13 lepopup-element-html" data-type= "html" data-top= "282" data-left= "308" data-animation-in= "bounceInDown" data-animation-out= "fadeOut" style= "animation-duration:0ms;animation-delay:0ms;z-index:512;top:282px;left:308px;width:473px;height:18px;" ><div class = "lepopup-element-html-content" >and many more ....</div></div><div class = "lepopup-element lepopup-element-14" data-type= "email" data-deps= "" data-id= "14" data-top= "305" data-left= "308" data-animation-in= "fadeIn" data-animation-out= "fadeOut" style= "animation-duration:0ms;animation-delay:0ms;z-index:513;top:305px;left:308px;width:473px;height:36px;" ><div class = "lepopup-input" ><input type= "email" name= "lepopup-14" class = "lepopup-ta-left " placeholder= "Enter your e-mail..." autocomplete= "email" data- default = "" value= "" aria-label= "Email Field" oninput= "lepopup_input_changed(this);" onfocus= "lepopup_input_error_hide(this);" ></div></div><div class = "lepopup-element lepopup-element-15" data-type= "checkbox" data-deps= "" data-id= "15" data-top= "344" data-left= "308" data-animation-in= "fadeIn" data-animation-out= "fadeOut" style= "animation-duration:0ms;animation-delay:0ms;z-index:514;top:344px;left:308px;width:160px;" ><div class = "lepopup-input lepopup-cr-layout-1 lepopup-cr-layout-left" ><div class = "lepopup-cr-container lepopup-cr-container-medium lepopup-cr-container-left" ><div class = "lepopup-cr-box" ><input class = "lepopup-checkbox lepopup-checkbox-classic lepopup-checkbox-medium" type= "checkbox" name= "lepopup-15[]" id= "lepopup-checkbox-DTZQwA36TcA7zdjs-14-0" value= "on" data- default = "off" onchange= "lepopup_input_changed(this);" ><label for = "lepopup-checkbox-DTZQwA36TcA7zdjs-14-0" onclick= "lepopup_input_error_hide(this);" ></label></div><div class = "lepopup-cr-label lepopup-ta-left" ><label for = "lepopup-checkbox-DTZQwA36TcA7zdjs-14-0" onclick= "lepopup_input_error_hide(this);" ></label></div></div></div></div><div class = "lepopup-element lepopup-element-16 lepopup-element-html" data-type= "html" data-top= "344" data-left= "338" data-animation-in= "fadeIn" data-animation-out= "fadeOut" style= "animation-duration:0ms;animation-delay:0ms;z-index:515;top:344px;left:338px;width:350px;height:5px;" ><div class = "lepopup-element-html-content" >I agree to the <a href= "https://www.javacodegeeks.com/about/terms-of-use" target= "_blank" >Terms </a> and <a href= "https://www.javacodegeeks.com/about/privacy-policy" target= "_blank" >Privacy Policy</a></div></div><div class = "lepopup-element lepopup-element-17" data-type= "button" data-top= "372" data-left= "308" data-animation-in= "bounceIn" data-animation-out= "fadeOut" style= "animation-duration:0ms;animation-delay:0ms;z-index:516;top:372px;left:308px;width:85px;height:37px;" ><a class = "lepopup-button lepopup-button-zoom-out " href= "#" onclick= "return lepopup_submit(this);" data-label= "Sign up" data-loading= "Loading..." ><span>Sign up</span></a></div><div class = "lepopup-element lepopup-element-19 lepopup-element-html" data-type= "html" data-top= "67" data-left= "-15" data-animation-in= "fadeIn" data-animation-out= "fadeOut" style= "animation-duration:0ms;animation-delay:0ms;z-index:518;top:67px;left:-15px;width:320px;height:363px;" ><div class = "lepopup-element-html-content" ><img data-lazyloaded= "1" src= "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIzMjAiIGhlaWdodD0iMzYzIiB2aWV3Qm94PSIwIDAgMzIwIDM2MyI+PHJlY3Qgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgZmlsbD0idHJhbnNwYXJlbnQiLz48L3N2Zz4=" decoding= "async" data-src= "https://www.javacodegeeks.com/wp-content/uploads/2015/01/books_promo.png.webp" alt= "" width= "320" height= "363" ><noscript><img decoding= "async" src= "https://www.javacodegeeks.com/wp-content/uploads/2015/01/books_promo.png.webp" alt= "" width= "320" height= "363" /></noscript></div></div></div></div><div class = "lepopup-form lepopup-form-60 lepopup-form-wc8lPeNpZRZ6774S lepopup-form-icon-inside lepopup-form-position-middle-right" data-session= "0" data-id= "wc8lPeNpZRZ6774S" data-form-id= "60" data-slug= "7lQM6oyWL5bTm5lw" data-title= "Under the Post Inline" data-page= "confirmation" data-xd= "off" data-width= "420" data-height= "320" data-position= "middle-right" data-esc= "off" data-enter= "on" data-disable-scrollbar= "off" style= "display:none;width:420px;height:320px;" onclick= "event.stopPropagation();" ><div class = "lepopup-form-inner" style= "width:420px;height:320px;" ><div class = "lepopup-element lepopup-element-0 lepopup-element-html" data-type= "html" data-top= "80" data-left= "70" data-animation-in= "bounceInDown" data-animation-out= "fadeOutUp" style= "animation-duration:1000ms;animation-delay:0ms;z-index:500;top:80px;left:70px;width:280px;height:160px;" ><div class = "lepopup-element-html-content" ><h4 style= "text-align: center; font-size: 18px; font-weight: bold;" >Thank you!</h4><p style= "text-align: center;" >We will contact you soon.</p></div></div></div></div><input type= "hidden" id= "lepopup-logic-wc8lPeNpZRZ6774S" value= "[]" ></div></person>> |
Hi…
I’m Elena gillbert.An optional value either contains a value or contains nil to indicate that the value is missing. Write a question mark ( ? ) after the type of a value to mark the value as optional. Why would you want to use an optional value?
This is good question. I wanted to write about this in blog but missed it. ‘?’ operator is available in some language like groovy and it is used for safe navigation of null value. This option was considered by java but they dropped it and took some inspiration from Scala or haskell. Null safe operator has few thing that does not make it that good selection. 1 – It Hides the real question like why object is null or does it has any business meaning 2 – Does not have option to handle the else part like supply some default… Read more »