Java Equivalent to iif function
Asked Answered
B

4

11

the question is simple, there is a functional equivalent of the famous iif in java?

For example:

IIf (vData = "S", True, False)

Thanks in advance.

Brainstorm answered 13/1, 2011 at 20:36 Comment(2)
JJnguy: No, iif is an inline-if function that takes a boolean value as its first parameter, and when true returns the second parameter, and when false returns the third parameter.Bindery
For posterity: Please note that the accepted answer is not actually correct with respect to short-circuiting. Read https://mcmap.net/q/239237/-iif-equivalent-in-c instead ...Tarrel
B
27
vData.equals("S") ? true : false

or in this particular case obviously one could just write

vData.equals("S")
Belsen answered 13/1, 2011 at 20:38 Comment(4)
Especially the second bit. That's not so obvious to a lot of people.Sniperscope
This isn't quite the same thing. Using IIF(x, y, z) will always evaluate y and z (causing side-effects of both to be visible), while x ? y : z will only evaulate y xor z (causing the side-effects of only one to be visible).Bindery
@Gabe: VB doesn't support short-circuit evaluation in general. Although VB.NET doesCapriccio
Thanks Adrian, all answers helped me.Brainstorm
C
6

Yeah, the ternary op ? :

vData.equals("S") ? true : false
Capriccio answered 13/1, 2011 at 20:38 Comment(0)
T
4

The main difference between the Java ternary operator and IIf is that IIf evaluates both the returned value and the unreturned value, while the ternary operator short-circuits and evaluates only the value returned. If there are side-effects to the evaluation, the two are not equivalent.

You can, of course, reimplement IIf as a static Java method. In that case, both parameters will be evaluated at call time, just as with IIf. But there is no builtin Java language feature that equates exactly to IIf.

public static <T> T iif(boolean test, T ifTrue, T ifFalse) {
    return test ? ifTrue : ifFalse;
}

(Note that the ifTrue and ifFalse arguments must be of the same type in Java, either using the ternary operator or using this generic alternative.)

Tarrel answered 13/1, 2011 at 20:43 Comment(2)
thanks, I understand, similar to the function AndAlso, OrElse in VB NET, right?Brainstorm
Yes, that's another example of short-circuiting.Tarrel
I
2

if is the same as the logical iff.

boolean result;
if (vData.equals("S"))
   result = true;
else
   result = false;

or

boolean result = vData.equals("S") ? true : false;

or

boolean result = vData.equals("S");

EDIT: However its quite likely you don't need a variable instead you can act on the result. e.g.

if (vData.equals("S")) {
   // do something
} else {
   // do something else
}

BTW it may be considered good practice to use

 if ("S".equals(vData)) {

The difference being that is vData is null the first example will throw an exception whereas the second will be false. You should ask yourself which would you prefer to happen.

Ichthyo answered 13/1, 2011 at 20:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.