01 switch 语句

1 switch 语句与整数类型

switch 语句支持 char 以及 byteshortint 等整数类型(包括其所对应的包装类型),但是并不支持 long 类型以及floatdouble 等浮点类型。其根本原因在于 switch 语句的底层 lookupswitchtableswitch 只支持 int 类型(charbyteshort 可以隐式的转换成 int),longfloatdouble 等类型在转换成 int 类型时可能会丢失精度。

package com.hcong.control;

/**
 * @Classname testSwitch
 * @Date 2023/3/27 18:22
 * @Created by HCong
 */
public class testSwitch {
    public static void main(String[] args) {
        testByte((byte) 0);
        testShort((short) -32769);
        testInt(0);
    }

    /**
     * 表达式的类型为:byte
     *
     * @param number
     */
    public static void testByte(byte number) {
        switch (number) {
            case -128:
                System.out.println("byte -128");
                break;
            case 0:
                System.out.println("byte 0");
                break;
            default:
                System.out.println("byte 127");
        }
    }

    /**
     * 表达式的类型为:short
     *
     * @param number
     */
    public static void testShort(short number) {
        switch (number) {
            case -32768:
                System.out.println("short -32768");
                break;
            case 0:
                System.out.println("short 0");
                break;
            default:
                System.out.println("short 32767");
        }
    }

    /**
     * 表达式的类型为:int
     *
     * @param number
     */
    public static void testInt(int number) {
        switch (number) {
            case Integer.MIN_VALUE:
                System.out.println("int " + Integer.MIN_VALUE);
                break;
            case 0:
                System.out.println("int 0");
                break;
            default:
                System.out.println("int " + Integer.MAX_VALUE);
        }
    }
}

2 switch 语句与字符串类型

Java 7 之后 switch 语句可以使用 String 类型,其原因在于不同字符串所生成的 hashcode 并不相同,而 hashcode 的取值范围与 int 类型的取值范围保持一致。

package com.hcong.control;

/**
 * @Classname testSwitch
 * @Date 2023/3/27 18:22
 * @Created by HCong
 */
public class testSwitch {
    public static void main(String[] args) {
        testString("start");
    }
    
    /**
     * 表达式的类型为:string
     *
     * @param str
     */
    public static void testString(String str) {
        // 比较字符串的 hashcode
        switch (str) {
            case "start":
                System.out.println("String start");
                break;
            case "Hello World":
                System.out.println("String Hello World");
                break;
            default:
                System.out.println("String end");
        }
    }
}

3 switch 语句与枚举类型

枚举类型建立了常量值与名字的关联,并且枚举类型的取值为整数,故而可以在 switch 中使用。

package com.hcong.control;

/**
 * @Classname testSwitch
 * @Date 2023/3/27 18:22
 * @Created by HCong
 */
public class testSwitch {
    public static void main(String[] args) {
        testEnum(enumDouble.NUM_ONE);
    }

    /**
     * 表达式的类型为:enum
     *
     * @param enu
     */
    public static void testEnum(enumDouble enu) {
        switch (enu) {
            case NUM_ONE:
                System.out.println("NUM_ONE");
                break;
            case NUM_TWO:
                System.out.println("NUM_TWO");
                break;
            case NUM_THREE:
                System.out.println("NUM_THREE");
                break;
            default:
                System.out.println("NUM_FOUR");
        }
    }
}

enum enumDouble {
    NUM_ONE,
    NUM_TWO,
    NUM_THREE,
    NUM_FOUR
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是聪聪黄吖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值