11

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?

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Subodh Joshi
  • 12,717
  • 29
  • 108
  • 202

4 Answers4

14

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.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
1

If I understand your question correctly and you want to make the code shorter, you could take advantage of short-circuit operators by writing:

if (person != null && person.getAddress() != null)
    return person.getAddress().getPostCode();

The second condition won't be checked if the first is false because the && operator short-circuits the logic when it encounters the first false.

Kon
  • 10,702
  • 6
  • 41
  • 58
  • 4
    Except you evaluate `person.getAddress()` twice :( – arshajii Aug 09 '13 at 18:00
  • 3
    @Gamb No, it's not a question of optimization (although that could also come into play if `getAddress()` took a long time, but persumably it doesn't). This code is not the same as the OP's code. – arshajii Aug 09 '13 at 18:02
  • @Gamb If `getAddress()`'s body involves substantial amounts of processing, a second call would be undesirable. Without knowing the method's implementation, we can't make assumptions about the optimization quality of one call versus two calls. – FThompson Aug 09 '13 at 18:04
  • For the record, I would never write code like my answer here. I would prefer the code the OP wrote originally. But if his question is regarding a strictly shorter way of writing something, then my reply should be helpful in some way. HOPEFULLY. – Kon Aug 09 '13 at 18:05
  • @Vulcan I asked that before realizing the slight difference in the code. Still, if we can't assume in one way, we shouldn't be so hasty as to consider the other one more valid. – Fritz Aug 09 '13 at 18:07
  • You're missing a last line: `return null;` (or `else return null;`) – bacar Oct 03 '14 at 17:49
0

It should work for you:

public String getPostcode(Person person) {
    return person != null && person.getAddress() != null ? person.getAddress().getPostcode() : null;
}

Also check this thread: Avoiding != null statements

Community
  • 1
  • 1
Eldar Agalarov
  • 4,849
  • 4
  • 30
  • 38
0

If you want to avoid calling getAddress twice, you can do this:

Address address;
if (person != null && (address = person.getAddress()) != null){
      return address.getPostCode();
}
radiantRazor
  • 477
  • 3
  • 13