1.3.1 switch 语句中的 String
switch语句是一种高效的多路语句,可以省掉很多繁杂的嵌套if判断;
在Java 6及之前,case语句中的常量只能是byte、char、short和int(也可以是对应的封装类)或枚举常量,在Java 7规范中增加了String,毕竟它也是常量类型;
Demo:
public class CoinSwitchString {
public static void main(String[] args) {
printDay("Sunday");
printDay("Tuesday");
printDay("Ten");
}
public static void printDay(String dayOfWeek) {
switch(dayOfWeek) {
case "Sunday": System.out.println("星期日"); break;
case "Monday": System.out.println("星期一"); break;
case "Tuesday": System.out.println("星期二"); break;
case "Wednesday": System.out.println("星期三"); break;
case "Thursday": System.out.println("星期四"); break;
case "Friday": System.out.println("星期五"); break;
case "Saturday": System.out.println("星期六"); break;
default: System.out.println("Error: '" + dayOfWeek + "' is not a day of the week!");
}
}
}
Ran As Java Application:
星期日
星期二
Error: 'Ten' is not a day of the week!
1.3.1 switch 语句中的 String的更多相关文章
- 关于switch语句中使用String类型的实现原理
在Java 7 以后,switch语句可以用作String类型上. 从本质来讲,switch对字符串的支持,其实也是int类型值的匹配.它的实现原理如下: 通过对case后面的String对象调用ha ...
- switch语句中的选择因子
switch语句能否用作用在byte上,能否作用在long上,能否作用在String上? switch选择语句的格式为: switch(intergral-selector){ case integ ...
- C++17尝鲜:在 if 和 switch 语句中进行初始化
初始化语句 在C++17中,类似于 for 语句,在 if 和 switch 语句的判断条件之前也能加上初始化语句,语法形式如下: if (初始化语句; 条件) 语句 else 语句 switch ( ...
- mysql语句中把string类型字段转datetime类型
mysql语句中把string类型字段转datetime类型 在mysql里面利用str_to_date()把字符串转换为日期 此处以表h_hotelcontext的Start_time和En ...
- 在 php 7.3 中 switch 语句中使用 continue
在 php 7.3 中 switch 语句中使用 continue 在 php 7.3 的 switch 中使用 continue 会出现警告.1 2 3 while ($foo) { switch ...
- C++中switch 语句中的变量声明和
switch 内部的变量定义: ; switch(i) { : string str; //error ; //error int val2; //right ; //right : val2 = ; ...
- switch语句中 参数的类型
switch可作用于char byte short int switch可作用于char byte short int对应的包装类 switch不可作用于long double float boole ...
- JavaScript对于switch语句中的case后键入值的带不带引号
一.用switch选择星期几 这时case为 例:case="星期一" 二.用switch选择单纯的数字 这时case为 例:case='1' 三.用switch选择一个应用了 ...
- switch语句中default用法详解
当年学C语言switch开关语句的时候,很多人会告诉你它是这么用的: switch(表达式){ case常量表达式1: 语句1;break; case常量表达式2: 语句2;break; - case ...
随机推荐
- How to solve “sudo: /etc/sudoers.d is world writable”
Run pkexec chmod 0440 /etc/sudoers
- cf D. Vessels
http://codeforces.com/contest/371/problem/D 第一遍写的超时了,然后看了别人的代码,思路都是找一个点的根,在往里面加水的时候碗中的水满的时候建立联系.查询的时 ...
- Expanding Rods
http://poj.org/problem?id=1 #include<cstdio> #include<cstring> #include<cmath> #in ...
- 调用API函数,在窗口非客户区绘图(通过GetWindowDC获得整个窗口的DC,就可以随意作画了)
http://hi.baidu.com/3582077/item/77d3c1ff60f9fa5ec9f33754 调用API函数,在窗口非客户区绘图 GDI+的Graphics类里有个FromHdc ...
- Sql语句中IN等方面的用法
select * from txt1 select * from txt2 select * from txt1 where name in (select name from txt2 where ...
- 《Ruby语言入门教程v1.0》学习笔记-03
10.09 第七章 7.1 模块 Ruby标准包里的 Math 模块提供了许多方法,比如:求平方根 sqrt ,使用的时候要这么写:模块名.方法名(参数).如:Math.sqrt( a*5+b ) M ...
- typedef 用法及 指针函数 和 函数指针
typedef 本质上是定义了一种新的类型, 该新类型可以原有类型的别名或是原有类型的组合. 而#define 只是字符串的替换. 如定义: typedef char* CHARP; 则 CHARP ...
- MySQL数据库的安装
官方下载地址: http://downloads.mysql.com/archives/community/ msi为安装版,zip为免安装版,最新版本的MySQL没有64位windows的msi版囧 ...
- Hat’s Words hdu-1247
就是查找这个单词能不能有两个单词组成,简单的字典树题目 ////////////////////////////////////////////////////////////// #include& ...
- KVM几种缓存模式
原文在这里: http://pic.dhe.ibm.com/infocenter/lnxinfo/v3r0m0/index.jsp?topic=%2Fliaat%2Fliaatbpkvmguestca ...