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);
    }

  • 相关阅读:
    sql except 用法,找两个表中非共同拥有的
    ‘堆’出你的洪荒之力
    原来你是个这样的JVM
    变形词
    54题
    最大对称子数组
    java 线程之间通信以及notify与notifyAll区别。
    大型网站架构系列:消息队列
    剑指offer第10题
    & 和 && 区别
  • 原文地址:https://www.cnblogs.com/rcklos/p/12931033.html
Copyright © 2011-2022 走看看