admin管理员组

文章数量:1026989

JDK 7 added support for String with a switch, which extended the list of types supported by switch. As of Java 7, a switch supported the primitive types byte, short, char, and int, their respective wrapper types (Byte, Short, Character, and Integer), enumerated types, and the String type.

Does switch support objects other than Byte, Short, Character, Integer, enumerated types, and the String type?

JDK 7 added support for String with a switch, which extended the list of types supported by switch. As of Java 7, a switch supported the primitive types byte, short, char, and int, their respective wrapper types (Byte, Short, Character, and Integer), enumerated types, and the String type.

Does switch support objects other than Byte, Short, Character, Integer, enumerated types, and the String type?

Share Improve this question asked Feb 22 at 11:36 Arvind Kumar AvinashArvind Kumar Avinash 79.6k10 gold badges92 silver badges135 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 12

Java 21

In Java 16, JEP 394 extended the instanceof operator to take a type pattern and perform pattern matching. Leveraging this feature, in Java 21, JEP 441 made it possible to use case labels with patterns rather than just constants with switch statements and expressions.

Demo:

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

class Main {
    private static final DateTimeFormatter FORMATTER =
            DateTimeFormatter.ofPattern("[MMMM dd, uuuu][ h:mm a][ VV][ XXX]")
                    .localizedBy(Locale.ENGLISH);

    static String format(Object obj) {
        return switch (obj) {
            case null -> "

JDK 7 added support for String with a switch, which extended the list of types supported by switch. As of Java 7, a switch supported the primitive types byte, short, char, and int, their respective wrapper types (Byte, Short, Character, and Integer), enumerated types, and the String type.

Does switch support objects other than Byte, Short, Character, Integer, enumerated types, and the String type?

JDK 7 added support for String with a switch, which extended the list of types supported by switch. As of Java 7, a switch supported the primitive types byte, short, char, and int, their respective wrapper types (Byte, Short, Character, and Integer), enumerated types, and the String type.

Does switch support objects other than Byte, Short, Character, Integer, enumerated types, and the String type?

Share Improve this question asked Feb 22 at 11:36 Arvind Kumar AvinashArvind Kumar Avinash 79.6k10 gold badges92 silver badges135 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 12

Java 21

In Java 16, JEP 394 extended the instanceof operator to take a type pattern and perform pattern matching. Leveraging this feature, in Java 21, JEP 441 made it possible to use case labels with patterns rather than just constants with switch statements and expressions.

Demo:

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

class Main {
    private static final DateTimeFormatter FORMATTER =
            DateTimeFormatter.ofPattern("[MMMM dd, uuuu][ h:mm a][ VV][ XXX]")
                    .localizedBy(Locale.ENGLISH);

    static String format(Object obj) {
        return switch (obj) {
            case null -> "

本文标签: