switch 表达式不能是 float、double 或 boolean

发布于 2024-10-19 16:46:04 字数 182 浏览 1 评论 0原文

为什么 switch 表达式不允许 longfloatdoubleboolean Java 中的值?为什么只允许 int (以及那些自动提升为 int 的)?

Why doesn't the switch expression allow long, float, double or boolean values in Java? why is only int (and those that are automatoically promoted to int) allowed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

屌丝范 2024-10-26 16:46:04

即使可以,float 和 double 也很难可靠地使用 - 不要忘记,由于表示的性质,在 float/double 上执行精确的相等匹配通常是一个坏主意。

对于布尔值,为什么不直接使用 if 开头呢?

老实说,我不记得曾经想要打开任何这些类型。您有特定的用例吗?

Float and double would be awkward to use reliably even if they were possible - don't forget that performing exact equality matches on float/double is usually a bad idea anyway, due to the nature of the representation.

For Boolean values, why not just use if to start with?

I can't remember ever wanting to switch on any of these types, to be honest. Do you have a particular use case in mind?

年少掌心 2024-10-26 16:46:04

您可以在 switch 语句中使用 enum,Java 7 将添加 String AFAIK。 switch 语句来自 C,其中只允许使用 int,并且实现其他类型更为复杂。

浮点数不适合进行 switch,因为精确比较经常会因舍入错误而被破坏。例如 0.11 - 0.1 == 0.01 为 false。

switch on boolean 没有多大用处,因为普通的 if 语句会更简单,但

if(a) {

} else { 

}

使用 BTW 不会更简单

switch(a) {
  case true:

     break;
  case false:

     break;
}

:如果可用的话,我会使用 switch(long),但它不是。无论如何,这对我来说是一个罕见的用例。

You can use enum in a switch statement and Java 7 will add String AFAIK. The switch statement comes from C where only int's were allowed and implementing other types is more complicated.

Floating point numbers are not a good candiates for switch as exact comparison is often broken by rounding errors. e.g. 0.11 - 0.1 == 0.01 is false.

switch on boolean is not much use as a plain if statement would be simpler

if(a) {

} else { 

}

would not be simpler with

switch(a) {
  case true:

     break;
  case false:

     break;
}

BTW: I would use switch(long) if it were available, but its not. Its a rare use case for me any way.

苦行僧 2024-10-26 16:46:04

对于 float 和 double float 和 double 我假设他们省略了它,原因与 为什么使用 == 来比较它们是一个坏主意。

对于布尔值,可能只是因为它无论如何都对应于 if 语句。请记住,case 表达式中只能包含常量,因此 case 始终对应于 if (someBool)if (!someBool)

对于long我没有解释。在我看来,在设计语言时也许应该包含这样的功能。

For float and double float and double I'd assume they have omitted it for the same reasons as why it's a bad idea to compare them using ==.

For boolean, it may simply be because it would correspond to an if statement anyway. Remember that you can only have constants in the case-expressions, so the cases would always correspond to if (someBool) and if (!someBool).

For long I don't have an explanation. Seems to me that such feature perhaps should have been included when designing the language.

你列表最软的妹 2024-10-26 16:46:04

当执行一些基于状态变量的操作时,通常使用 switch-case 结构。 int 有足够多的选择。布尔值只有两个,因此普通的 if 通常就足够了。以这种方式使用双精度数和浮点数并不是那么准确。

坦率地说,我无法想象这个东西的用例,您对这个问题有什么实际问题吗?

Usually switch-case structure is used when executing some operations based on a state variable. There an int has more than enough options. Boolean has only two so a normal if is usually good enough. Doubles and floats aren't really that accurate to be used in this fashion.

Frankly I can't imagine a use case for this stuff, did you have some practical problem in mind with this question?

长途伴 2024-10-26 16:46:04

双打也有同样的问题,并有解决方案。

switch 大括号内,将 double 转换为 String 和 do case "String"
例如:

double d = 1.5
switch (String.valueOf(d)) {
       case "1.5": 
       break;
}

Had the same problem with a double and have a solution.

Within the switch braces you cast the double to a String and the do case "String".
For example:

double d = 1.5
switch (String.valueOf(d)) {
       case "1.5": 
       break;
}
落花浅忆 2024-10-26 16:46:04

是的,Java 不允许在 Switch 语句中使用 boolean、double 和 float。
首先是 Switch 语句,最初取自 C lang。并且在 C Switch 语句中仅支持在 Switch 中使用整数。
但是Java还在JAva7中添加了一个String,用于在Switch中使用来比较预定义的String。
双精度和浮动
double 和 float 值可以对任何最接近的值进行四舍五入并再次变为整数,在这种情况下,可能会在 switch 语句中选择两种不同的情况,因此在 switch 语句中使用 double 和 float 是一个非常糟糕的主意切换语句。

布尔值
true 和 false 可以用 if 和 else ,为什么还要用 Switch 呢?为了找到正确的大小写,Switch 语句将搜索该大小写,直到以默认大小写结束。所以只有两种情况,不需要使用 Switch 语句。

Yes, Java has not allowed boolean, double, and float in Switch Statement.
The first thing is Switch Statement originally taken from C lang. and in C Switch statement only supports ti Integral Numbers to use in Switch.
But Java Also Added a String in JAva7 to use in Switch to compare the predefined String.
double and float
double and float values can be rounded off any nearest value and becomes the whole number again, in this case, there might be the possibility of choosing two different cases in a switch statement, so it's a very bad idea to use double and float in a Switch statement.

boolean
for true and false, we can use if and else, why use Switch again.? to find the correct case, the Switch statement will be searching for the case until it ends with the default case. so just for 2 cases, it's not needed to use a Switch statement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文