I am a bit of a beginner with coding, so can someone please tell me if it is possible to do something like this in Java, in a void result type?
switch (if this != null {return this;}
else {return that;}) {
case "This": blah
break;
case"That": blah
break;
}
Also, can anyone tell me the most efficient way to do a switch statement with two variables? Thanks! :)
Use the ternary operator:
switch (this != null ? this : that) {
...
}
Or the Optional class:
switch (Optional.ofNullable(this).orElse(that)) {
...
}
The simple non answer: don't do such a thing. Because such code is hard to read and maintain.
If at all, do something like
switch(computeLookupValue(whatever))
But your idea to do that many things in a few lines of code is bad practice.
And then also forget that part about efficiency. In Java, you write efficient code by writing simple code that can be optimized by the just in time compiler for you. Trying to write "smart" Java source code can quickly lead to you preventing the JIT from doing its job!
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