1、Java語法變化
1.1 java1.3-1.4
public class Java3Demo {
public static void main(String[] args) {
/**
* Java 1.2
* Collection Framework 2000
* Java Beans
* Java 1.3
* Java 1.4
* 斷言 assert 依賴編譯參數
* assert -ea = enable
* assert -da = disable
* 編程模型: fast-fail
* org.springframework.util.Assert
* 堆棧跟蹤
* 增加兩個構造器,在構造時將一異常來源記錄到堆棧中
* Exception(Throwable cause)
* Exception(String message, Throwable cause)
*/
}
}
1.2 java1.5
public class Java5Demo {
public static void main(String[] args) {
/**
* 泛型
* 有了新的語言之後就會產生變化,繼而產生新的編程模型
*/
String[] values = new String[] {"hello", "world"};
String[] values2 = of("hello", "world");
Integer[] data = of(1, 2, 3);
}
private static <T> T[] of(T... values) {
return values;
}
}
1.3 Java1.6&1.7
public class Java6Demo {
/**
* Override 是一個強約束,如果有 @Override ,編譯器會檢測;如果不加,編譯器不會檢測
*
* @return
*/
@Override
public String toString() {
return super.toString();
}
}
/**
* Indicates that a method declaration is intended to override a
* method declaration in a supertype. If a method is annotated with
* this annotation type compilers are required to generate an error
* message unless at least one of the following conditions hold:
*
* <ul><li>
* The method does override or implement a method declared in a
* supertype.
* </li><li>
* The method has a signature that is override-equivalent to that of
* any public method declared in {@linkplain Object}.
* </li></ul>
*
* @author Peter von der Ahé
* @author Joshua Bloch
* @jls 8.4.8 Inheritance, Overriding, and Hiding
* @jls 9.4.1 Inheritance and Overriding
* @jls 9.6.4.4 @Override
* @since 1.5
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}
public class Java7Demo {
public static void main(String[] args) {
/**
* 文件操作之後,要進行關閉操作
* inputStream.read();
* inputStream.close();
*
* 在1.7之前,我們通過try{} finally{} 在finally中釋放資源。
* 在finally中關閉資源存在以下問題:
* 1、自己要手動寫代碼做關閉的邏輯;
* 2、有時候還會忘記關閉一些資源;
* 3、關閉代碼的邏輯比較冗長,不應該是正常的業務邏輯需要關注的;
*
* 在1.7之後,新增try-catch的特性,不需要我們在 finally 中關閉,可以通過 try-catch 語法來自動關閉
* 對於實現AutoCloseable接口的類的實例,將其放到try後面(我們稱之爲:帶資源的try語句),在try結束的時候,會自動將這些資源關閉(調用close方法)。
* AutoCloseable since 1.7
* JDBC Connection
* InputStream
* ...
*/
try (FileInputStream inputStream = new FileInputStream("D:/test.txt")) {
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class Java7Demo2 {
public static void main(String[] args) {
try(AutoCloseableObject object = new AutoCloseableObject()) {
System.out.println("--執行中--");
} catch (Exception e) {
System.out.println("--exception--");
} finally {
System.out.println("--finally--");
}
}
public static class AutoCloseableObject implements AutoCloseable {
@Override
public void close() throws Exception {
System.out.println("--close--");
}
}
}
1.4 Java1.9&1.10
/**
* 模塊化,國內不常用
*/
module javase {
requires java.sql;
exports com.imooc.demo;
}
public class Java10Demo {
public static void main(String[] args) {
/**
* 兼容多語言(JS)
* JDK 1.6 Rihno JS on JVM
*/
var a = new ArrayList<>();
}
}
2.、Java 數據結構
3、Java 類庫提升
4、Java 編程模型
面向對象編程(OOP)、面向切面編程(AOP)、面向元信息編程(MDOP)、面向函數編程(FOP)、面向模塊編程(MOP)
4.1 面向對象編程(OOP)
封裝性
決定了數據訪問的限制
派生性
決定上下游關係
多態性
一種接口多種實現
4.2 面向切面變成(AOP)
4.3 面向元信息編程(MDOP)
4.4 面向函數編程(FOP)
5、Java編程思想
契約編程、設計模式、模式驅動
5.1 契約編程
5.2 設計模式
5.3 驅動模式