How can I simplify the following code to one line (without the if-else statement)?
public static void main(String[] args)
{
boolean result;
boolean a = false;
boolean b = false;
boolean isEmpty = false;
if (a) {
result = isEmpty && !b;
System.out.println("if " + (isEmpty && !b));
} else {
System.out.println("else " + !b);
result = !b;
}
System.out.println(result);
}
If a is true, you must check that isEmpty AND !b are both true. If a is false (or !a is true), it's enough to check that !b is true.
Therefore you can replace the logic of the if statement with:
result = !b && (isEmpty || !a);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With