Hello Team,
Wanted to check the actual use case of Optional.Nullable in AEM section.
Why Part 1 is preferred over Part 2? Is it because of developer-friendly code?
I have observed that in Part 1, by mistake if I add jcr:content at the end, i mean:
String currentPagePath = "/content/abc/us/en/jcr:content"; then application throws error.
Solved! Go to Solution.
Views
Replies
Total Likes
The use of Optional in Java is a way to deal with null values and avoid NullPointerException. It's a more modern approach and is generally considered more safer to avoid any runtime execution - the flow will continue to next line or function.
In general, it's recommended to use Optional when dealing with null values in Java. It makes the code safer and more readable.
Regarding the error when you add jcr:content at the end of the path, this is because jcr:content is a node that contains properties of the page.
The use of Optional in Java is a way to deal with null values and avoid NullPointerException. It's a more modern approach and is generally considered more safer to avoid any runtime execution - the flow will continue to next line or function.
In general, it's recommended to use Optional when dealing with null values in Java. It makes the code safer and more readable.
Regarding the error when you add jcr:content at the end of the path, this is because jcr:content is a node that contains properties of the page.
Hi,
This goes beyond AEM and it is more like a general Java question, there is a pretty useful scenario where "Optional" should be adopted, such as returning the type of a method, etc. I would encourage you to read these articles about when it is "better" to use optional over a simple null check.
https://medium.com/javarevisited/4-reasons-why-you-should-use-java-optional-or-not-4e44d51a9645
https://www.baeldung.com/java-optional-uses
Hope this helps.
@Mahesh_Gunaje
It was introduced in Java 8 and is part of the java.util package.
In Adobe Experience Manager (AEM), Optional.Nullable is a class used for handling nullable values in Java. It allows developers to express the absence of a value explicitly, reducing null pointer exceptions. By leveraging Optional.Nullable, AEM developers can write cleaner and more robust code, handling null values more effectively.
Part1 -> is safe as handling exception allow us to execute subsequent line. it also allows us to handle null pointer exceptions, thereby preventing unknown errors on our site and ensuring that our backend code does not break.
Part2 -> in case of exception sub-sequent code will not get run and site/functionality will get break.
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies