java中的Switch()的括号里面能否是Double类型的值
时间: 2024-02-09 18:12:49 浏览: 67
在Java中,Switch语句的括号里必须是一个整型表达式,也就是说只能是byte、short、int、char类型的值。因此,不能直接使用double类型的值作为Switch语句的条件表达式。
但是,从Java 7开始,Switch语句支持了String类型的值作为条件表达式。这是因为String类型内部实现了hashCode()和equals()方法,可以被用来进行比较。例如:
```
String fruit = "apple";
switch (fruit) {
case "apple":
System.out.println("This is an apple.");
break;
case "banana":
System.out.println("This is a banana.");
break;
default:
System.out.println("Unknown fruit.");
break;
}
```
在这个例子中,fruit变量是一个String类型的值,可以被用作Switch语句的条件表达式。注意,每个case标签都是一个String类型的值,用引号括起来。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)