-
第一堂課
-
Course Objectives
-
The different types of programming
- 機械語言(Machine languages)
- 組合語言(Assembly languages)
- 高階語言(High-level languages)
-
What is java?
- Language Feather
- Java SE 5+ Feather
-
Resource
- Java White Paper
- The Java Tutorials
-
Before You Begin
- Download and Install Java JDK
- Setting the PATH Variable
- Java SE架構圖
-
Java environment
-
jdk
- Verify in console
- javac
- java
-
整合開發環境(IDE)
- Netbeans
- Eclipse
-
編輯器(Editor)
- Notepad++
- Vim
-
Java Application
- package
-
import
- default import: java.lang.*
- import static
- class
-
comment
- 寫註解有助於別人能夠迅速了解你的原始碼
- 單行註解(//)
- 分支主題
-
輸出(Output)
-
err.print
- java className >> log.txt
- 不會進入到檔案中 會在終端機輸出
- using in ExceptionHandling
- out.print
-
out.println
- java.util.regex
-
out.printf
- (String) %s
- (char) %c
- (int.short.long.byte) %d
- (boolean) %b
- (double.float) %f
- %n
- %tD %tF %tc
- java.util.Formatter
- javax.swing.JOpitionPane.showMessageDialog(args...)
-
輸入(Input)
-
new Scanner(System.in)
- next()
- nextLine()
- nextInt()
- nextDouble()
- ...
- javax.swing.JOpitionPane.showInputDialog(args...)
-
第二堂課
-
結構(Structure)
- 依照流程控制的特性能夠讓程式更多樣化
- 直述式(Squence)
- 選擇式(Selection)
- 重複式(Repetition)
-
流程控制(flow-control)
-
選擇式敘述(Selection statements)
-
if-else
- if-else
- if-else if-else
- ? :
- argument can be a interval or a value
-
switch
- case
- default
- argument only access a value
-
重複式敘述(Repetition statements)
-
for
- fori
- fore
-
while
- while
- do-while
-
label
- 只能接於迴圈前面
- 單行
- 多行{}
-
break
- 離開迴圈
-
continue
- 離開本次迴圈
-
運算子(Operators)
-
算數運算子(Arithmetic Operators)
- 加(+)
- 減(-)
- 乘(*)
- 除(/)
- 餘(%)
-
算數轉換(Arithmrtic conversion)
- 隱式型態轉換(Implicit type conversion)
- 顯式型態轉換(Explicit type conversion)
-
比較運算子(Comparsion operator)
- 小於(<)
- 不大於(<=)
- 大於(>)
- 不小於(>=)
- 等於(==)
- 不等於(!=)
-
條件運算子(Conditional operator)
- [條件式] ? [成立傳回值] : [失敗傳回值]
-
Increment and Decrement Operators
- 遞增(++)
- 遞減(--)
- i++, ++i
-
Logical Operators
- 且(&&)
- 或(||)
- 反相(!)
- &
- |
-
^
- Different: true
- Same: false
-
第三堂課
-
攥寫規則
- 類別首字大寫
- 方法首字小寫
- 變數首字小寫
- 若有兩個字以上相接 第二個字字首大寫
-
修飾字
-
存取型態(access_spedifiers)
- 公開(public)
- 保護(protected)
- 匿名(package)
- 私有(private)
-
限定型態(qualifiers)
-
靜態(static)
- 類別層級(static)
- ClassName.member
- ex: Math class
- import static
- 實體層級(non-static)
- objectName.member
-
最終(final)
- class: 不能被繼承(Extends)
- method: 不能被覆寫(Override)
- attribute: 不能被改變 換句話說就是常數(Constant)
- 通常大寫命名
- 抽象(abstract)
-
類別成員(class members)
-
屬性 (attritube)
-
基本型別(PrimitiveType)
- 基本運算如加減乘除 均已被定義 若是外覆類別 則無法廣泛定義
- boolean
- initial value: false
- byte
- initial value: 0
- -128 ~ 127
- 1 byte
- short
- initial value: 0
- -32768 ~ 32767
- 2 bytes
- int
- initial value: 0
- -21e ~ +21e
- 4 bytes
- long
- initial value: 0
- 8 bytes
- + l
- char
- initial value: ''
- 2 bytes
- float
- initial value: 0.0
- 4 bytes
- + f
- double
- initial value: 0.0
- 8 bytes
-
參考型別(ReferenceType)
- String
- String str = new String("Hello")
- String str = "Hello"
- str.length()
- Array[]
- int[] a = new int[]{1,2,3}
- 分支主題
- array.length
- 外覆類別(Wrapper Class)
- 自動裝箱(AutoBoxing) and 自動拆箱(UnBoxing)
- Scanner
- Date
- Thread
- ...
-
宣告(Declaration)
- 資料型別(DataType)
- 基本型別(Primitive)
- 參考型別(Reference)
- attritube_name
- [ $, _ ] 可做變數名稱
-
建構(Create)
- keyword: new
-
初始化(initalization)
- initial value
- constructor
-
方法(method)
-
回傳值(return_type)
- non-void
- 基本型別(Primitive)
- 參考型別(Reference)
- keyword: return
- void
- 方法名稱(method_name)
-
方法參數(argument_list)
- 多載(Overload)
- (dataType... args)
- 可變長度的參數 (VariableArguments)
- 當有兩個參數以上時 可變的參數必須擺後面
-
方法區塊(block)
- //implement content
- 覆寫(Override)
-
建構子(Constructor)
- 不傳回值與類別同名
- 建構物件用
-
UML類別圖
- +(public)
- #(package)
- ~(protected)
- -(private)
-
第四堂課
-
抽象(abstract)
- not implemented rule
- cann't use on attritube
- can not be instantiated
- abstract -><- final
-
封裝(Encupsulation)
- Data hiding
- Getter and Setter
- public method
- boolean isXXX()
-
繼承(inheritance)
- reusable, not create again
- keyword: extends
- 單一繼承(single-inheritance)
-
super and sub
- all members be iheritantiated
-
super and this
- always on top
- 分支主題
- super -><- this
-
Access
-
protected
- sub can access
-
private
- only self
-
多型(polymorphism)
- call all subclass's method
- it can do it
- Basic: SuperType sup = new SubType();
- Advanced: SuperType[] sup = {subObject ... };
-
介面(interface)
- 突破單一繼承的限制
- keyword: implements
- always be abstract
- new Interface(){
//implements content
}
- [attributes] public static final
- [methods] public abstract
-
第五堂課
-
例外處理(Exception Handling)
- 當程式執行中 拋出exception 我一定只能讓程式中斷掉嗎?
- we need exception handling such that the program can be continued
-
syntax
- keyword: try, catch, finally, throws, throw
- in try block, if exception occurs, the code after the break point will not run.
-
try
- 找出炸彈在哪裡
- 分支主題
- ArrayIndexOutOfBoundsException
- ClassCastException
- ClassNotFoundException
- InputMismatchException
- NullPoniterException
- NumberFormatException
-
RuntimeException
- 當輸入的參數在不可接受的範圍時
- ex: JOptionPane.showConfirmDialog() massageType = 1000
- ...
- 如果是在方法內 建議使用throws 使得呼叫此方法的時候 強迫使用例外處理
-
catch
- have order from sub to super
- System.exit(int i);
-
finally
- must be run
- try-catch, try-finally, try-catch-finally
-
斷言(assert)
- 預設不被啟用 需加上 -ea 參數來執行(java -ea .class)
- 如果不啟用 含有assert的行將不被執行
- assert (condition) : statement ;
-
學習資源
- 良葛格的學習筆記
-
第六堂課
-
java.io
-
File
- canRead()
- canWrite()
- exists()
- isFile()
- isDirectory()
- isAbsolutePath()
- getName()
- getPath()
- getParent()
- length()
-
lastModified()
- 試著將傳回的long值 轉為Date 並印出
- API
-
output: java.util.Formatter
- Formmater output = new Formatter(new File("fileName"))
- output.format("%s %d %tF...", args...)
- output.close()
-
input : java.util.Scanner
- Scanner input = new Scanner(new File("fileName"))
- while(input.hasNext()){}
- input.nextLine()
- input.close()
-
序列化(Serialization)
- 有沒有辦法能儲存物件當前的狀態且不讓別人取得物件資訊
- implements Serializable
- ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("fileName.ser"))
- output.writeObject(Object serialObj)
- output.close()
- ObjectInputStream input = new ObjectInputStream(new FileInputStream("fileName.ser"))
- OriginalType T = (OriginalType)input.readObject()
- input.close()
-
java.nio
- Andriod
-
擅用IDE 徹底加快你的開發速度 利用自動完成 自行設定
-
Eclipse
- Java -> Editor -> Template
-
Netbeans
- Java -> Editor -> Code Template
-
JM
- JOptionPane.showMessageDialog(null, "Message", "Title", JOptionPane.INFORMATION_MESSAGE);
-
JC
- JOptionPane.showConfirmDialog(null, "Message", "Title", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE);
- JO
-
In
- Integer
-
Do
- Double
-
Cal
- Calendar
-
pm
- public void methodName(Object obj){return;}
-
newfile
- File file = new File("filePath")
-
newscan
- Scanner scan = new Scanner(System.in);
-
newsf
- Scanner input = new Scanner(new File("fileName"))
-
newf
- Formatter output = new Formatter(new File("fileName"))
-
第七堂課
-
泛型(Generics)
- 即使方法可以多載(Overload) 攥寫上還是很不方便
-
Object
- 如果不知原型就會造成CastClassException
- 有時候不相容於基本型別
- 泛型(廣泛接受所有的型別)提供了模版(Template)的概念
- 印出任何陣列型別的值
-
syntax
- class GenericsDemo<T>{}
- <T> void generic(T... ){}
-
內容(Content)
- E : 元素(Element)
- T : 模版(Template)
- super : 上層
- extends : 下層
- implements : 實作介面
- ? : 常與上述3個關鍵字 相搭
- 其內容不限填上述的字 只是其字可代表其意
-
集合(Collection)
-
陣列的特性
- 長度不可變
- 元素同型別
-
集合特性
-
Set : 不能包含重複
- HashSet
- TreeSet : order
-
List : 可包含重複
- ArrayList
- LinkedList
- Vector
-
Queue : first-in first-out
- PriorityQueue : order
- offer()
- peek()
- poll()
-
Map : Key-Value
- Hashtable
- HashMap
- TreeMap
- Interface: Collection & Class: Collections -> API
- java.util.Arrays
- java.util.Properties
-
第八堂課
-
執行緒(Thread)
- 我的程式只能夠單工處理嗎?
- 有了執行緒 你可以同時執行兩個以上的程式
-
Restruction
- Topic
-
syntax
- Override run() methods
-
Call start() to start thread
- 為什麼不直接call run?
- 當你呼叫start()其實間接呼叫到run() 其差別在於start只能啟動一次
- extends Thread
- implements Runnable
-
SCJP
-
java.lang.*
- Object
-
String
- concat()
- split()
- valueOf()
-
StringBuffer
- 常用於多執行緒(mulit-Thread)
- append()
- insert()
-
StringBuilder
- 常用於單執行緒(single-Thread)
- append()
- insert()
-
外覆類別(Wrapper class)
- parseXXX()
- valueOf()
- Thread
-
java.util.*
-
Arrays
- binarySearch()
- Scanner
- Formatter
- Date
- Calendar
- GergorianCalendar
-
Collection
-
Collections
- sort()
-
Set
- HashSet
- TreeSet
-
List
- ArrayList
-
Queue
- PriorityQueue
-
Map
-
HashMap
- put()
- Locale
-
java.io.*
- File
- FileReader
- FileWriter
- BufferReader
- BufferWriter
- FileInputStream
- FileOutputStream
- ObjectInputStream
- ObjectOutputStream
- Console
-
java.text.*
- NumberFormat
- DataFormat
- Array[]
- ...