2021-01-08

记录一下不知道的一些新特性

 

public class Main {

    public static void main(String[] args) throws IOException, InterruptedException {

        //jdk15

        //文本块
        String html = """
              <html>
                  <body>
                      <p>Hello, world</p>
                  </body>
              </html>
              """;
        System.err.println(html);

        //instanceof模式匹配
        Object obj = "11111111";
        if(obj instanceof String str){
            System.out.println(str);
        }

        //Switch表达式
        int day = 1;
        switch (day) {
            case 1 -> System.out.println(1);
            case 2 -> System.out.println(2);
            case 3 -> System.out.println(3);
            case 4 -> System.out.println(4);
        }

        //记录类型(Record Type)
        Point point = new Point(1,3);
        System.out.println(point.x());
        System.out.println(point.y());

        //jdk11

        //本地变量类型推断(Local Var)
        Arrays.asList("Java", "Python", "Ruby")
                .forEach((var s) -> {
                    System.out.println("Hello, " + s);
                });

        //字符串加强
        /**
         * // 判断字符串是否为空白
         * " ".isBlank(); // true
         * // 去除首尾空格
         * " Javastack ".strip(); // "Javastack"
         * // 去除尾部空格
         * " Javastack ".stripTrailing(); // " Javastack"
         * // 去除首部空格
         * " Javastack ".stripLeading(); // "Javastack "
         * // 复制字符串
         * "Java".repeat(3);// "JavaJavaJava"
         * // 行数统计
         * "A\nB\nC".lines().count(); // 3
         */

        //创建不可变的集合 (List/ Set/ Map)都添加了 of 和 copyOf 方法
        var list = List.of("Java", "Python", "C");
        var copy = List.copyOf(list);
        System.out.println(list == copy); // true

        //Stream 加强
        //...

        //HTTP
        var request = HttpRequest.newBuilder()
                .uri(URI.create("https://www.baidu.com"))
                .GET()
                .build();
        var client = HttpClient.newHttpClient();
        // 同步
        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
        System.out.println(response.body());
        // 异步
        client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
                .thenApply(HttpResponse::body)
                .thenAccept(System.out::println);

        //直接把String写入文件,或者把整个文件读出为一个String
        Files.writeString(
                Path.of("./", "tmp.txt"), // 路径
                "hello, jdk11 files api", // 内容
                StandardCharsets.UTF_8); // 编码
        String s = Files.readString(
                Paths.get("./tmp.txt"), // 路径
                StandardCharsets.UTF_8); // 编码
        System.out.println(s);

    }

}

record Point(Integer x,Integer y) {

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值