zoukankan      html  css  js  c++  java
  • Java 14 实用新特性

    Java 14 实用新特性

    instanceof 的模式匹配(预览)

    // Old
    Object obj = new String("123");
    if(obj instanceof String) {
        String str = (String)obj;
        str.contains("123")
    } else {
        str = "456"
    }
    
    //New
    Object Obj = new String("123");
    if(obj instanceof String str) {
        str.contains("23");
    } else {
        str = "4567"
    }
    

    NullPointException 更多提示信息

    在运行的参数中加上 -XX:+ShowCodeDetailsInExceptionMessages ,会提示引起异常的具体地方和调用的地方

    Record 预览属性

    // Old
    public final class User {
    
        private final String name;
    
        public User(String name) {
            this.name = name;
        }
    
        public String getName() {
            return name;
        }
    
        @Override
        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }
    
            User user = (User) o;
    
            return getName().equals(user.getName());
        }
    
        @Override
        public int hashCode() {
            return getName().hashCode();
        }
    
        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    '}';
        }
    }
    
    // New
    public record User(String name) {
    }
    

    注意点

    public record User(String name) {
        // record 自带全参构造器、Getter、hashCode()、equals()、toString()
        // Getter的方法不带 Get,如 getName() => name()
        
        // 可以添加静态方法和变量、构造器以及实例方法
    }
    

    switch 表达式(Java 12加入,14确定)

    // Old
    String str = null;
    switch (str) {
        case "1":
        case "2":
        	System.out.println("1");break;
        case "3":
        	System.out.println("2");break;
        default:
        	System.out.println("3");
    }
    
    // New
    String str = null;
    switch (str) {
        case "1", "2" -> System.out.println("1");
        case "3" -> System.out.println("2");
        default -> System.out.println("3");
    }
    

    instanceof 结合使用

    // Old
    Object obj = null;
    String formatted = null;
    if (obj instanceof Integer i) {
        formatted = String.format("int %d", i);
    }
    else if (obj instanceof Byte b) {
        formatted = String.format("byte %d", b);
    }
    else if (obj instanceof Long l) {
        formatted = String.format("long %d", l);
    }
    else if (obj instanceof Double d) {
        formatted = String.format("double %f", d);
    } else if (obj instanceof String s) {
        formatted = String.format("String %s", s);
    }
    
    // New
    // switch 会返回值
    Object obj = null;
    String formatted =
        switch (obj) {
            case Integer i -> String.format("int %d", i); 
            case Byte b    -> String.format("byte %d", b); 
            case Long l    -> String.format("long %d", l); 
            case Double d   -> String.format("double %f", d); 
            case String s   -> String.format("String %s, s);
            default        -> String.format("Object %s", obj);
        };
    

    Java 13 中 switch 新特性

    // yield 相当于方法中的 return,返回指定数据
    String str = null;
    int num = switch(str) {
    	case "1" -> 1;
    	case "2" -> 2;
        case "3": yield 3;
    	default: {
    		System.out.println("xxx");
    		yield 4;
    	}
    };
    

    文本块(预览)

    // Old
    String str = "hello xxx\n" +
        		"world";
    
    
    // New
    // 在涉及如 html、Json数据等多转义字符时,可极大提高可读性
    // 一般情况所有文本原样不变(包括换行),"\"取消换行,"\s"表示一个空格(也可以直接输入)
    String str = """
        hello \
        xxx
        world
        """;
    
  • 相关阅读:
    关于论文绘图的一些拾遗
    在线word论文生成的方法
    用LyX写中文幻灯片
    关于KO信息
    关于中文期刊LaTeX的CCT相关
    Android NDK and OpenCV Development With Android Studio
    android上的JAVA8:使用retrolambda
    WebSocket学习笔记——无痛入门
    html5利用websocket完成的推送功能
    [Android]通过JNI访问并操作Bitmap的元素,支持RGB565和ARGB8888
  • 原文地址:https://www.cnblogs.com/posase/p/12838995.html
Copyright © 2011-2022 走看看