首页 > 编程语言 > 详细

java基础面试题:switch语句能否作用在byte上,能否作用在long上,能否作用在String上?

时间:2018-01-21 21:15:51      阅读:203      评论:0      收藏:0      [点我收藏+]
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.");

        }
    }

}

 

java基础面试题:switch语句能否作用在byte上,能否作用在long上,能否作用在String上?

原文:https://www.cnblogs.com/qingyundian/p/8325464.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!