这个功能会被放到后面的Java版本中吗?
有人能解释一下为什么我不能这样做,比如,Java的switch语句工作的技术方式吗?
这个功能会被放到后面的Java版本中吗?
有人能解释一下为什么我不能这样做,比如,Java的switch语句工作的技术方式吗?
当前回答
带有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 6的另一种方式:
String runFct =
queryType.equals("eq") ? "method1":
queryType.equals("L_L")? "method2":
queryType.equals("L_R")? "method3":
queryType.equals("L_LR")? "method4":
"method5";
Method m = this.getClass().getMethod(runFct);
m.invoke(this);
James Curran简洁地说:“基于整数的开关可以优化为非常高效的代码。基于其他数据类型的开关只能编译为一系列if()语句。因此,C和c++只允许在整数类型上切换,因为这对其他类型是没有意义的。”
My opinion, and it's only that, is that as soon as you start switching on non-primitives you need to start thinking about "equals" versus "==". Firstly comparing two strings can be a fairly lengthy procedure, adding to the performance problems that are mentioned above. Secondly if there is switching on strings there will be demand for switching on strings ignoring case, switching on strings considering/ignoring locale,switching on strings based on regex.... I would approve of a decision that saved a lot of time for the language developers at the cost of a small amount of time for programmers.
如果代码中有一个地方可以打开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;
}
这个回答很好地解释了技术细节。我只是想添加Java 12开关表达式,你可以用下面的语法做到这一点:
String translation(String cat_language) {
return switch (cat_language) {
case "miau miau" -> "I am to run";
case "miauuuh" -> "I am to sleep";
case "mi...au?" -> "leave me alone";
default -> "eat";
};
}
下面是一个基于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
}
}