A static null-coalescing method

Remi Forax forax at univ-mlv.fr
Mon Oct 28 19:59:37 UTC 2024


> From: "Emre Kaplan" <emr3kp at gmail.com>
> To: "jdk-dev" <jdk-dev at openjdk.org>
> Sent: Monday, October 28, 2024 8:40:55 PM
> Subject: A static null-coalescing method

> Hello,

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.

It has been discussed multiple times, 
see [ https://bugs.openjdk.org/browse/JDK-8138963 | https://bugs.openjdk.org/browse/JDK-8138963 ] and the linked discussions on core-libs-dev mailing list. 

> Thanks in advance..

regards, 
Rémi 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/jdk-dev/attachments/20241028/1b46758d/attachment-0001.htm>


More information about the jdk-dev mailing list