这个功能会被放到后面的Java版本中吗?

有人能解释一下为什么我不能这样做,比如,Java的switch语句工作的技术方式吗?


当前回答

JDK-13中的JEP 354: Switch Expressions(预览)和JDK-14中的JEP 361: Switch Expressions(标准)将扩展Switch语句,使其可以用作表达式。

现在你可以:

直接从开关表达式中分配变量, 使用新形式的开关标签(case L ->): “case L ->”开关标签右侧的代码被限制为表达式、块或(为方便起见)throw语句。 每个大小写使用多个常量,用逗号分隔, 而且也没有更多的价值突破: 为了从switch表达式中产生一个值,用yield语句代替break with value语句。

所以答案(1,2)的演示可能是这样的:

  public static void main(String[] args) {
    switch (args[0]) {
      case "Monday", "Tuesday", "Wednesday" ->  System.out.println("boring");
      case "Thursday" -> System.out.println("getting better");
      case "Friday", "Saturday", "Sunday" -> System.out.println("much better");
    }

其他回答

带有String情况的Switch语句已经在Java SE 7中实现,距离它们第一次被请求至少有16年了。目前还没有给出延迟发布的明确原因,但可能与性能有关。

JDK 7中的实现

这个特性现在已经在javac中通过“去糖化”过程实现了;在case声明中使用String常量的干净的高级语法在编译时扩展为遵循模式的更复杂的代码。生成的代码使用一直存在的JVM指令。

A switch with String cases is translated into two switches during compilation. The first maps each string to a unique integer—its position in the original switch. This is done by first switching on the hash code of the label. The corresponding case is an if statement that tests string equality; if there are collisions on the hash, the test is a cascading if-else-if. The second switch mirrors that in the original source code, but substitutes the case labels with their corresponding positions. This two-step process makes it easy to preserve the flow control of the original switch.

JVM中的开关

有关交换机的更多技术深度,可以参考JVM规范,其中描述了交换机语句的编译。简而言之,有两种不同的JVM指令可用于开关,这取决于用例使用的常量的稀疏性。两者都依赖于为每个case使用整数常量来有效执行。

如果常量是密集的,它们被用作指令指针表的索引(在减去最小值之后)-表witch指令。

如果常量是稀疏的,则执行对正确情况的二进制搜索——查找开关指令。

在String对象的去糖开关中,两个指令都可能被使用。查找开关适用于哈希码的第一个开关,以查找案件的原始位置。由此产生的序数很自然地适合于大表女巫。

这两个指令都要求分配给每个case的整数常量在编译时进行排序。在运行时,虽然表开关的O(1)性能通常比查找开关的O(log(n))性能更好,但它需要一些分析来确定表是否足够密集,以证明时空权衡的合理性。Bill Venners写了一篇很棒的文章,更详细地介绍了这一点,并介绍了其他Java流控制指令。

JDK 7之前

在JDK 7之前,enum可以近似于基于字符串的开关。它使用编译器在每个枚举类型上生成的静态valueOf方法。例如:

Pill p = Pill.valueOf(str);
switch(p) {
  case RED:  pop();  break;
  case BLUE: push(); break;
}

在Java 11+中,变量也是可行的。唯一的条件是它必须是常数。

例如:

final String LEFT = "left";
final String RIGHT = "right";
final String UP = "up";
final String DOWN = "down";

String var = ...;

switch (var) {
    case LEFT:
    case RIGHT:
    case DOWN:
    default:
        return 0;
}

PS.我还没有在早期的jdk中尝试过这一点。所以请更新答案,如果它也支持。

如果代码中有一个地方可以打开String,那么最好将String重构为可能值的枚举,这样就可以打开String。当然,可以将string的潜在值限制为枚举中的值,这可能是需要的,也可能不是需要的。

当然,你的枚举可以有一个'other'条目,和一个fromString(String)方法,然后你可以有

ValueEnum enumval = ValueEnum.fromString(myString);
switch (enumval) {
   case MILK: lap(); break;
   case WATER: sip(); break;
   case BEER: quaff(); break;
   case OTHER: 
   default: dance(); break;
}

下面是一个基于JeeBee的帖子的完整示例,使用java枚举而不是使用自定义方法。

注意,在Java SE 7及更高版本中,您可以在switch语句的表达式中使用String对象。

public class Main {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {

      String current = args[0];
      Days currentDay = Days.valueOf(current.toUpperCase());

      switch (currentDay) {
          case MONDAY:
          case TUESDAY:
          case WEDNESDAY:
              System.out.println("boring");
              break;
          case THURSDAY:
              System.out.println("getting better");
          case FRIDAY:
          case SATURDAY:
          case SUNDAY:
              System.out.println("much better");
              break;

      }
  }

  public enum Days {

    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY,
    SUNDAY
  }
}

从1.7开始直接使用String的例子如下:

public static void main(String[] args) {

    switch (args[0]) {
        case "Monday":
        case "Tuesday":
        case "Wednesday":
            System.out.println("boring");
            break;
        case "Thursday":
            System.out.println("getting better");
        case "Friday":
        case "Saturday":
        case "Sunday":
            System.out.println("much better");
            break;
    }

}