🌐
Spring
docs.spring.io › spring-framework › reference › core › expressions › language-ref › operator-safe-navigation.html
Safe Navigation Operator :: Spring Framework
Given the expression #person?.address.city, if #person is null the safe navigation operator (?.) ensures that no exception will be thrown when attempting to access the address property of #person. However, since #person?.address evaluates to null, a NullPointerException will be thrown when attempting to access the city property of null.
boolean operator for safely navigating nested data structures where elements may or may not be null/not present
In object-oriented programming, the safe navigation operator (also known as optional chaining operator, safe call operator, null-conditional operator, null-propagation operator) is a binary operator that returns null if its first argument is … Wikipedia
🌐
Wikipedia
en.wikipedia.org › wiki › Safe_navigation_operator
Safe navigation operator - Wikipedia
1 month ago - Clojure doesn't have true operators in the sense other languages uses it, but as it interoperable with Java, and has to perform object navigation when it does, the some-> macro can be used to perform safe navigation.
🌐
Oracle
oracle.com › java › technical details
Tired of Null Pointer Exceptions? Consider Using Java SE 8's Optional!
Languages such as Groovy have a safe navigation operator represented by "?." to safely navigate through potential null references. (Note that it is soon to be included in C#, too, and it was proposed for Java SE 7 but didn't make it into that release.) It works as follows:
🌐
GitHub
github.com › Catalysts › SafeNavigationOperator
GitHub - Catalysts/SafeNavigationOperator: A Safe Navigation Operator for Java. Makes it possible to traverse through object calls without having to check for null values at each stage.
A Safe Navigation Operator for Java. Makes it possible to traverse through object calls without having to check for null values at each stage. - Catalysts/SafeNavigationOperator
Author   Catalysts
🌐
TutorialsPoint
tutorialspoint.com › spring_expression_language › spring_expression_language_safe_navigation_operator.htm
Spring SpEL - Safe Navigation Operator
SpEL expression supports Safe Navigation operator which is used to avoid NullPointerException. int length = parser.parseExpression("name?.length").getValue(context, Integer.class); Here if name is null, then expression will not throw null pointer exception. Following example shows the various ...
🌐
Reddit
reddit.com › r/java › how hard would it be to add a safe call operator to the java language?
r/java on Reddit: How hard would it be to add a safe call operator to the Java language?
September 4, 2021 -

Checking for null is quite verbose in Java. The current best practice is to either use Nullability annotations or Optional<T>. Personally, I'm in the annotations camp. Yes, optional is very useful in streams but I don't like the cognitive and runtime overhead it implies anywhere else.

My dream would be if one day nullability would be added to the type system. Obviously, that's a lot of work. But wouldn't be a pragmatic first step, to reduce just the boilerplate of null checks with a safe call operator? The compiler could surely rewrite this code:

if (getLoggedInUser()?.isPowerUser()) {
}

To this:

if (getLoggedInUser() != null && getLoggedInUser().isPowerUser()) {
}

To avoid an unexpected double method invocation, it could assign to ad hoc created temp variables:

User generatedSafeCall1;
if ((generatedSafeCall1 = getLoggedInUser()) != null && generatedSafeCall1.isPowerUser()) {
}

I believe this would end a lot of complaints about null handling in Java. Since only new code would use the ?. operator, backwards compatibility shouldn't be an issue either. Are there any downsides that I'm missing?

🌐
Spring
docs.enterprise.spring.io › spring-framework › reference › 6.0 › core › expressions › language-ref › operator-safe-navigation.html
Safe Navigation Operator :: Spring Framework
The safe navigation operator is used to avoid a NullPointerException and comes from the Groovy language. Typically, when you have a reference to an object, you might need to verify that it is not null before accessing methods or properties of the object. To avoid this, the safe navigation operator ...
🌐
Opensource
opensource.net › home › blog › what you need to know about the safe navigation operator in apache groovy
What you need to know about the Safe Navigation operator in Apache Groovy - OpenSource.net
December 12, 2023 - Groovy truth, the ternary, Elvis operators, Safe Navigation and index operators, when taken together, move Groovy syntax and semantics to a whole new level. Unwieldy Java expressions shorten up by eliminating repeated use of the same variable names and become completely practical as a replacement for if – then – else.
Find elsewhere
🌐
Blogger
self-learning-java-tutorial.blogspot.com › 2019 › 01 › groovy-safe-navigation-operator.html
Programming for beginners: Groovy: Safe Navigation Operator
Safe Navigation Operator (?.) is used to get rid off NullPointerException. For example, below script defines an Employee ...
Top answer
1 of 1
23

Naturally, the best person to ask this question is someone on the JCP Executive Committee, not us. However, that won't prevent me from engaging in some idle speculation.

The answer to every "why wasn't this feature implemented" question is always because the benefits did not exceed the costs.

Eric Lippert (former member of the C# team) says that, in order for a product to have a feature, that feature must be:

  • thought of in the first place
  • desired
  • designed
  • specified
  • implemented
  • tested
  • documented
  • shipped to customers

In other words, there must be many important things that must happen before any new programming language feature can be realized. The costs are larger than you think they are.

On the C# team, every new feature request starts out with a score of minus 100. Then the team evaluates the benefits and the costs, adding points for the benefits, and subtracting points for the costs. If the score doesn't go above zero, the proposed feature is summarily discarded. In other words, the new feature must provide a compelling benefit.

But the Elvis Operator made it into C#. So why didn't it make it into Java?

Despite their apparent similarities, Java and C# have significantly different language philosophies. This is evidenced by the fact that Java enterprise programs tend to be large, structural collections of architecture. Brevity and language expressiveness are sacrificed on the altar of ceremony and ease of coding. Well-known software architectural patterns that everyone on the development team can recognize are preferred over language conveniences.

Consider this Reddit exchange:

The Elvis operator has been proposed for every version of Java since 7, and has been rejected every time. Different languages fall on different points along the spectrum from "pure" to "pragmatic", and languages implementing the Elvis operator do tend to be further toward the pragmatic end of the spectrum than Java.

If you have a team of 15+ year Java pros writing a highly-distributed, highly-concurrent backend processing system of some sort, then you probably want a great degree of architectural rigor.

However, if you have a junior to mid-level team, half of whom migrated from Visual Basic, and you have them writing an ASP.NET web app that mostly just does CRUD operations... then it might be overkill to design a bunch of AbstractFactoryFactory classes to abstract away the fact that you have no control over which columns are nullable in the shitty legacy database that you must use.

These profound differences in language philosophy extend not only to the way the languages are used, but to the way the language design process itself is undertaken. C# is a benevolent dictator language. To get a new feature into C#, you only really have to convince one person: Anders Hejlsberg.

Java takes a more conservative approach. To get a new feature into Java, it must get consensus from a consortium of large vendors such as Oracle, IBM, HP, Fujitsu & Red Hat. Obviously, that process is going to be slower and present a higher bar for new language features.

The question "why wasn't x feature implemented..." always implicitly includes the words, "...if it's obviously such a good idea?" As I have adequately demonstrated here, the choice is never that simple.

🌐
Blogger
giri-tech.blogspot.com › 2018 › 10 › navigate-safely-with-null-safe-operator.html
My Te(a)ch Notes: Navigate safely with null-safe operator for Maps in Groovy . . .
Groovy offered null-safe operator ( ?. ) right from the beginning. Recent JVM languages started offering ways to deal with it safely with which Java developers are falling in love. But, what Groovy offers with null-safe operator ( ?. ), is the most elegant syntax for safely navigating an object ...
🌐
TutorialsPoint
tutorialspoint.com › groovy › groovy_safe_navigation_operator.htm
Groovy - Safe Navigation Operator
safe navigation operator ?. is a powerful tool in Groovy to avoid null pointer exceptions while accessing properties or methods of potential null object. It provides an elegant and concise way to navigate through object's nested properties or methods.
🌐
DZone
dzone.com › coding › java › java 8 elvis operator
Java 8 Elvis Operator
August 28, 2018 - Indeed, the documentation even cites some of the examples. What Alternatives to Null Are There? Languages such as Groovy have a safe navigation operator represented by “?.” to safely navigate through potential null references.
🌐
Salesforce Developers
developer.salesforce.com › docs › atlas.en-us.apexcode.meta › apexcode › langCon_apex_SafeNavigationOperator.htm
Safe Navigation Operator | Apex Developer Guide | Salesforce Developers
Use the safe navigation operator (?.) to replace explicit, sequential checks for null references. This operator short-circuits expressions that attempt to operate on a null value and returns null instead of throwing a NullPointerException.
🌐
Reddit
reddit.com › r/java › elvis and other null-safe operators in java
Elvis and other null-safe operators in Java : r/java
December 14, 2017 - He distinguishes the "pure" Elvis operator ?:, which is a null-safe ternary, from ?. which is a safe navigation operator. Amusingly, he says that the latter is not Elvis, unless one assumes a one-eyed Elvis! http://mail.openjdk.java.net/pipermail/coin-dev/2009-July/002089.html
🌐
GitHub
github.com › projectlombok › lombok › issues › 1070
Feature Request: Groovy inspired "Elvis operator" and "Safe navigation operator" · Issue #1070 · projectlombok/lombok
January 17, 2016 - Groovy offers two very useful operators, the "Elvis operator" (?:) and the "Safe navigation operator" (?.). From http://www.oracle.com/technetwork/articles/java/java8-optional-2175753.html: "Languages such as Groovy have a safe navigation operator represented by '?.' to safely navigate through potential null references.
Published   Apr 02, 2016