How to use java switch

content:
1. Regular grammar
2. Correct case analysis
3. Error case analysis
 
1. Regular grammar
1.1. switch parameter range
     switch(A), the value of A in parentheses can be byte, short, int, char, String, and enumeration types. Application examples:
//(1)byte
byte baction=2;
switch (baction)
{
    case 1:System.out.print(baction);break;
    case 2:System.out.print(baction);break;
}
//(2)short
short saction=3;
switch (saction)
{
    case 1:System.out.print(saction);break;
    case 3:System.out.print(saction);break;
}
//(3)int
int iaction=4;
switch (iaction)
{
    case 1:System.out.print(iaction);break;
    case 4:System.out.print(iaction);break;
}
//(4)char
char caction='a';
switch (caction)
{
    case 1:System.out.print(caction);break;
    case 'a':System.out.print(caction);break;
}
//(5)String
String straction="abc";
switch (straction)
{
    case "1":System.out.print(straction);break;
    case "abc":System.out.print(straction);break;
}
//(6) Enumeration
String fullStr="00000000000000";
CompletedProgressEnum action=CompletedProgressEnum.basic;
StringBuilder sb = new StringBuilder(fullStr);
switch (action)
{
    case basic:sb.replace(0, 2,CompletedProgressEnum.basic.getValue()); break;
    case edu:sb.replace(2, 4,CompletedProgressEnum.edu.getValue());break;
}
 
      Note : The long type cannot be used as a switch parameter.
1.2.case clause
     case B; case is a constant expression, which means that the value of B can only be a constant (you need to define a final constant) or int, byte, short, char (such as 1, 2, 3, 200000000000 (note this is an integer)).
     
     If there is no matching case, default is executed, and default is an optional option.
 
2. Correct case analysis:
1. Standard type (there is a break statement after the case, and the value after the case is an integer)
int i=3; 
switch(i) 
case 1: 
System.out.println(1); 
break; 
case 2: 
System.out.println(2); 
break;  
default: 
System.out.println("default"); 
break; 
}
 
2.常量型(case后面都有break语句,case后的值都是常量)
private final int NUM1=1;
private final int NUM2=1;
int i=3;
switch(i)
{
case NUM1:
System.out.println(1);
break;
case NUM2:
System.out.println(2);
break; 
default:
System.out.println("default");
break;
}
 
 
三.错误案例分析:
1.第二种情况容易出错的情况:
 发现问题
private int CLICK_QUERY = 1;
private int CLICK_RESET = 2;
@Override
public void onClick(View v)
{
int tag = (Integer) v.getTag();
switch (tag)
{
  case CLICK_QUERY:
  query();
  break;
  case CLICK_RESET:
  reset();
  break;
}
}
编译时一直报错:CLICK_QUERY 和CLICK_RESET——case expressions must be constant expressions
解决问题:

          case后面必须跟常量,必须要常量,将上面两个变量声明为final即可。

private final int CLICK_QUERY = 1;
private final int CLICK_RESET = 2;
2.下面是switch的简单写法:
switch(A){
case B;
}
   final型的变量也是有要求的,也即是它必须是编译时的常量,怎么讲呢,看下面的程序段:
final int a = 0;
final int b;
   第二个语句就是在编译时不能够被识别出值的变量,因为它没有初始化,当然,这条语句也是错误的。所以总结case后的值可以是常数值或final型的值。再看下面的程序段:
  byte a = 11;
  switch(a){// C
  case 11 : System.out.println(" 11 "); break;
  case 225 : System.out.println(" 11 "); break;// D
  }
    该代码正确吗?答案是否定的。虽然在 C 处是合法的也即是byte型的a值可以出现在switch中,但是 D处的语句也即是第二个case后的值是225大小超过了byte的范围,所以是错误的。再就是case后的值不能出现重复。因此在使用中要注意。
 
3.忘记写break的错误
   再就是在使用switch-case中最容易忽视的就是忘记在每个case后处理完后忘记写上break;语句。那它带来的后果是什么呢,下面小程序段会告诉你:
  byte a = 2;
  switch(a){
  case 1 : System.out.println(" A ");
  case 2 : System.out.println(" B ");
  case 3 : System.out.println(" C ");
  case 4 : System.out.println(" D ");
  default : System.out.println(" default ");
  }
=========输出结果为:
default 
--------------------------

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326079040&siteId=291194637