Null-safe Method invocation Java7

java, java-7

Solution

Null-safe method invocation was proposed for Java 7 as a part of Project Coin, but it didn't make it to final release.

See all the proposed features, and what all finally got selected here - https://wikis.oracle.com/display/ProjectCoin/2009ProposalsTOC

As far as simplifying that method is concerned, you can do a little bit change:

public String getPostcode(Person person) {

    if (person == null) return null;
    Address address = person.getAddress();
    return address != null ? address.getPostcode() : null;
}

I don't think you can get any concise and clearer than this. IMHO, trying to merge that code into a single line, will only make the code less clear and less readable.

Problem

I want to get details about this feature of Java7 like this code ``` public String getPostcode(Person person) { if (person != null) { Address address = person.getAddress(); if (address != null) { return address.getPostcode(); } } return null; } ``` Can be do something like this ``` public String getPostcode(Person person) { return person?.getAddress()?.getPostcode(); } ``` But frankly its not much clear to me.Please explain?

Original source

Related problems