Java switch case

weixin_33816946發表於2018-09-14


JDK 1.8



import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

@Slf4j
public class SwitchTest {

    @Test
    public void givenByte_then() {
        Byte flag = 1;
        switchMethod(flag);
        flag = 2;
        switchMethod(flag);
        flag = 3;
        switchMethod(flag);
    }

    /**
     * 如果switch表示式是Byte型別,
     * case後面的數字也會自動轉型為Byte型別
     *
     * @param flag
     */
    private void switchMethod(Byte flag) {
        switch (flag) {
            case 1:
                log.info("is 1");
                break;
            case 2:
                log.info("is 2");
                break;
            default:
                log.info("not match. value:" + flag);
        }
        log.info("success:" + flag);
    }


}

 

相關文章