[初级]Java中的switch对整型、字符型、字符串的具体实现细节

转载自 [初级]Java中的switch对整型、字符型、字符串的具体实现细节

Java 7中,switch的参数可以是String类型了,这对我们来说是一个很方便的改进。到目前为止switch支持这样几种数据类型:byteshort int char String 。但是,作为一个程序员我们不仅要知道他有多么好用,还要知道它是如何实现的,witch对整型的支持是怎么实现的呢?对字符型是怎么实现的呢?String类型呢?有一点Java开发经验的人这个时候都会猜测switch对String的支持是使用equals()方法和hashcode()方法。那么到底是不是这两个方法呢?接下来我们就看一下,switch到底是如何实现的。

一、switch对整型支持的实现

下面是一段很简单的Java代码,定义一个int型变量a,然后使用switch语句进行判断。执行这段代码输出内容为5,那么我们将下面这段代码反编译,看看他到底是怎么实现的。


   
   
  1. public class switchDemoInt {
  2.     public static void main(String[] args) {
  3.         int a = 5;
  4.         switch (a) {
  5.         case 1:
  6.            System. out.println( 1);
  7.             break;
  8.         case 5:
  9.            System. out.println( 5);
  10.             break;
  11.         default:
  12.             break;
  13.        }
  14.    }
  15. }
  16. //output 5

反编译后的代码如下:


   
   
  1. public class switchDemoInt
  2. {
  3.     public switchDemoInt()
  4.    {
  5.    }
  6.     public static void main(String args[])
  7.    {
  8.         int a = 5;
  9.         switch(a)
  10.        {
  11.         case 1: // '\001'
  12.            System. out.println( 1);
  13.             break;
  14.         case 5: // '\005'
  15.            System. out.println( 5);
  16.             break;
  17.        }
  18.    }
  19. }

我们发现,反编译后的代码和之前的代码比较除了多了两行注释以外没有任何区别,那么我们就知道,switch对int的判断是直接比较整数的值。

二、switch对字符型支持的实现

直接上代码:


   
   
  1. public class switchDemoInt {
  2.     public static void main(String[] args) {
  3.         char a = 'b';
  4.         switch (a) {
  5.         case 'a':
  6.            System. out.println( 'a');
  7.             break;
  8.         case 'b':
  9.            System. out.println( 'b');
  10.             break;
  11.         default:
  12.             break;
  13.        }
  14.    }
  15. }

编译后的代码如下: `public class switchDemoChar


   
   
  1. public class switchDemoChar
  2. {
  3.     public switchDemoChar()
  4.    {
  5.    }
  6.     public static void main(String args[])
  7.    {
  8.         char a = 'b';
  9.         switch(a)
  10.        {
  11.         case 97: // 'a'
  12.            System. out.println( 'a');
  13.             break;
  14.         case 98: // 'b'
  15.            System. out.println( 'b');
  16.             break;
  17.        }
  18.  }
  19. }

通过以上的代码作比较我们发现:对char类型进行比较的时候,实际上比较的是ascii码,编译器会把char型变量转换成对应的int型变量

三、switch对字符串支持的实现

还是先上代码:


   
   
  1. public class switchDemoString {
  2.     public static void main(String[] args) {
  3.        String str = "world";
  4.         switch (str) {
  5.         case "hello":
  6.            System. out.println( "hello");
  7.             break;
  8.         case "world":
  9.            System. out.println( "world");
  10.             break;
  11.         default:
  12.             break;
  13.        }
  14.    }
  15. }

对代码进行反编译:


   
   
  1. public class switchDemoString
  2. {
  3.     public switchDemoString()
  4.    {
  5.    }
  6.     public static void main(String args[])
  7.    {
  8.        String str = "world";
  9.        String s;
  10.         switch((s = str).hashCode())
  11.        {
  12.         default:
  13.             break;
  14.         case 99162322:
  15.             if(s. equals( "hello"))
  16.                System. out.println( "hello");
  17.             break;
  18.         case 113318802:
  19.             if(s. equals( "world"))
  20.                System. out.println( "world");
  21.             break;
  22.        }
  23.    }
  24. }

看到这个代码,你知道原来字符串的switch是通过equals()hashCode()方法来实现的。记住,switch中只能使用整型,比如byteshortchar(ackii码是整型)以及int。还好hashCode()方法返回的是int,而不是long。通过这个很容易记住hashCode返回的是int这个事实。仔细看下可以发现,进行switch的实际是哈希值,然后通过使用equals方法比较进行安全检查,这个检查是必要的,因为哈希可能会发生碰撞。因此它的性能是不如使用枚举进行switch或者使用纯整数常量,但这也不是很差。因为Java编译器只增加了一个equals方法,如果你比较的是字符串字面量的话会非常快,比如”abc” ==”abc”。如果你把hashCode()方法的调用也考虑进来了,那么还会再多一次的调用开销,因为字符串一旦创建了,它就会把哈希值缓存起来。因此如果这个siwtch语句是用在一个循环里的,比如逐项处理某个值,或者游戏引擎循环地渲染屏幕,这里hashCode()方法的调用开销其实不会很大。

好,以上就是关于switch对整型、字符型、和字符串型的支持的实现方式,总结一下我们可以发现,其实swich只支持一种数据类型,那就是整型,其他数据类型都是转换成整型之后在使用switch的。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值