switch


Character ch='X';
Byte bt=56;
Short st=56;
Integer in=56;
String str="a";

Character ch2=null;
Byte bt2=null;
Short st2=null;
Integer in2=null;
String str2=null;
switch (ch){case 'X':System.out.println(ch);break;default:System.out.println("kong");}
switch (bt){case 56:System.out.println(bt);break;default:System.out.println("kong");}
switch (st){case 56:System.out.println(st);break;default:System.out.println("kong");}
switch (in){case 56:System.out.println(in);break;default:System.out.println("kong");}
switch (str){case "a":System.out.println(str);break;default:System.out.println("kong");}

switch (ch2){case 'X':System.out.println(ch2);break;default:System.out.println("kong");}
switch (bt2){case 56:System.out.println(bt2);break;default:System.out.println("kong");}
switch (st2){case 56:System.out.println(st2);break;default:System.out.println("kong");}
switch (in2){case 56:System.out.println(in2);break;default:System.out.println("kong");}
switch (str2){case "a":System.out.println(str2);break;default:System.out.println("kong");}

运行结果:
X
56
56
56
a
Exception in thread "main" java.lang.NullPointerException

原因分析: 
编译后的文件如下:
Character localCharacter = Character.valueOf('X');
Byte localByte = Byte.valueOf(56);
Short localShort = Short.valueOf(56);
Integer localInteger = Integer.valueOf(56);
String str1 = "a";
    Object localObject1 = null;
    Object localObject2 = null;
    Object localObject3 = null;
    Object localObject4 = null;
    String str2 = null;
    switch (localCharacter.charValue()) { case 'X': System.out.println(localCharacter); break; default: System.out.println("kong"); }
    switch (localByte.byteValue()) { case 56: System.out.println(localByte); break; default:  System.out.println("kong"); }
    switch (localShort.shortValue()) { case 56: System.out.println(localShort); break; default:  System.out.println("kong"); }
    switch (localInteger.intValue()) { case 56: System.out.println(localInteger); break; default: System.out.println("kong"); }
   
String str3 = str1; int i = -1;
   switch (str3.hashCode()) { case 97:  if (!str3.equals("a")) break; i = 0; }
   switch (i) { case 0: System.out.println(str1); break; default: System.out.println("kong");  }
    switch (localObject1.charValue()) { case 'X':
      System.out.println(localObject1); break;
    default:
      System.out.println("kong"); }
    switch (localObject2.byteValue()) { case 56:
      System.out.println(localObject2); break;
    default:
      System.out.println("kong"); }
    switch (localObject3.shortValue()) { case 56:
      System.out.println(localObject3); break;
    default:
      System.out.println("kong"); }
    switch (localObject4.intValue()) { case 56:
      System.out.println(localObject4); break;
    default:
      System.out.println("kong"); }
   
str3 = str2; i = -1; switch (str3.hashCode()) { case 97:
      if (!str3.equals("a")) break; i = 0; } switch (i) { case 0:
      System.out.println(str2); break;
    default:
      System.out.println("kong"); }



总结:
1.switch支持的数据类型:可以转换为int的数据类型
char byte short int 
Character Byte Short Integer 
String 
enum

2.Character 调用getCharValue返回char类型,同理Byte Short Integer

3.String 类型先比较hashCode是否相等,然后使用equals比较值是否相等

4.不支持Object类型

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值