Be careful Of This Java Optional Method

Let’s Remember Java Optional 🤓

According to Oracle it’s “A container object which may or may not contain a non-null value.”
Optional was introduced in Java 8 and has been used by the SpringBoot team in many projects.

The most common usage…



Let’s Remember Java Optional 🤓

According to Oracle it’s “A container object which may or may not contain a non-null value.”
Optional was introduced in Java 8 and has been used by the SpringBoot team in many projects.

The most common usage of Optionals is in the Spring Data project. Let’s look at the JpaRepository interface and an example method.
Say we have a User entity with an Id type of integer and that we have a JpaRepository for it

@Repository  
public interface IUserRepo extends JpaRepository<User, Integer>  
{  
    Optional<User> findByUserName(String userName);  
}

We defined a method that searches for a user via their user name and returns an Optional of a User.



Optional’s Convenience Methods 🙌

Optional comes in with many method meant to enable us to write clean and readable code.

  • map(..).or(…)
  • map(…).orElse(…)
  • check out Oracle’s docs for the full list.

However, there is one method with a dangerously unexpected behavior



Meet The orElse Method 👀

According to Oracle’s doc:

public T orElse(T other) 

Return the value if present, otherwise return other.

Now, we can add a method call as the parameter of the orElse, which will be run if the Optional is empty, right?

Yes, that’s correct, BUT, what if I tell you that it will run anyways regardless of the presence of the value in Optional or not.

Let’s test it ✍️

@Test  
public void orElseTest()  
{  
    String result = Optional.of("hello").orElse(someMethod());  
    assertThat(result).isEqualTo("hello");  
}  
private String someMethod()  
{  
    System.out.println("I am running !!");  
    return "hola";  
}

The test does pass, but we notice that on the console we have the string “I am running” printed out.



Why is that? 🤨

  • Java runs the methods and caches its result in case it’s to be returned for performance reasons.



So Be Careful ⛔️

We want to be careful if the method inside the orElse might have a side effect, because it will be run anyways.



👉🏾 Also check out How to escape NullPointerExceptions in Java using Optional


Print Share Comment Cite Upload Translate
APA
Abdulcelil Cercenazi | Sciencx (2024-03-28T14:55:48+00:00) » Be careful Of This Java Optional Method. Retrieved from https://www.scien.cx/2021/10/13/be-careful-of-this-java-optional-method/.
MLA
" » Be careful Of This Java Optional Method." Abdulcelil Cercenazi | Sciencx - Wednesday October 13, 2021, https://www.scien.cx/2021/10/13/be-careful-of-this-java-optional-method/
HARVARD
Abdulcelil Cercenazi | Sciencx Wednesday October 13, 2021 » Be careful Of This Java Optional Method., viewed 2024-03-28T14:55:48+00:00,<https://www.scien.cx/2021/10/13/be-careful-of-this-java-optional-method/>
VANCOUVER
Abdulcelil Cercenazi | Sciencx - » Be careful Of This Java Optional Method. [Internet]. [Accessed 2024-03-28T14:55:48+00:00]. Available from: https://www.scien.cx/2021/10/13/be-careful-of-this-java-optional-method/
CHICAGO
" » Be careful Of This Java Optional Method." Abdulcelil Cercenazi | Sciencx - Accessed 2024-03-28T14:55:48+00:00. https://www.scien.cx/2021/10/13/be-careful-of-this-java-optional-method/
IEEE
" » Be careful Of This Java Optional Method." Abdulcelil Cercenazi | Sciencx [Online]. Available: https://www.scien.cx/2021/10/13/be-careful-of-this-java-optional-method/. [Accessed: 2024-03-28T14:55:48+00:00]
rf:citation
» Be careful Of This Java Optional Method | Abdulcelil Cercenazi | Sciencx | https://www.scien.cx/2021/10/13/be-careful-of-this-java-optional-method/ | 2024-03-28T14:55:48+00:00
https://github.com/addpipe/simple-recorderjs-demo