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支持更多的数据类型,字符串,字符类型,枚举,布尔都支持!



  • 相关阅读:
    爬虫大作业
    作业
    新练习
    爬虫新练习
    最新操作
    小练习
    Hadoop综合大作业
    理解Mapreduce
    熟悉常用的HBase操作
    第三章 熟悉常用的HDFS操作
  • 原文地址:https://www.cnblogs.com/a986771570/p/8542488.html
Copyright © 2011-2022 走看看