Java Switch對各類型支持實(shí)現(xiàn)原理
switch
Java7開始,switch的參數(shù)可以是String類型了,這真的是一個(gè)很有用的改進(jìn),畢竟string還是挺常用的。到目前為止,switch支持的參數(shù)類型有:byte、short、int、char、String、enum。switch對各種類型參數(shù)的支持到底是怎么實(shí)現(xiàn)的呢?
byte、short、int
public class Main2 {
public static void main(String[] args) {
byte b = 1;
switch (b) {
case 0:
System.out.println("0");
break;
case 1:
System.out.println("1");
break;
default:
System.out.println("other");
}
}
}
反編譯:
public class Main2 {
public Main2() {
}
public static void main(String[] args) {
byte b = 1;
switch(b) {
case 0:
System.out.println("0");
break;
case 1:
System.out.println("1");
break;
default:
System.out.println("other");
}
}
}
將byte換成short、int,編譯后的結(jié)果與上面展示的基本相同。
char
public class Main2 {
public static void main(String[] args) {
char c = '0';
switch (c) {
case '0':
System.out.println("0");
break;
case '1':
System.out.println("1");
break;
default:
System.out.println("other");
}
}
}
反編譯:
public class Main2 {
public Main2() {
}
public static void main(String[] args) {
char c = 48;
switch(c) {
case 48:
System.out.println("0");
break;
case 49:
System.out.println("1");
break;
default:
System.out.println("other");
}
}
}
從反編譯的結(jié)果看,對char類型進(jìn)行比較的時(shí)候,實(shí)際比較的是字符對應(yīng)的ASCⅡ碼,編譯器把switch中char類型的變量轉(zhuǎn)換成int類型的變量。
String
public class Main2 {
public static void main(String[] args) {
String s = "0";
switch (s) {
case "0":
System.out.println("0");
break;
case "1":
System.out.println("1");
break;
default:
System.out.println("other");
}
}
}
反編譯:
public class Main2 {
public Main2() {
}
public static void main(String[] args) {
String s = "0";
byte var3 = -1;
switch(s.hashCode()) {
case 48:
if (s.equals("0")) {
var3 = 0;
}
break;
case 49:
if (s.equals("1")) {
var3 = 1;
}
}
switch(var3) {
case 0:
System.out.println("0");
break;
case 1:
System.out.println("1");
break;
default:
System.out.println("other");
}
}
}
從反編譯的結(jié)果看,switch借助hashCode()和equals()實(shí)現(xiàn)了對String的支持——先使用hashCode進(jìn)行初步判斷,然后使用equal()進(jìn)行二次校驗(yàn)(由于哈希沖突的存在,這個(gè)二次校驗(yàn)是必要的)。
public enum ColorEnum {
RED, YELLOW, BLUE, GREED
}
public class Main {
public static void main(String[] args) {
ColorEnum color = ColorEnum.YELLOW;
switch (color) {
case RED:
System.out.println("red");
break;
case YELLOW:
System.out.println("yellow");
break;
default:
System.out.println("other");
}
}
}
反編譯:
/*
* Decompiled with CFR 0.149.
*/
package com.learn.java;
public final class ColorEnum
extends Enum<ColorEnum> {
public static final /* enum */ ColorEnum RED = new ColorEnum("RED", 0);
public static final /* enum */ ColorEnum YELLOW = new ColorEnum("YELLOW", 1);
public static final /* enum */ ColorEnum BLUE = new ColorEnum("BLUE", 2);
public static final /* enum */ ColorEnum GREED = new ColorEnum("GREED", 3);
private static final /* synthetic */ ColorEnum[] $VALUES;
public static ColorEnum[] values() {
return (ColorEnum[])$VALUES.clone();
}
public static ColorEnum valueOf(String string) {
return Enum.valueOf(ColorEnum.class, string);
}
private ColorEnum(String string, int n) {
super(string, n);
}
static {
$VALUES = new ColorEnum[]{RED, YELLOW, BLUE, GREED};
}
}
/*
* Decompiled with CFR 0.149.
*/
package com.learn.java;
import com.learn.java.ColorEnum;
public class Main {
public static void main(String[] arrstring) {
ColorEnum colorEnum = ColorEnum.YELLOW;
switch (1.$SwitchMap$com$learn$java$ColorEnum[colorEnum.ordinal()]) {
case 1: {
System.out.println("red");
break;
}
case 2: {
System.out.println("yellow");
break;
}
default: {
System.out.println("other");
}
}
}
}
上面的反編譯結(jié)果表明,switch借助枚舉類的序號實(shí)現(xiàn)對枚舉類的支持。
總結(jié)
其實(shí)switch只支持整型,其他數(shù)據(jù)類型都是轉(zhuǎn)換成整型后才使用的switch。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java詳解HashMap實(shí)現(xiàn)原理和源碼分析
這篇文章主要介紹了Java關(guān)于HashMap的實(shí)現(xiàn)原理并進(jìn)行源碼分析,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09
一文詳解Java17中LinkedList類的用法和應(yīng)用場景
LinkedList 是 Java 集合框架中基于雙向鏈表實(shí)現(xiàn)的類,實(shí)現(xiàn)了 List 和 Deque 接口,本文將為大家介紹一下它在Java 17 中如何更高效的使用吧2025-03-03
Spring MVC實(shí)現(xiàn)的登錄攔截器代碼分享
這篇文章主要介紹了Spring MVC實(shí)現(xiàn)的登錄攔截器代碼分享,涉及攔截器的簡單介紹,攔截器和過濾器的區(qū)以及攔截器實(shí)現(xiàn)代碼等相關(guān)內(nèi)容,這里分享給大家,供需要的朋友參考。2017-10-10

