49 Keywords : abstract assert boolean break byte case catch char class const continue default do double else extends final finally float for goto if implements import instanceof int interface long native new package private protected public return short static strictfp super switch synchronized this throw throws transient try void volatile while
All keywords start with a lowercase letter.
All six number types in Java are signed, so the can be positive or negative. (byte, short, int, long, float, double)
ค่าที่เป็นไปได้ของ char คือ 65, 041 เป็นต้น class X{ public static void main(String[] bc) { char a = 65; char b = 0101; char c = '\u0041'; char d = 0x0041; char e = 'A'; System.out.println(""+a+b+c+d+e); }}
แบบของตัวเลขโดยปริยาย(Default number) ถ้าเป็นจำนวนเต็มคือ int และมีทศนิยมคือ double ให้ระวังเรื่องขนาด wrong : f = 1.5; right : f = 1.5f; right : f = 1.5F;
เมื่อ b เป็น byte โดย b = 1 + 1 จะไม่มีปัญหา แต่ b = 100 + 100; จะ compile error เรื่อง loss of precision wrong : b = 100 + 100; wrong : b = (byte)100 + 100; right : b = (byte)(100 + 100);
การประกาศอาร์เรย์ ต้องจองมิติแรกไว้ก่อน wrong : int[] a[] = new int[][]; right : int[] a[] = new int[2][]; right : int a[][] = new int[2][]; right : int[][] a = new int[2][2]; right : int[][] a = new int[0][]; (ไม่พบปัญหา แต่คงพบตอน runtime)
NaN (Not a Number) เช่น square root ของค่าติดลบ และ NaN จะไม่เท่ากับอะไรเลย แม้แต่ตัวเอง
NaN is not equal to anything, not even itself.
float ถูกหารด้วย 0 ได้ และค่าจะเป็น Infinity
การสร้าง object แบบ wrapper Right: Boolean b = new Boolean(true); Right: Boolean b = new Boolean("true"); Right: boolean b = new Boolean(true).booleanValue();
บทที่ 7 : Objects and Collections
.equals ใช้เปรียบเทียบทั้งประเภท และ value ต้อง ok ทั้ง 2 ตัว
== ใช้เปรียบเทียบ เฉพาะ value ต่างประเภทกันก็เท่ากันได้ เช่น long และ int
set ซ้ำไม่ได้ และไม่เรียง ใช้ Hashset()
list ซ้ำได้ และมี index ใช้ LinkedList()
map ใช้ key และ key ซ้ำไม่ได้
List : ArrayList Vector LinkedList
Map : HashMap Hashtable LinkedHashMap TreeMap
Set : HashSet LinkedHashSet TreeSet
TreeSet เรียงตามอักษร และไม่ซ้ำ
Vector, HashTable เป็น synchronized methods
ordered คือ เรียงตามการเข้า-ออก
sorted คือ เรียงตามอักษร
เรื่องความเท่ากัน ถ้า equals เท่ากัน hashCode ย่อมเท่ากัน Integer x = new Integer(5); Integer y = x; Integer z = new Integer(5); System.out.println(x.equals(y)); // true System.out.println(x==y); // true System.out.println(x.equals(z)); // true System.out.println(x.hashCode() == z.hashCode()); // true
if equals() true,hashCode() == must true. if hashCode() == true, equals() might return true.
Request garbage collection with System.gc(); (Recommended) เป็นเพียงร้องขอ แต่อาจไม่ถูกปฏิบัติก็ได้
Object must be considered eligible before they can be garbage collected.
o = null; (ไม่เสมอไปที่ o อ้างอิงจะ eligible)
บทที่ 8 : Inner Classes
instantiating a static nested class ถูกเรียกด้วย oclass.iclass X = new oclass.iclass();
การเรียก inner class จาก outer class
oclass mo = new oclass();
oclass.iclass io = mo.new iclass();
Anonymous inner classes
class p { public void p1() { System.out.println("p1"); }
class pn {
p px = new p() { public void p1() { System.out.println("p2"); } };
}
local variable ใน method ต้องเป็น final แล้ว inner class จึงจะเรียกใช้ได้