zoukankan      html  css  js  c++  java
  • java中switch能否使用String作为参数?

            System.out.println("输入数字:");
    	Scanner sc=new Scanner(System.in);
            int i=sc.nextInt();
            switch(i)
            {
            case 1:
            	System.out.println("hello world!");
            case 2:
            	System.out.println("test2"); 
            default:
                System.out.println("not found");
            }

    在jdk1.7之前,switch支持的多种数据类型 int float  double byte 以及对应的封装类型 都可以支持。

    其对应的封装类型会自动转换为int  ,所以 float  double 等数据会出现失精的情况。

    那么switch到底能不能支持String类型的数据判断呢?

    String test="test2";
    		switch(test)
    		{
    		case "test":
    			System.out.println("hello");
    			break;
    		case "test2":
    			System.out.println("hello world");
    			break;
    		}
    		

    在jdk1.7开始,测试是否支持String 结果是支持的


    运行结果正常,从jdk1.7开始,switch支持更多的数据类型,字符串,字符类型,枚举,布尔都支持!



  • 相关阅读:
    C++ 中的深入浅拷贝和深拷贝
    C++ 引用小问题
    6-10
    6-8
    6-7
    6-4
    6-3
    6-1
    5-31
    COMException 依赖服务或组无法启动(0x8007042C)处理办法
  • 原文地址:https://www.cnblogs.com/a986771570/p/8542488.html
Copyright © 2011-2022 走看看