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?
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?
float
or double
for version information. 1.1
will always be 1.1
if you don't do any calculations. Even Java uses this trick to display version information sometimes. But I guess we'll have to byte the bullit and compare against an actual object instance representing a Version
- or use String
comparison from Java 7 onwards of course. –
Almsgiver float
or double
number. You can have a literal which will convert to the closest float
or double
, but neither can precisely-represent 1.1. Did you actually mean version 1.100000000000000088817841970012523233890533447265625 (double
) or version 1.10000002384185791015625 (float
)? –
Upstairs String
switch which does not have these issues. Using a double
or float
for a version is a hack anyway. –
Almsgiver float
or double
. Even with just major/minor, you have odd things - version 1.10 (the 11th minor version in major version 1) would be equal to 1.1. How is that useful? Using float
or double
for a version number is just wrong in my view. –
Upstairs int x = 20; switch(true){ case x >0 && x <10 : ...; case x >10 && x <20: ... ; case x >20 && x <30: ... ; ....}
. This is really useful. –
Bratislava 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.
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.
switch
on long
. That's a surprising amount of work for spectacularly little gain. (I guess why there wasn't switch
on long
in 1.0 is that it wouldn't fit with the existing pair of switch
ops, so would either have to add two more pointless ops or provide a separate mechanism for compiling this case. And JDK 1.00 needed to ship to Netscape in six weeks.) –
Loreeloreen ints
so yes, it would probably involve a change in the bytecode set which is a huge thing to begin with. Updated the answer slighly. –
Unconstitutional 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?
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;
}
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.
© 2022 - 2024 — McMap. All rights reserved.
if ... else
– Hertel