0

Possible Duplicate:
Java “?” Operator for checking null - What is it? (Not Ternary!)

I just read this article, which claims that:

Take the latest version of Java, which tries to make null-pointer checking easier by offering shorthand syntax for the endless pointer testing. Just adding a question mark to each method invocation automatically includes a test for null pointers, replacing a rat's nest of if-then statements, such as:

public String getPostcode(Person person) {
    String ans = null;
    if (person != null) {
        Name nm= person.getName();
        if (nm!= null) {
            ans= nm.getPostcode();
        }
    }
    return ans
}

With this:

public String getFirstName(Person person) {
    return person?.getName()?.getGivenName();
}

Aside from the fact that those pieces of code actually do different things1, is this the case, or it it an outright fabrication? I can't find any mention of that operator elsewhere...

1 unless the person in question frequently got lost, remedied only by making their given name their postcode

Community
  • 1
  • 1
Eric
  • 91,378
  • 50
  • 226
  • 356
  • 2
    Do I get a link to the duplicate question? – Eric Dec 09 '10 at 19:19
  • very weird that SO is no longer linked the dupe automatically anymore – matt b Dec 09 '10 at 19:21
  • It's in the source for the question... Why is it not displaying? – Eric Dec 09 '10 at 19:21
  • @Eric: hm, that's not so useful, that it is automatically appended when someone votes to close as duplicate (it was there earlier!) but disappears when the question is actually closed... Here you go: http://stackoverflow.com/questions/4390141/java-operator-for-checking-null-what-is-it-not-ternary – Michael Borgwardt Dec 09 '10 at 19:22
  • Right, I added it in myself. Lousy good-for-nothing lets-close-the-question-while-its-being-edited over-2000-rep users. – Eric Dec 09 '10 at 19:22
  • Gosh, this really is an exact duplicate – Eric Dec 09 '10 at 19:24

0 Answers0