2017-02-13 Head First Java
1:寫一次就可以在所有地方執行 write-once/run-anywhere。
2:編譯器會產生字節碼。編譯後的字節碼與平臺無關。
3:Java虛擬機可以讀取與執行字節碼。
4:Sharpen your pencil/sharpen |ˈʃɑːpən| vt 磨快、變尖
5:源文件:.java文件。
6:類:類的內容必須包在花括號裏面。
7:每個Java程序最少都會有一個類以及一個main(),每個應用程序只有一個main()函數。
8:main()就是程序的起點。
9:不過你的程序有多大,一定都會有一個mian()來作爲程序的起點。
10:編譯器:compiler。
11:Java有3種循環結構:while循環、do-while循環、for循環。
12:循環的關鍵在於條件測試(conditional test)。在Java中,條件測試的結果是boolean值--不是true就是false。
13:我們可以用比較運算符(comparison operator)來執行簡單的boolean值測試。
14:其他程序語言可以直接用整數類型測試,我也可以向下面這麼做嗎:
int x = 1;
while (x){ }
不行,Java 中的integer 與 boolean兩種類型並不相容。我們只能用這樣的 boolean變量來測試:
boolean isHot = true;
while(isHot) { }
15:System.out.print與System.out.printIn有何區別?
printIn會在最後面插入換行。
16:在大括號結束時註釋大括號的內容:
public class BeerSong {
public static void main (String[] args) {
int beerNum = 99;
String word = “bottles”;
while (beerNum > 0) {
if (beerNum == 1) {
word = “bottle”;
}
System.out.println(beerNum + “ ” + word + “ of beer on the wall”);
System.out.println(beerNum + “ ” + word + “ of beer.”);
System.out.println(“Take one down.”);
System.out.println(“Pass it around.”);
beerNum = beerNum - 1;
if (beerNum > 0) {
System.out.println(beerNum + “ ” + word + “ of beer on the wall”);
} else {
System.out.println(“No more bottles of beer on the wall”);
}//else結束
} //while循環結束
} //main方法結束
} //class結束
17:Java的String類型數組:String[] pets = {“Fido”, “Zeus”, “Bin”};
18:數組元素提取:String s = pets[0]; // “Fido”
19:Java的隨機數:int rand1 = (int) (Math.random() * oneLength); random()返回的是介於0與1之間([0,1))的值。
20:Java的String操作:
String phrase = wordListOne[rand1] + “ “ + wordListTwo[rand2] + “ “ + wordListThree[rand3];
s = s + “ “ + “is a dog”; // “Fido is a dog”