Java中switch对整型/字符型/字符串型具体实现细节

本文详细解析了Java7中switch语句支持的数据类型:byte、short、int、char及String,并通过实例代码展示了这些类型如何被转换为整型进行比较。

转自:http://www.hollischuang.com/archives/61

Java7中switch中支持的数据类型有: byte short int char String类型

其实switch是只支持一种数据类型,那就是整型:

1. byte short int  本身就是整型,可以直接按照整型进行比较

2. char的字符会依照ascii表,转换为对应的整型,然后进行switch条件选择

3. String的字符串会  先将字符串转换为hash值,   然后再对字符串进行equals方法比较,   以此来完成switch的条件选择

因为有ascii表存在,所以char类型和整数型可以进行转换, 也就是char类型可以直接赋值为整数型,整数型也可以直接赋值为字符型(无法转换的情况还没去归纳)

都说代码知识都是敲出来的,于是每种情况我都对比了下,如下图:

 

字节类型代码

这是字节类型的java源码,因为有字码表的存在,所以整数类型的变量也可以直接赋值字符

package compiler.demobyte;

public class Demo01 {

    public static void main(String[] args) {
        byte byt1 = 'a';

        switch (byt1) {
            case 'a':
                System.out.println("hello");
                break;
            case 'b':
                System.out.println("world");
                break;
            default:
                break;
        }
        byte byt2 = 97;
        switch (byt2) {
            case 97:
                System.out.println("hello");
                break;
            case 98:
                System.out.println("world");
                break;
            default:
                break;
        }
    }
}

这是.class文件

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package compiler.demobyte;

public class Demo01 {
    public Demo01() {
    }

    public static void main(String[] args) {
        byte byt1 = 97;
        switch(byt1) {
        case 97:
            System.out.println("hello");
            break;
        case 98:
            System.out.println("world");
        }

        byte byt2 = 97;
        switch(byt2) {
        case 97:
            System.out.println("hello");
            break;
        case 98:
            System.out.println("world");
        }

    }
}

 

short类型代码

short类型与byte类型是一样的

package compiler.demoshort;

public class Demo01 {

    public static void main(String[] args) {
        short sht = 'a';
        switch (sht) {
            case 'a':
                System.out.println("hello");
                break;
            case 'b':
                System.out.println("world");
                break;
            default:
                break;
        }

        short sht2 = 97;
        switch (sht2) {
            case 97:
                System.out.println("hello");
                break;
            case 98:
                System.out.println("world");
                break;
            default:
                break;
        }
    }
}

这是.clas文件

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package compiler.demoshort;

public class Demo01 {
    public Demo01() {
    }

    public static void main(String[] args) {
        short sht = 97;
        switch(sht) {
        case 97:
            System.out.println("hello");
            break;
        case 98:
            System.out.println("world");
        }

        short sht2 = 97;
        switch(sht2) {
        case 97:
            System.out.println("hello");
            break;
        case 98:
            System.out.println("world");
        }

    }
}

 

int类型也是一样的,没有进行测试

 

char类型代码

package compiler.demochar;

public class Demo01 {

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

        char ch2 = 97;
        switch (ch2) {
            case 97:
                System.out.println("hello");
                break;
            case 98:
                System.out.println("world");
                break;
            default:
                break;
        }

    }
}

生成.class文件

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package compiler.demochar;

public class Demo01 {
    public Demo01() {
    }

    public static void main(String[] args) {
        char ch = 97;
        switch(ch) {
        case 97:
            System.out.println("hello");
            break;
        case 98:
            System.out.println("world");
        }

        char ch2 = 97;
        switch(ch2) {
        case 97:
            System.out.println("hello");
            break;
        case 98:
            System.out.println("world");
        }

    }
}

 

String 类型代码

String会稍微特殊一些,分两步进行转换:

第一步: 先将字符串转换为哈希值,进行条件选择.  然后因为存在哈希值碰撞的问题, 所以在条件选择成功后, 又使用了equals方法进行内容的比较.

哈希值的表现形式是 int整型

所以实质上: String类型也是视为是int整型的比较.

java代码如下:

package compiler.demostring;

public class Demo01 {

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

.clas文件

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package compiler.demostring;

public class Demo01 {
    public Demo01() {
    }

    public static void main(String[] args) {
        String str = "wor";
        byte var3 = -1;
        switch(str.hashCode()) {
        case 99162322:
            if (str.equals("hello")) {
                var3 = 0;
            }
            break;
        case 113318802:
            if (str.equals("world")) {
                var3 = 1;
            }
        }

        switch(var3) {
        case 0:
            System.out.println("hello");
            break;
        case 1:
            System.out.println("world");
        }

    }
}

 

 

综上所述:  又回归到我们的结论:    其实switch是只支持一种数据类型,那就是整型.

 

转载于:https://www.cnblogs.com/Yiran-shampin/p/9473780.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值