Przykłady do wpisu o nowej składni switch w Javie 13
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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