If without else ternary operator

JavaIf StatementTernary Operator

Java Problem Overview


So far from I have been searching through the net, the statement always have if and else condition such as a ? b : c. I would like to know whether the if ternary statement can be used without else. Assuming i have the following code, i wish to close the PreparedStatement if it is not null

(I am using Java programming language.)

PreparedStatement pstmt;

//.... 

(pstmt!=null) ? pstmt.close : <do nothing>;

Java Solutions


Solution 1 - Java

No, you cannot do that. Instead try this:

if(bool1 && bool2) voidFunc1();

Solution 2 - Java

Why using ternary operator when you have only one choice?

if (pstmt != null) pstmt.close(); 

is enough!

Solution 3 - Java

As mentioned in the other answers, you can't use a ternary operator to do this.

However, if the need strikes you, you can use Java 8 Optional and lambdas to put this kind of logic into a single statement:

Optional.of(pstmt).ifPresent((p) -> p.close())

Solution 4 - Java

Ternary if operator is the particular ternary operator. One of a kind.

From Wiki: > In mathematics, a ternary operation is an n-ary operation with n = 3.

It means all 3 operands are required for you.

Solution 5 - Java

Just write it out?

if(pstmt != null) pstmt.close();

It's the exact same length.

Solution 6 - Java

A ternary operation is called ternary beacause it takes 3 arguments, if it takes 2 it is a binary operation.

And as noted above, it is an expression returning a value.

If you omit the else you would have an undefined situation where the expression would not return a value.

So as also noted in other answer, you should use an if statement.

Solution 7 - Java

You cannot use ternary without else, but to do a "if-without-else" in one line, you can use Java 8 Optional class.

PreparedStatement pstmt;

//.... 

Optional.ofNullable(pstmt).ifPresent(pstmt::close); // <- but IOException will still happen here. Handle it.

Solution 8 - Java

pstmt != null && pstmt.close;

The line of code above translates to When the left side of the expression "translates" to true -> execute the right side.

Solution 9 - Java

use:

<logic Expression> ? <method> : null;

Example:

(pstmt!=null) ? pstmt.close : null;

is dirty solution but works...

Solution 10 - Java

Well in JavaScript you can simply do:

expression ? doAction() : undefined

since that's what's literally actually happening in a real if statement, the else clause is simply undefined. I image you can do pretty much the same thing in (almost?) any programming language, for the else clause just put a null-type variable that doesn't return a value, it shouldn't cause any compile errors.

or just make a function to return if all else fails

function oy(x1,x2){if(x1) return x2();}

oy(etzem==6, ()=>yichoyliss=8);

Solution 11 - Java

Yes you can do that actually (in JavaScript at least):

condition && x = true;

or (in JavaScript, and there might be a similar way to do this in Java):

void(condition && x = true)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionZ.VView Question on Stackoverflow
Solution 1 - Javafrankie liuzziView Answer on Stackoverflow
Solution 2 - Javacigno5.5View Answer on Stackoverflow
Solution 3 - JavaKreaseView Answer on Stackoverflow
Solution 4 - JavaYegoshin MaximView Answer on Stackoverflow
Solution 5 - JavaJeroen VannevelView Answer on Stackoverflow
Solution 6 - JavaDavid MårtenssonView Answer on Stackoverflow
Solution 7 - JavaWesternGunView Answer on Stackoverflow
Solution 8 - JavaimbenrabiView Answer on Stackoverflow
Solution 9 - JavaSkuld SlërView Answer on Stackoverflow
Solution 10 - JavaB''H Bi'ezras -- Boruch HashemView Answer on Stackoverflow
Solution 11 - JavaYaakov5777View Answer on Stackoverflow