1

In case of assignment the situation is simple,

result = testCondition ? value1 : value2;

But what if I want to use it instead of an if statement? for instance in a logging situation:

logger.shouldDebbug ? logger.log("logging") : (what to do if not?);

In the case I don't what to do anything in the case of false, can I still use this Operator?

boaz shor
  • 299
  • 1
  • 3
  • 16
  • 2
    "It is a compile-time error for either the second or the third operand expression to be an invocation of a void method." – Arnaud Jun 20 '18 at 14:12
  • No you can't :) – Halayem Anis Jun 20 '18 at 14:12
  • 3
    [The ternary operator is an expression, not an instruction or statement](https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.25). So no, w.l.o.g. you cannot use it as an alternative for an `if`. – Turing85 Jun 20 '18 at 14:13
  • You can use to conditionally produce a *value* in the middle of a statement. You can't use it to conditionally insert a statement. It is not a replacement for `if`. – khelwood Jun 20 '18 at 14:18
  • 2
    Possible duplicate of [Java Ternary without Assignment](https://stackoverflow.com/questions/15977031/java-ternary-without-assignment) – Arnaud Jun 20 '18 at 14:19

3 Answers3

3

Yes you can if you wrap them in a returning function, but no you shouldn't.

In your example of the logger, let your logger output to void, discard the input when debugging isn't enabled.

You do not want to riddle your code with all these logging checks. Perform a check as least and as central as possible.

Either have a check in the logger.log function if debugging is enabled, or replace the logger with a dummy mock that does nothing except accept input and immediately discard it.

If you use standard logging frameworks like log4j you can set debugging levels, where you show only info or more serious, only warnings or more serious, only errors or more serious.

The same goes for other "quick" checks. If you find yourself using a certain pattern a lot, write a utility class for it with a static method if need be, so you have one place, where you have to change stuff, instead of 200 code points that you have to update when going to production.

Tschallacka
  • 27,901
  • 14
  • 88
  • 133
0

You could use it if you insist, by defining a meaningless variable and take advantage of the functions' side-effects, but that's not a very good coding habit. It's purely a work-around.

For example:

public static boolean test() {
    return 1>0;
}

public static int success() {
    System.out.println("true");
    return 0; // has no meaning whatsoever
}

public static int fail() {
    System.out.println("false");
    return 0; // has no meaning whatsoever
}

public static void main(String[] args) {
    int meaningless = test() ? success() : fail();
}
GalAbra
  • 5,048
  • 4
  • 23
  • 42
0

Everything has been explained in comments, so I will put here only some idea:

public class Ternary {
    private final boolean condition;
    private Ternary(boolean condition) { this.condition = condition; }
    public static Ternary of(boolean condition) { return new Ternary(condition); } 
    public Ternary onTrue(Runnable r) { if (condition) { r.run(); } return this; }
    public Ternary onFalse(Runnable r) { if (!condition) { r.run(); } return this; }
}

Example of usage:

Ternary.of(o != null).onTrue(() -> doSomething()).onFalse(() -> doSomethingElse());

But simplier would be to write:

if (o != null) { doSomething(); } else { doSomethingElse(); }
matoni
  • 2,479
  • 21
  • 39