The original idea comes from groovy. It was proposed for Java 7 as part of Project Coin: https://wiki.openjdk.java.net/display/Coin/2009+Proposals+TOC (Elvis and Other Null-Safe Operators), but hasn't been accepted yet.

The related Elvis operator ?: was proposed to make x ?: y shorthand for x != null ? x : y, especially useful when x is a complex expression.

Answer from ataylor on Stack Overflow
🌐
Jenkov
jenkov.com › tutorials › java › ternary-operator.html
Java Ternary Operator
You can use the Java ternary operator as a shorthand for null checks before calling a method on an object.
🌐
GitHub
github.com › redhat-developer › vscode-java › issues › 2727
Doing null check on @Nullable functions via ternary operator still gets highlighted with "Potential null pointer access" problem · Issue #2727 · redhat-developer/vscode-java
October 10, 2022 - getSomethingNullable() is annotated with @Nullable and will be highlighted despite having a prior null check. This does not occur when doing if-else null check, only when using a ternary operator.
Published   Oct 10, 2022
🌐
Mkyong
mkyong.com › home › java › java ternary operator examples
Java Ternary Operator examples - Mkyong.com
October 16, 2019 - package com.mkyong.test; public class JavaExample1_2 { public static void main(String[] args) { int age = 10; String result = (age > 18) ? "Yes, you can vote!" : "No, you can't vote!"; System.out.println(result); } } Output · No, you can't vote! In short, It improves code readability. It’s common to use the ternary operator as null check.
🌐
Qlik
customerportal.qlik.com › article › Testing-for-null-or-empty-zero-value-using-the-Java-ternary-operator-in-a-tJavaRow-5D0il
Testing for null or empty/zero value using the Java ternary ...
Skip to Main · Search · Login · Products · Data Integration and Quality · Qlik Talend · Qlik Cloud® Data Integration · Talend Data Fabric · Analytics · Analytics Overview
🌐
Reddit
reddit.com › r/javahelp › ternary vs optional
r/javahelp on Reddit: Ternary vs Optional
October 25, 2021 -

This is absolutely a minor thing. It's probably just taste, but it keeps popping up in my mind for a whole day now.

I've used something like this a lot:

var result = source == null ? null : source.toString();

I never liked the ternary, just because the source could be null.
Now I found this way to achieve the same:

var result = Optional.ofNullable(source)
    .map(SourceType::toString)
    .orElse(null);

But I still need to put so much code just to deal with a potential null value. I don't like both, but I tend to like the stream-like syntax, where you can kind of treat the value like it's there and have just the null at the end. Not for this trivial call, but once things get a bit more complicated.

Is there any real benefit from the one solution over the other one, to decide which to use when? Perhaps there's even a better solution I'm not aware of, I could of course write a method with the source as parameter and the function to call, if the parameter is not null, or that method already exists.

Find elsewhere
🌐
Talend
community.talend.com › s › article › Testing-for-null-or-empty-zero-value-using-the-Java-ternary-operator-in-a-tJavaRow-5D0il
Testing for null or empty/zero value using the Java ternary ...
Qlik Community is the global online community for Qlik employees, experts, customers, partners, developers and evangelists to collaborate.
🌐
W3Docs
w3docs.com › java
Avoiding NullPointerException in Java
Here's an example of how you can ... } else { System.out.println("str is null"); } } } Use the ternary operator: You can use the ternary operator (?...
🌐
softwarecave
softwarecave.org › 2021 › 10 › 07 › unexpected-nullpointerexception-npe-in-ternary-conditional-operator-in-java
Unexpected NullPointerException (NPE) in ternary conditional operator in Java | softwarecave
October 7, 2021 - In fact, the second argument of ternary operator is of type int and the third argument is of type Integer. The compiler than decides that the type of the whole expression will be plain int. and tries to unbox defaultValue which is actually null.
🌐
Xperti
xperti.io › home › java ternary operator with examples
Java Ternary Operator with Examples
May 2, 2022 - In the case of dissimilar data types, the code will result in a syntax error. As ternary operator takes relatively less space as compared to an if-else statement, it is feasible to be used as a shorthand for null checks ...
🌐
Eclipse
bugs.eclipse.org › bugs › show_bug.cgi
195757 – Missing redundant null check in if condition through ternary operator
Bugzilla – Bug 195757 Missing redundant null check in if condition through ternary operator Last modified: 2025-05-08 13:36:49 EDT
🌐
SitePoint
sitepoint.com › blog › java › java’s ternary operator in three minutes
Java's Ternary Operator in Three Minutes — SitePoint
November 6, 2024 - If the condition evaluates to true, the first expression is returned. If the condition evaluates to false, the second expression is returned. If either of these expressions is null, then null is returned.
Top answer
1 of 4
8

The ternary operator is well used, especially for short null-checks / defaults:

System.out.println("foo is "+(foo==null) ? "not set" : foo);

Some people consider this not as readable as an if/else, but that was not the question.

The XOR bitwise operator is only used in bit-processing. If you need a bitwise XOR, then there is no way around it.

The XOR logical operator is indeed so rare, that I did not see it in the last ten year in Java in valid cases. This is also due to the fact, that the boolean XOR "does not scale" like || or &&. What I mean:

if( a && b && c && d ) ....   // it's clear what the intention is
if( a || b || c || d ) ....   // here also
if( a ^  b ^  c ^  d ) ....   // ???

In the last case I would guess, the coder meant "only one should be true". But XOR is a beast. The coder got the beast and not what (s)he wanted.

That would be an interesting interview question: What is the result of the last if?

2 of 4
7

I used to find the ternary operator difficult to parse, but I've since found that there are some places where it is very useful. These don't tend to be application logic, but logging.

For example:

log.info( "Object foo is " + ( foo.isEnabled() ? "" : "not " ) + "enabled" );

To me, that's a lot neater than either

if ( foo.isEnabled() ) 
    log.info( "Foo is enabled" );
else
    log.info( "Foo is not enabled" );

Or

log.info( "Foo is enabled : " + foo.isEnabled() );

In summary, it's a question of where it's being used and for that.

As for bit-wise, I still struggle with those but that's because I'm used to working at a high level of abstraction, as other commenters have suggested. If I do come across somewhere in code that someone's decided to use that to be "efficient", the time wasted while I figure it out negates benefits.

🌐
Quora
quora.com › Which-is-more-efficient-for-checking-null-values-ternary-operators-or-if-statements
Which is more efficient for checking null values: ternary operators or if statements? - Quora
Answer: The two are equivalent constructs and therefore equally efficient. But there is a method that is slightly more efficient. The and and or operators generally do the job, given proper shortcut semantics, before you get to needing the ternary operator. You can almost always say [code ](this ...
🌐
Baeldung
baeldung.com › home › java › core java › ternary operator in java
Ternary Operator in Java | Baeldung
September 24, 2025 - Learn about the ternary operator ?:, which allows us to define conditional expressions in Java.
🌐
Technical Jottings
thetechcandy.wordpress.com › 2010 › 01 › 28 › avoid-null
How to avoid “!= null” statements in Java? | Technical Jottings
January 28, 2010 - Another issue concerns how to handle common if-null scenarios with a shortened form of the ternary operator specific to nulls. So rather than: String str = getStringMayBeNull(); str = (str == null ? “” : str); you might do: String str = getStringMayBeNull() ?: “”; This proposal moves checking of nulls from run-time to compile-time which will avoid many NullPointerExceptions and increase the robustness of Java programs.
🌐
Baeldung
baeldung.com › home › java › java numbers › check if an integer value is null or zero in java
Check if an Integer Value Is Null or Zero in Java | Baeldung
January 8, 2024 - As we’ve already handled the null case, we only need to check whether the result of the expression with the ternary operator is zero. Next, let’s test if this works with our three Integer instances: int n0 = 0; boolean result0 = usingTernaryOperator(n0); assertTrue(result0); boolean resultNull = usingTernaryOperator(null); assertTrue(resultNull); int n42 = 42; boolean result42 = usingTernaryOperator(n42); assertFalse(result42); The test passes if we give it a run. So, the usingTernaryOperator() method works as expected. Java 8 introduced the Optional type, so if our Java version is 8 or later, we can also implement this check using a static method from the Optional class: