zoukankan      html  css  js  c++  java
  • java7 语法糖 之 switch 声明string

      Jdk7新switch 恒语句可以string种类。

    例如:

    @Test
    	public void test_1(){
    		String string = "hello";
    		switch (string) {
    		case "hello":
    			System.out.println(string);
    			break;
    
    		default:
    			throw new IllegalArgumentException("非法參数");
    		}
    	}

      语法糖的背后,其有用的对待string 类型时候,用的是hashCode() 方法转换的.

    所以string 类型不能为 NULL.

    比如:

    @Test
    	public void test_3(){
    		String string = null;
    		expectedException.expect(NullPointerException.class);
    		switch (string) {
    		case "hello":
    			System.out.println(string);
    			break;
    
    		default:
    			throw new IllegalArgumentException("非法參数");
    		}
    	}

    会抛出异常
    NullPointerException

    版权声明:本文博主原创文章,博客,未经同意不得转载[http://blog.csdn.net/doctor_who2004]。

  • 相关阅读:
    Redis涉及的概念
    Redis高级教程
    Redis基础入门
    Java多线程面试题
    Java集合面试题
    Java集合基础
    Java基础面试题总结
    Zookeeper Basics
    GitLab基础入门
    阿里云ECS服务器Docker安装Tomcat过程记录
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/4887218.html
Copyright © 2011-2022 走看看