boolean result = switch (ternaryBool) {
case TRUE -> true;
case FALSE -> false;
case FILE_NOT_FOUND -> throw new UncheckedIOException(
"This is ridiculous!",
new FileNotFoundException());
default -> throw new IllegalArgumentException("Seriously?!");
};
// Тернарный boolean
enum Bool {
TRUE,
FALSE,
FILE_NOT_FOUND
};
boolean result;
switch (ternaryBool) {
case TRUE:
result = true;
break;
case FALSE:
result = false;
break;
case FILE_NOT_FOUND:
// объявление переменной для демонстрации проблемы в default
var ex = new UncheckedIOException("This is ridiculous!",
new FileNotFoundException());
throw ex;
default:
// А вот и проблема: мы не можем объявить еще одну переменную с именем ex
var ex2 = new IllegalArgumentException("Seriously?!");
throw ex2;
}
int result;
switch (number) {
case 1:
result = callMethod("one");
break;
case 2:
result = callMethod("two");
break;
default:
result = callMethod("many");
break;
}
private static boolean toBoolean(Bool ternaryBool) {
switch (ternaryBool) {
case TRUE: return true;
case FALSE: return false;
case FILE_NOT_FOUND:
throw new UncheckedIOException("This is ridiculous!",
new FileNotFoundException());
// без default метод не скомпилируется
default:
throw new IllegalArgumentException("Seriously?!");
}
}
boolean result = switch (ternaryBool) {
case TRUE -> true;
case FALSE -> false;
case FILE_NOT_FOUND -> throw new UncheckedIOException(
"This is ridiculous!",
new FileNotFoundException());
// в ветке `default` уже нет необходимости
default -> throw new IllegalArgumentException("Seriously?!");
};
if (condition) {
result = doThis();
} else {
result = doThat();
}
result = condition ? doThis() : doThat();
boolean result = switch (ternaryBool) {
case TRUE:
yield true;
case FALSE:
yield false;
case FILE_NOT_FOUND:
throw new UncheckedIOException(
"This is ridiculous!",
new FileNotFoundException());
default:
throw new IllegalArgumentException("Seriously?!");
};
switch (number) {
case 1:
case 2:
callMethod("few");
break;
default:
callMethod("many");
break;
}
String result = switch (ternaryBool) {
case TRUE, FALSE -> "sane";
default -> "insane";
};
switch (number) {
case 1 -> callMethod("one");
case 2 -> callMethod("two");
default -> callMethod("many");
}
switch (ternaryBool) {
case TRUE, FALSE -> System.out.println("Bool was sane");
default -> System.out.println("Bool was insane");
};
boolean result = switch (Bool.random()) {
case TRUE -> {
System.out.println("Bool true");
yield true;
}
case FALSE -> {
System.out.println("Bool false");
yield false;
}
case FILE_NOT_FOUND -> {
var ex = new UncheckedIOException(
"This is ridiculous!",
new FileNotFoundException());
throw ex;
}
default -> {
var ex = new IllegalArgumentException(
"Seriously?!");
throw ex;
}
};
String result = switch (ternaryBool) {
case TRUE, FALSE -> "sane";
default -> "insane";
};
Serializable serializableMessage = switch (bool) {
case TRUE, FALSE -> "sane";
// note that we don't throw the exception!
// but it's `Serializable`, so it matches the target type
default -> new IllegalArgumentException("insane");
};
// compiler infers super type of `String` and
// `IllegalArgumentException` ~> `Serializable`
var serializableMessage = switch (bool) {
case TRUE, FALSE -> "sane";
// note that we don't throw the exception!
default -> new IllegalArgumentException("insane");
};
public String sanity(Bool ternaryBool) {
switch (ternaryBool) {
// `return` is only possible from block
case TRUE, FALSE -> { return "sane"; }
default -> { return "This is ridiculous!"; }
}
}
int result = 0;
switch (sign) {
case '+' -> result = a + b;
case '^' -> {
result = 1;
for (int i = 1; i <= b; i++) {
result *= a;
}
}
default -> System.out.println("Мат. операция не поддерживается");
}
public String sanity(Bool ternaryBool) {
String result = switch (ternaryBool) {
// this does not compile - error:
// "return outside of enclosing switch expression"
case TRUE, FALSE -> { return "sane"; }
default -> { return "This is ridiculous!"; }
};
}
// compile error:
// "the switch expression does not cover all possible input values"
boolean result = switch (ternaryBool) {
case TRUE -> true;
// no case for `FALSE`
case FILE_NOT_FOUND -> throw new UncheckedIOException(
"This is ridiculous!",
new FileNotFoundException());
};
// compiles without `default` branch because
// all cases for `ternaryBool` are covered
boolean result = switch (ternaryBool) {
case TRUE -> true;
case FALSE -> false;
case FILE_NOT_FOUND -> throw new UncheckedIOException(
"This is ridiculous!",
new FileNotFoundException());
};
Object obj = // ...
// работает с Java 16
if (obj instanceof String str)
callStringMethod(str);
else if (obj instanceof Number no)
callNumberMethod(no);
else
callObjectMethod(obj);
// работает (как превью) с JDK 17+
switch (obj) {
case String str -> callStringMethod(str);
case Number no -> callNumberMethod(no);
default -> callObjectMethod(obj);
}
String str = // ...
String length = switch (str) {
case str.length() > 42 -> "long";
case str.length() > 19 -> "medium";
case str.length() > 1 -> "small";
case null || str.length() == 0 -> "empty";
};