記錄Java各種容易遺忘的段落
策略
規範
JCP: 審核Java Spec. 的委員會
JSR: Java Spec
Java EE Java SE+ API: JSP、Servlet、JavaMail、Enterprise JavaBeans(EJB) Web容器:Servlet/JSP唯一認得的HTTP伺服器
Java SE
JVM: 執行檔為.class的作業系統
JRE: 包含JVM與 核心libraries
JDK: 包含JRE與 Javac等開發工具
Java ME 消費性電子產品,嵌入式系統等移動式設備
為了獲得行為
1 2 3 4 5 6 7 8 9 10 11 12 public interface Power{ void getPowerFromTP(); } class Refrigerator implements Power{ public void getPowerFromTP(){ System.out.println("轉換..轉換....") } } Power obj = new Refrigerator(); obj.getPowerFromTP();
Use interface to connect two layers Common way to decouple the layers and allow for independent implementation of each layer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 public interface DataProvider { List<Data> getData(); } public class DatabaseDataProvider implements DataProvider { public List<Data> getData() { // implementation to fetch data from a database } } public class HigherLevelLayer { private DataProvider dataProvider; public HigherLevelLayer(DataProvider dataProvider) { this.dataProvider = dataProvider; } public void doSomethingWithData() { List<Data> data = dataProvider.getData(); // do something with the data } }
為了相同類別
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 class Vehicle { protected String brand = "Ford"; public void honk() { System.out.println("Tuut, tuut!"); } } class Car extends Vehicle { private String modelName = "Mustang"; // Car attribute public static void main(String[] args) { Car myCar = new Car(); myCar.honk(); System.out.println(myCar.brand + " " + myCar.modelName); } } > Tuut, tuut! Ford Mustang
介面與繼承一起 1 extends ContractFormController implements Serializable
封裝(Encapsulation) 包裝變數達到資訊隱藏,用private隱藏與setter, getter存取。
1 2 3 4 5 6 7 8 9 10 11 12 public class MyClass { private int myVariable; public void setMyVariable(int value) { // 可以在這裡添加額外的邏輯,例如檢查值的有效性 myVariable = value; } public int getMyVariable() { return myVariable; } }
多型/多態(Polymorphism)
重載(Overloading): 相同方法名稱,不同參數
1 2 3 4 5 6 7 8 9 public class MyClass { public int add(int a, int b) { return a + b; } public double add(double a, double b) { return a + b; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 public class Animal { public void makeSound() { System.out.println("Some generic sound"); } } public class Dog extends Animal { @Override public void makeSound() { System.out.println("Bark"); } } public class Cat extends Animal { @Override public void makeSound() { System.out.println("Meow"); } }
8 Primitive Data Types byte, short, int, long, float, double, boolean, char
== and .equals() 1 2 3 4 5 6 7 8 9 10 11 class HelloWorld { public static void main(String[] args) { String A="A"; String B="B"; String C="C"; String D= A+B+C; String E="ABC"; System.out.println(D == E); // false System.out.println(D.equals(E)); // true } }
== : Memory equal .equals(): Value equal
Boolean條件?成立:不成立 (int)reverse==reverse?(int)reverse:0
Integer int 是基本數據類型,int 變量存儲的是數值。 Integer 是引用類型,實際是一個對象,Integer 存儲的是引用對象的地址。
Array 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 // String to array char[] strArray = inputString.toCharArray(); // init int[] array =new int [2]; int[] array =new int [] {0,0}; // 實例 int [] array 與 new int [] {}給初始值 int [] array = new int [] {1, 3, 8, 9, 4, 7, 6}; System.out.println(Arrays.toString(array)); // 也可寫成 int [] array = {1, 3, 8, 9, 4, 7, 6}; // 分割 int [] arr = Arrays.copyOfRange(array, 0, 3); System.out.println(Arrays.toString(arr)); > java -cp /tmp/3QZLh3auEl Main [1, 3, 8] // 複製 nt [] arr2 = Arrays.addAll(arr); // 改成list List<Integer> list = Arrays.asList(1, 10, 2, 9, 3, 7, 4, 6, 5, 7, 7, 7); // 印出 import java.util.*; System.out.println(Arrays.toString(array)); int[] ints = new int[10]; // 陣列Array ints[0]=0; ArrayList intList = new ArrayList(10); // ArrayList intList.add(0); 1.Array的大小是固定的;ArrayList的大小是可變的。 2.Array中的元素可以是原始型別(primitive)或物件(object);3.ArrayList的元素只能是物件。 4.Array不能使用泛型(generic),ArrayList可以使用泛型。 5.Array用length屬性取得內容長度;ArrayList用size()方法取得內容長度。 6.Array用賦值符(=)儲存元素值;ArrayList用add()方法儲存元素值。
Loop 1 2 3 4 5 int[] arr = {1,2,3}; for(i=0; i<arr.length; i++){} for(i=0; i<=arr.length -1; i++){} while(arr[0]< 3 && arr[1]< 3){}
Static 固定記憶體空間
不用new也可以用。
new也只是用固定內空間。
new值會覆蓋。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 class Example{ static int data = 0; public Example() {} } class Main { public static void main(String[] argv) { System.out.println(Example.data); // 1 Example exp1 = new Example(); Example exp2 = new Example(); System.out.println(exp1.data); exp2.data = 1; System.out.println(exp2.data); // 2 3 System.out.println(exp1.data); } } > 0 0 1 1
Only for methods, implemented in native code using JNI (Java Native Interface).
Volatile means the working memory of each thread is not used, and access, reading and writing are always done from the main memory.
Synchronized good for multithread, but lower performance.
類別:無法被繼承(extends)。
函數:繼承他的子類別無法覆寫。
變數:常數無法被修改。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 class Example{ final int data = 0; public Example() {} } class Main { public static void main(String[] argv) { System.out.println(Example.data); Example exp1 = new Example(); exp1.data = 1; } } > /tmp/DC9NTE17Mc/Main.java:7: error: non-static variable data cannot be referenced from a static context System.out.println(Example.data); // final static int data = 0; ^ /tmp/DC9NTE17Mc/Main.java:9: error: cannot assign a value to final variable data exp1.data = 1; // final 無法修改。 ^ 2 errors
Finally
Finalize
不需考量多執行緒同步的時候我們使用StringBuilder來獲得最佳的效率。
StringBuffer 要考慮多執行緒的情況下使用StringBuffer來讓類別自動處理同步的問題。