[NEW BUG]: Avoid allocations in String.replace(CharSequence, CharSequence) in case no replacement happened

Vitaly Davidovich vitalyd at gmail.com
Thu Mar 2 11:45:03 UTC 2017

On Thu, Mar 2, 2017 at 6:38 AM Ulf Zibis <Ulf.Zibis at cosoco.de> wrote:

> Hi Vitaly,
> I don't see any contract to throw an NPE:
> https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#replace-java.lang.CharSequence-java.lang.CharSequence-

Hmm, I must've looked at java 7 docs, which do call out the NPE:

Was this changed intentionally in 8?

> <https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#replace-java.lang.CharSequence-java.lang.CharSequence->
> In any case, what's the reasonable of checking an argument, which is not
> used in that case?

My understanding is contracts like the above (let's assume it still called
out the NPE in 8) are checked even if the argument isn't used in some
cases.  The other case where I believe this is done is when passing a
Supplier to some method that uses it to obtain a default value - even if
it's not needed, it's checked for null because most (all?) such methods
stipulate that it cannot be null.

> Am 02.03.2017 um 00:18 schrieb Vitaly Davidovich:
> > Seems like a good idea.  You probably want to null check 'replacement'
> > before the bail out as the method is specified as throwing NPE in that
> case.
> --
Sent from my phone

More information about the core-libs-dev mailing list