Melad Basilius
Melad Basilius

Reputation: 4306

Why this method returns "null" but not null

I have this method which extracts some info from remittanceInformation object

private static String combineStrings(RemittanceInformation remittanceInformation) {
        return Optional.ofNullable(remittanceInformation)
                .map(RemittanceInformation::getUstrds)
                .map(l -> l.stream().collect(Collectors.joining(/* CRLF? */)))
                .orElse(null);
    }

Now everything inside remittanceInformation is null and the method in this case should return null but it returns "null" with double quotes, Why this behavior ?

Upvotes: 0

Views: 126

Answers (1)

Joop Eggen
Joop Eggen

Reputation: 109613

The joining of strings delivers a string, possible "null" (""+null) if there is one term being null. That must be the case: a non-null remittanceInformation where getUstrds() gives a collection with a single null.

Upvotes: 2

Related Questions