这个功能会被放到后面的Java版本中吗?
有人能解释一下为什么我不能这样做,比如,Java的switch语句工作的技术方式吗?
这个功能会被放到后面的Java版本中吗?
有人能解释一下为什么我不能这样做,比如,Java的switch语句工作的技术方式吗?
当前回答
从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;
}
}
其他回答
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.
除了以上好的论点,我还要补充一点,今天很多人认为switch是Java过去过程化的过时残余(回到C时代)。
我不完全同意这个观点,我认为switch在某些情况下有它的用处,至少因为它的速度,无论如何,它比我在一些代码中看到的一系列级联数字要好…
但实际上,有必要考虑一下需要开关的情况,看看它是否不能被更面向对象的东西所取代。例如Java 1.5+中的枚举,也许是哈希表或其他集合(有时我后悔我们没有(匿名)函数作为第一类公民,就像Lua -没有开关-或JavaScript),甚至是多态性。
带有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;
}
基于整数的开关可以优化为非常高效的代码。基于其他数据类型的开关只能编译为一系列if()语句。
因此,C和c++只允许对整型进行切换,因为它对其他类型没有意义。
c#的设计者认为风格很重要,即使它没有什么优势。
Java的设计者显然和C的设计者思想相似。
其他回答说,这是在Java 7中添加的,并为早期版本提供了变通办法。这个答案试图回答“为什么”
Java是对c++过于复杂的一种反应。它被设计成一种简单干净的语言。
String在语言中有一些特殊情况的处理,但在我看来很明显,设计师试图将特殊的大小写和语法糖分保持在最低限度。
由于字符串不是简单的基元类型,因此打开字符串实际上是相当复杂的。在设计Java的时候,这并不是一个常见的特性,也不太适合极简设计。特别是当他们决定不为字符串使用特殊case ==时,case在==不工作的情况下工作会有点奇怪。
在1.0到1.4之间,语言本身基本保持不变。对Java的大多数增强都是在库方面。
这一切在Java 5中都有所改变,该语言得到了实质性的扩展。在版本7和8中进行了进一步的扩展。我认为这种态度的转变是由c#的兴起所推动的