Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Przykłady do wpisu o nowej składni switch w Javie 13
String formatted =
switch (obj) {
case Integer i -> String.format("int %d", i);
case Byte b -> String.format("byte %d", b);
case Long l -> String.format("long %d", l);
case Double d -> String.format("double %f", d);
case String s -> String.format("String %s, s);
default -> String.format("Object %s", obj);
};
public class SwitchExpressions {
public static void main(String[] args) {
DaysOfWeek day = SUNDAY;
System.out.println("Is it a weekend?");
// #1 old way
switch(day) {
case SUNDAY:
case SATURDAY:
System.out.println("Yes!");
break;
case FRIDAY:
System.out.println("Almost!");
break;
case MONDAY:
case TUESDAY:
System.out.println("No");
default:
System.out.println(", but it is closer and closer");
}
// #2 new way
switch (day) {
case SUNDAY, SATURDAY -> System.out.println("Yes!");
case FRIDAY -> System.out.println("Almost!");
case MONDAY, TUESDAY -> System.out.println("No");
default -> System.out.println("No, but it is closer and closer");
}
}
}
public class SwitchExpressionsFive {
public static void main(String[] args) {
DaysOfWeek day = SUNDAY;
System.out.println("Is it a weekend?");
System.out.println(switch (day) {
case SUNDAY, SATURDAY -> "Yes!";
case FRIDAY -> "Almost!";
case MONDAY, TUESDAY -> "No";
default -> "No, but it is closer and closer";
});
}
}
public class SwitchExpressionsFour {
public static void main(String[] args) {
DaysOfWeek day = SUNDAY;
System.out.println("Is it a weekend?");
String isWeekend = switch(day) {
case SUNDAY:
case SATURDAY:
yield "Yes!";
case FRIDAY:
yield "Almost!";
case MONDAY:
System.out.println("Yw, it's start of the week");
case TUESDAY:
yield "No";
default:
yield "No, but it is closer and closer";
};
System.out.println(isWeekend);
}
}
public class SwitchExpressionsThree {
public static void main(String[] args) {
DaysOfWeek day = SUNDAY;
System.out.println("Is it a weekend?");
String isNewWeekend = switch (day) {
case SUNDAY, SATURDAY -> "Yes!";
case FRIDAY -> "Almost!";
case MONDAY, TUESDAY -> "No";
default -> {
var x = 10 + 13;
// do something
yield "No, but it is closer and closer";
}
};
System.out.println(isNewWeekend);
}
}
public class SwitchExpressionsTwo {
public static void main(String[] args) {
DaysOfWeek day = SUNDAY;
System.out.println("Is it a weekend?");
// #1 old way
String isWeekend;
switch(day) {
case SUNDAY:
case SATURDAY:
isWeekend = "Yes!";
break;
case FRIDAY:
isWeekend = "Almost!";
break;
case MONDAY:
case TUESDAY:
isWeekend = "No";
break;
default:
isWeekend = "No, but it is closer and closer";
}
System.out.println(isWeekend);
// #2 new way
String isNewWeekend = switch (day) {
case SUNDAY, SATURDAY -> "Yes!";
case FRIDAY -> "Almost!";
case MONDAY, TUESDAY -> "No";
default -> "No, but it is closer and closer";
};
System.out.println(isNewWeekend);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.