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



  • 相关阅读:
    js的同步和异步
    事件三
    事件2
    JS作用域题
    游戏之乐
    NSTimer(2)
    NSTimer
    理解 Objective-C Runtime
    iOS系统安全机制概览
    Which is the best of GCD, NSThread or NSOperationQueue?
  • 原文地址:https://www.cnblogs.com/a986771570/p/8542488.html
Copyright © 2011-2022 走看看