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.
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.
This syntax does not exist in Java, nor is it slated to be included in any of the upcoming versions that I know of.
Videos
With the ternary operator :
String string = bs != null ? f.format(bs) : "";
I'm late to the game but I wanted something similar and found use of Java 8 Optional class. Solution for problem statement in question:
String string = Optional.ofNullable(bs).map(f::format).orElse("");
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.
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?
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.