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?
1 Answer
Reset to default 12Java 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
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 -> "
本文标签:
版权声明:本文标题:java - Does switch support objects other than Byte, Short, Character, Integer, enumerated types, and the String type? - Stack Ov 内容由热心网友自发贡献,该文观点仅代表作者本人,
转载请联系作者并注明出处:http://it.en369.cn/questions/1741343171a1863684.html,
本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论