java基礎面試題:switch語句能否作用在byte上,能否作用在long上,能否作用在String上?


package com.swift;

public class Switch_Test {

    public static void main(String[] args) {
        /*
         * switch語句能否作用在byte上,能否作用在long上,能否作用在String上?
         */
        byte zijie = 3;
        System.out.println(zijie);
        long changzheng=3;
        switch (changzheng) {  //cannot switch on a value of type long.
        case 'a':
            System.out.println("this is a .");
            break;
        case 0:
            System.out.println("this is 0 int");
            break;
        case 3:
            System.out.println("this is 0 int");
            break;
        default:
            System.out.println("this is default.");

        }
    }

}

byte short char都是隱性int類型都可以,以及他們的包裝類

long 不行

String也可以,要求case中也為String類型

package com.swift;

public class Switch_Test {

    public static void main(String[] args) {
        /*
         * switch語句能否作用在byte上,能否作用在long上,能否作用在String上?
         */
        byte zijie = 3;
        System.out.println(zijie);
        long changzheng=3;
        String str="abc";
        switch (str) {  //cannot switch on a value of type long.
        case "ab":
            System.out.println("this is a .");
            break;
        case "a":
            System.out.println("this is 0 int");
            break;
        case "abc":
            System.out.println("this is abc int");
            break;
        default:
            System.out.println("this is default.");

        }
    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM