An Interest In:
Web News this Week
- April 23, 2024
- April 22, 2024
- April 21, 2024
- April 20, 2024
- April 19, 2024
- April 18, 2024
- April 17, 2024
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 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
Original Link: https://dev.to/jarjanazy/be-careful-of-this-java-optional-method-439
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To