Allen Liao :
I'm trying to initialize two variables with an enhanced switch statement:
int num = //something
boolean val1;
String val2;
val1, val2 = switch(num) {
case 0 -> (true, "zero!");
case 1 -> (true, "one!");
default -> (false, "unknown :/");
}
Is this possible?
ernest_k :
There's no tuple unpacking in Java. A quick alternative that still uses a switch expression could use a custom class (using Pair
in the following example):
Pair<Boolean, String> val = switch (num) {
case 0 -> Pair.of(true, "zero!");
case 1 -> Pair.of(true, "one!");
default -> Pair.of(false, "unknown :/");
};
boolean val1 = val.getLeft();
String val2 = val.getRight();