zoukankan      html  css  js  c++  java
  • JAVA JDK(4)—— jdk8特性

    今日一言:
    She's articulate, strong, persuasive,
    arugumentative, beautiful and she's
    my dearest, dearest friend.
      ——《五十度灰》

    JAVA JDK(4)—— jdk8特性


    参考资料:

    目录:

    • jdk5
    • jdk6
    • jdk7
    • jdk8

    JDK 8

    特性还有很多没有详细给出,我只给出我经常用到或可能会用到的。


    1. 接口的默认方法与静态方法

    public interface testInterface {

        default void printHello(){
            System.out.println("Hello World!");
        }

        static void switchMode(int mode){
            System.out.println("You switch mode " + mode );
        }

    }

    main函数

    public static void main(String[] args) {
        testInterface.switchMode(1);
        testJava8 test = new testJava8();
        test.printHello();
    }

    2. Lambda 表达式

    场景1:新建接口对象(单函数接口)

    // before
    new Thread(new Runnable() {
        @Override
        public void run() {
            out.println("Done");
        }
    }).start();

    // lambda
    new Thread(()->out.println("Done")).start();

    场景2:逐个调用List成员的某个函数

    public class testJava8 {

        public testJava8(){}

        static testJava8 create(final Supplier<testJava8> test ){
            return test.get();
        }

        void SomeOneDoSomethingfinal testJava8 test ){
            out.println("test has done");
        }

        void DoSomething(){
            out.println("done");
        }

        public static void main(String[] args) {
            testJava8 test = testJava8.create(testJava8::new);
            ArrayList<testJava8> arrayList = new ArrayList<>();
            arrayList.add(test);
            arrayList.forEach(testJava8 :: DoSomething);
            arrayList.forEach(test::SomeOneDoSomething);
        }

    }

    3. 重复注解和类型注释

    参考资料:博客园 - JAVA 注解的几大作用及使用方法详解

    1. 要用好注解,必须熟悉java 的反射机制,从上面的例子可以看出,注解的解析完全依赖于反射。
    2. 不要滥用注解。平常我们编程过程很少接触和使用注解,只有做设计,且不想让设计有过多的配置时

    4. base64加解密API

    public static void main(String[] args) {
        String base64 = Base64.getEncoder().encodeToString("test base64".getBytes());
        out.println(base64);
        String string = new String(Base64.getDecoder().decode(base64));
        out.println(string);
    }

  • 相关阅读:
    lua table的遍历
    代码动态更换MeshRenderer的材质Materials
    随手一记-单例模式写法
    unity UGUI填坑 之 HorizontalLayoutGroup 和 ContentSizeFitter配合使用
    unity 使用OnDrag实现物体旋转
    unity用json和服务器数据交互
    C语言核心技术-C语言概述与开发环境搭建
    Linux系统运维与架构设计-浅谈计算机系统
    Linux系统运维与架构设计-Unix/Linux概述
    Java从0到全栈-Java语言概述与开发环境搭建
  • 原文地址:https://www.cnblogs.com/rcklos/p/12931033.html
Copyright © 2011-2022 走看看