天天看點

Java:從程式設計語言到程式設計思想

《Java從程式設計語言到程式設計思想》學習筆記

一、Java語言的變化

(1)1.2

集合架構Collection Framework

Java Beans

(2)1.3

(3)1.4

assert

(4)1.5

工廠方法,傳回一個數組

package com.demo;

public class Java5Demo {
    public static void main(String[] args) {
        // String[] values = new String[]{"pig", "dog"};

        String[] values = of("pig", "dog");
    }

    public static <T> T[] of(T... values){
        return values;
    }
}
      

強制需要一個參數

package com.demo;

public class Java5Demo {
    public static void main(String[] args) {
        // String[] values = new String[]{"pig", "dog"};
        String[] values = of("pig", "dog", "cat");
    }

    public static <T> T[] of(T one, T... values){
        return values;
    }
}
      

(5)1.7

@Override 進行編譯器檢測

package com.demo;

public class Java5Demo extends Object{
    @Override
    public String toString() {
        return "Java5Demo";
    }
}
      

異常try…catch…

try {
    FileInputStream stream = new FileInputStream("name.txt");
    // 多異常精确捕獲
} catch (FileNotFoundException | RuntimeException e) {
    e.printStackTrace();
}      
// AutoCloseable 接口, 會自動關閉,不需要finaly中關閉
try (FileInputStream stream = new FileInputStream("name.txt")) {


} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}      

1.8 Lambda

1.9 子產品化

2.0 類型推斷

二、資料結構

  • 原生類型
    • boolean
    • byte
    • short
    • int
    • long
    • float
    • double
  • 對象類型
    • Object
    • String
    • Class
  • 數組類型
    • int[]
    • Object[]
  • 集合類型
    • Collection Stack Vector
    • List Set Queue Map Enumeration
    • Iterable Iterator
package com.demo;

import java.util.Arrays;

public class Demo{
    public static void main(String[] args) {
        print(Arrays.asList("a", "b", "c"));
    }

    public static void print(Iterable<?> iterable){
        for(Object object: iterable){
            System.out.println(object);
        }
    }
}
      

三、Java類庫提升

  • Java5
    • 并發架構(J.U.C)
    • 格式化Formatter
    • Java管理擴充(JXM)
    • Instrumentation
    • XML處理(DOM、SAX、XPath、XSTL)
  • Java6
    • JDBC 4.0JAXB 2.0
    • 可拔插注解處理API
    • Common Annotations
    • Java Compiler API
    • Scripting JVM
  • Java7
    • NIO2
    • Fork/Join架構
    • invokedynamic位元組碼
  • Java8
    • Stream API
    • CompletableFuture
    • Annotation on Java Types
    • Date and Time API
    • 可重複Annotations
    • JavaScript 運作時
  • Java 9
    • Reactive Streams Flow API
    • Process API Updates
    • Variable Handles
    • Method Handles
    • Spin-Wait Hints
    • Stack-Walking API
  • Java 10
    • Java-Based JIT Compiler
System.out.printf("Hello %s", "Tom");
// Hello Tom      

四、程式設計模型

  • 面向對象程式設計OOP
    • 封裝性(通路限制)
    • 派生性(上下遊關系)
    • 多态性(一種接口多種實作)
  • 面向切面程式設計AOP
    • 靜态接口
    • 動态代理
    • 位元組碼提升 ASM CGLIB Javassist BCEL
    • 攔截判斷 方法,注解,參數,異常
    • 攔截執行 前置,後置,傳回,異常
  • 面向元資訊程式設計MDOP
    • 注解 @Annotation
    • 反射 Reflection
    • 泛型 Generic
  • 面向函數程式設計FOP
    • 函數式接口 @FunctionalInterface
    • 預設方法
    • 方法引用
  • 面向子產品程式設計MOP

五、程式設計思想

  • 契約程式設計
    • 操作對象 Field字段、Method方法、Constructor構造器
  • 語義命名:子產品名、包名、類名、枚舉、字段、方法、常量
  • 通路控制:private(預設)、protected、public
  • 異常錯誤:類型(檢查和非檢查、層次(Throwable、Error、Exception)、來源(JDK 自定義 三方庫)
  • 構造器、方法參數:名稱、類型(資料結構、泛型)、順序、數量、限制
  • 方法傳回類型:類型(資料結構、泛型)、多态性(層次性)、限制(注解)
  • 設計模式
  • 面向對象設計模式
    • 構造模式
    • 結構模式
    • 行為模式
    • 并發模式
  • 面向切面設計模式
    • 判斷模式
    • 攔截模式
  • 面向中繼資料設計模式
    • 泛型接口設計
    • 注解驅動設計
    • 面向函數設計模式
      • 函數式接口設計SCFP
      • Fluent API設計
      • Reactive / Stream API設計
  • 模式驅動
    • 接口驅動
    • Java SE (GoF23模式)
    • Java EE API (Servlet/JSP/EJB)
    • Spring Core API(interface 21)
  • 配置驅動Java System Properties
  • OS 環境變量
  • 檔案配置(XML/Properties/YAML)
  • Java EE配置(JDNI/Servlet EJB)
  • 注解驅動
  • Java EE (Java Beans/JMX)
  • Java EE (Servlet 3.0/JAX-RS/Bean Validation/EJB 3.0)
  • Spring(@Component/@Service/@Respository)
    • Spring Boot(@SpringBootApplication)
    • Spring Cloud(@SpringCloudApplication)
  • 函數驅動
    • Java 8 Stream API
    • Java 9 FLow API
    • RxJava
    • Vert.x
    • Spring Boot WebFlux
    • Spring Cloud Gateway/Function
  • 子產品驅動
    • Java OSGI
  • Java 9 Module
  • Spring @Enable*
  • Spring Boot AutoConfiguration
  • Spring Boot Actuator