A static null-coalescing method

Michael Nascimento misterm at gmail.com
Tue Oct 29 12:51:14 UTC 2024


Hi Emre,

Why wouldn't requireNonNullElse(o1, requireNonNull(o2)) or
requireNonNull(requireNonNullElse(o1, o2)) suffice?

Regards,
Michael

On Mon, 28 Oct 2024 at 16:41, Emre Kaplan <emr3kp at gmail.com> wrote:

> Hello,
>
> I think java.util.Optional class should have a static method named like
> getOrDefault(Object o, Object default)
>
> In short, if an object is null, it should return the default object,
> otherwise the same object. And inside, default null check should be done.
> Same result can be achieved by method chaining of ofNullable and orElse,
> but then an object is created. I think it will be memory efficient because
> only one static method needs to be called. I don't know if it has been
> discussed before.
>
> Also there is Objects.requireNonNullElse(Object o1, Object o2)
> but the second parameter should be non-null.
>
> Thanks in advance..
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/jdk-dev/attachments/20241029/32fdb434/attachment.htm>


More information about the jdk-dev mailing list