java switch用法

Java 7中,switch的参数能够是String类型了,这对咱们来讲是一个很方便的改进。到目前为止switch支持这样几种数据类型:byte short int char String 。switch对String的支持是使用equals()方法和hashcode()方法。缓存

1、switch对整型支持的实现安全

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

public class switchDemoInt {
    public static void main(String[] args) {
        int a = 5;
        switch (a) {
        case 1:
            System.out.println(1);
            break;
        case 5:
            System.out.println(5);
            break;
        default:
            break;
        }
    }
}
//output 5

反编译后的代码以下:this

public class switchDemoInt
{
    public switchDemoInt()
    {
    }
    public static void main(String args[])
    {
        int a = 5;
        switch(a)
        {
        case 1: // '\001'
            System.out.println(1);
            break;

        case 5: // '\005'
            System.out.println(5);
            break;
        }
    }
}

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

2、switch对字符型支持的实现游戏

直接上代码:ci

public class switchDemoInt {
    public static void main(String[] args) {
        char a = 'b';
        switch (a) {
        case 'a':
            System.out.println('a');
            break;
        case 'b':
            System.out.println('b');
            break;
        default:
            break;
        }
    }
}

编译后的代码以下:字符串

public class switchDemoChar
{
    public switchDemoChar()
    {
    }
    public static void main(String args[])
    {
        char a = 'b';
        switch(a)
        {
        case 97: // 'a'
            System.out.println('a');
            break;
        case 98: // 'b'
            System.out.println('b');
            break;
        }
  }
}

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

3、switch对字符串支持的实现编译器

仍是先上代码:

public class switchDemoString {
    public static void main(String[] args) {
        String str = "world";
        switch (str) {
        case "hello":
            System.out.println("hello");
            break;
        case "world":
            System.out.println("world");
            break;
        default:
            break;
        }
    }
}

对代码进行反编译:

public class switchDemoString
{
    public switchDemoString()
    {
    }
    public static void main(String args[])
    {
        String str = "world";
        String s;
        switch((s = str).hashCode())
        {
        default:
            break;
        case 99162322:
            if(s.equals("hello"))
                System.out.println("hello");
            break;
        case 113318802:
            if(s.equals("world"))
                System.out.println("world");
            break;
        }
    }
}

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

这就是Java 7如何实现的字符串switch。它使用了hashCode()来进行switch,而后经过equals方法进行验证。这其实只是一个语法糖,而不是什么内建的本地功能。选择权在你,我我的来讲不是很喜欢在switch语句中使用字符串,由于它使得代码更脆弱,容易出现大小写敏感的问题,并且编译器又没有作输入校验 。

接下来咱们再看下enum的用法:

enum Animal {    
    DOG,CAT,BEAR;  
     public static Animal getAnimal(String animal){    
       return valueOf(animal );    
    }     
}  
  
public class Client {    
    
   public void caseAnimal(String animal){    
       switch(Animal.getAnimal(animal)){    
       case CAT:    
           System.out.println("this is a cat");    
           break;    
       case DOG:    
           System.out.println("this is a dog");    
           break;    
       case BEAR:    
           System.out.println("this is a bear");    
           break;    
       }    
   }  
   
}

这是一种比较推荐的用法。

相关文章
相关标签/搜索