| เตรียมสอนจาวา (rule.doc :: 19 สิงหาคม 2548)
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)
Primitive data types : byte=1byte, short=2bytes, int=4byte, long=8bytes, float=4bytes, double=8bytes, char=2bytes, boolean
Decimal Literals = 123
Octal Literals = 011 or 077 (081 with compile error)
Hexadecimal Literals = 0x001 or 0xcafe;
Floating-Point Literals = 1.0f or 1.0F
float f = 1.1 (Compile error)
double d = 1.1 or 1.1f
boolean b = true or false (else error)
char c = 'e' or '\u0065'
char use 16 bits ('\u065' and '\u00065' compile error)
c = (char)70000; (ok)
c = 80000 or -1 (compile error of 0 - 65535)
int[] a; or int a[]; or int a[][]; or int[] a[];
Object in the heap is reference variable
Primitive in the stack
 
 บทที่ 1 : Language Fundamentals
เขียน method main ให้ถูก ไม่งั้น ฟ้อง runtime แต่ args เปลี่ยนได้ และ public กับ static สลับหน้าหลังได้class x {
 public static void main(String args) { System.out.println("a"); }
 }
main เรียกใช้ instance variable หรือ method ได้ แต่ instance variable, method ต้องประกาศเป็น static เพราะการเรียกตรงจาก main ต้องเรียก ตัวที่ประกาศเป็น static เท่านั้น
keyword เหมือน reserved word (ผีตัวเดียวกัน)
keyword เป็น identifiers, method, variable ไม่ได้
keyword เป็น lowercase ทั้งหมด และที่เป็นแต่ไม่ใช้คือ goto และ const
keyword มี 49 ตัวแบ่งเป็นดังนี้Access Modifiers: private, protected, public
 Class, Method, Variable Modifiers: abstract, class, extends, final, implements, interface, native, new, static, strictfp, synchronized, transient, volatile
 Flow Control: break, case, continue, default, do, else, for, if, instanceof, return, switch, while
 Error Handling: catch, finally, throw, throws, try, assert
 Package Control: import, package
 Primitives: boolean, char, byte, short, int, long, float, double
 Variable Keywords: super, this
 Void: void
 Unused: const, goto
primitive มี 8 แต่มีแค่ 6 ตัวที่มีค่าเป็น positive และ negative 
ใช้ keyword เป็นชื่อ class สามารถ compile ได้ แต่ run ไม่ผ่านฟ้อง NoClassDefFoundError
สำหรับ byte มี -128 ถึง 127 
char เป็น unsigned 16 bits ค่าอยู่ในช่วง 0 ถึง 65535
ค่าที่เป็นไปได้ของ 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 precisionwrong : b = 100 + 100;
 wrong : b = (byte)100 + 100;
 right : b = (byte)(100 + 100);
String รับ null ได้ แต่รับ Null ไม่ได้ และ null ไม่ใช่ keyword 
String รับ char, 'a', '\u1111' หรือ (String)'a' ก็ไม่ได้ 
การประกาศอาร์เรย์ ต้องจองมิติแรกไว้ก่อน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)
อาร์เรย์หลายมิติ คืออาร์เรย์ของอาร์เรย์ จะรับค่าได้เฉพาะ node สุดท้าย ถ้าเป็นมิติแรกก็ต้องรับมาเป็นอาร์เรย์
อาร์เรย์หลายมิติ จะเปลี่ยนจำนวนมิติไม่ได้ แต่เปลี่ยนจำนวนสมาชิกในแต่ละมิติได้เช่น อาร์เรย์ 3 มิติ จะสามารถใช้ a[0] = new int [5][1];
 เช่น อาร์เรย์ 3 มิติ จะสามารถใช้ a[1] = new int [2][3];
 wrong : a[0] = 5;
 wrong : a[0][1] = 5;
 right : a[0][1][0] = 5;
 right : a[0][2] = a[0][1];
 บทที่ 2 : Declarations and Access Controls
access modifiers หรือ access level : public, protected, private (Default ไม่ต้องใส่)
class มี modifers ได้เพียง 2 แบบคือ public หรือ default จะใช้ protected หรือ private ไม่ได้
class และ method สามารถใช้ strictfp
method สามารถใช้ native, synchronized
variable สามารถใช้ transient, volatile
final ใช้ได้ทั้ง class, method และ variable
abstract ใช้ได้กับ class และ method
abstract เป็น private หรือ final ไม่ได้
local variable มีได้เฉพาะ final
constant variable ใน interface เป็นได้เฉพาะ public, static และ final เช่น ภาษี
method ใน interface เป็นได้เฉพาะ public และ abstract
 บทที่ 3 : Operators and Assignments
shift operator มี >> และ << เลื่อน bit แบบคิดเครื่องหมาย และ >>> ไม่คิดเครื่องหมาย ซ้ายเป็น 0 ตลอด
ถ้าใช้ <<< จะ compile ไม่ผ่าน
ผลของ 8 >> 1 กับ 8 >>> 1 เหมือนกันคือ 4 (เพราะ 0000-1000 เป็น 0000-0100)
ถ้า -128 คือ 1000-0000เมื่อ -128 >> 1 จะได้ -64 คือ 1100-0000 เพราะเวลาคิดจะ complement ได้ 0011-1111 แล้ว + 1 เป็น 0100-0000 = 64
 เมื่อ -128 >> 2 จะได้ -32 คือ 1110-0000 เพราะเวลาคิดจะ complement ได้ 0001-1111 แล้ว + 1 เป็น 0010-0000 = 32
 
ถ้า -6 คือ 1111-1010เมื่อ -6 << 1 จะได้ -12 คือ 1111-0100 เพราะเวลาคิดจะ complement ได้ 0000-1011 แล้ว + 1 เป็น 0000-1100 = 12
 เมื่อ -6 << 2 จะได้ -24 คือ 1110-1000 เพราะเวลาคิดจะ complement ได้ 0001-0111 แล้ว + 1 เป็น 0001-1000 = 24
 
ถ้า -2 คือ 1111-1110เมื่อ -2 >>> 1 จะได้ -1 คือ 1111-1111 เพราะเวลาคิดจะ complement ได้ 0000-0000 แล้ว + 1 เป็น 0000-0001 = 1
ถ้า -127 คือ 1000-0001เมื่อ -127 << 1 จะได้ 2 คือ 0000-0010 (เครื่องหมาย คือ bit ที่ 8 หายไป)
 เมื่อ -127 >> 1 จะได้ -64 คือ 1100-0000 เพราะเวลาคิดจะ complement ได้ 0011-1111 แล้ว + 1 เป็น 0100-0000 = 64
 เมื่อ -127 >>> 1 จะได้ -64 คือ 1100-0000 เพราะเวลาคิดจะ complement ได้ 0011-1111 แล้ว + 1 เป็น 0100-0000 = 64 (ไม่เห็น bit ที่ 8 เป็น 0 เลย)
 
ถ้าใช้ << 33 จะเหมือน << 1 (เพราะ 33 % 32 เหลือ 1)
การ casting ของ shift operatorwrong : สำหรับ byte ถ้า x = x << 1;
 wrong : สำหรับ byte ถ้า x = (byte)x << 1;
 right : สำหรับ byte ถ้า x = (byte)(x << 1);
+ หมายถึง String Concatenation Operator ซึ่งใช้ได้ทั้งตัวเลข และตัวอักษร
System.out.println(1 + 1 + "a" + "a" + 1 + 1); ผลลัพธ์คือ 2aa11
 บทที่ 4 : Flow Control, Exceptions and Assertions
try เฉย ๆ ไม่ได้ ต้องอยู่กับ catch หรือ finally อย่างใดอย่างหนึ่ง
สั่ง return ใน catch จะเลิกทำงานต่อเมื่อออกจาก finally
finally จะทำงาน ไม่ว่าจะเข้า catch หรือไม่ เพราะทำเป็นสิ่งสุดท้ายก่อนออกจาก try
switch รัยได้เฉพาะ byte, short, int และ char รวมถึงตัวแปรที่มี data type ข้างต้นที่เป็น final เท่านั้น
ใน case ต้องมี break เพราะถ้าไม่มีก็จะเลื่อนไปทำ case อื่นโดยไม่ตรวจสอบเลย
default ไม่จำเป็นต้องอยู่ท้ายสุด ขอให้มี case และ default อยู่กับ break ครบคู่เท่านั้น แต่ไม่ครบก็ compile และ run ผ่าน
loop ไม่รู้จบเกิดขึ้นได้ แต่ Ctrl-Break ก็หยุดได้
if (true); สามารถ compile แล้ว run ผ่านด้วย แต่ if (1); compile ไม่ผ่าน
label ต้องใช้กับ loop เช่น for หรือ while ไม่งั้น compilation fail
หลังปิด } ของ try ต้องเป็น catch หรือ finally ไม่งั้น compilation fail
finally หรือ catch ของ try มีซ้ำกันไม่ได้
สามารถ throw ใน try เพื่อแจ้ง error ได้เลยclass x {
 public static void main(String args[]) {
 try{
 throw new RuntimeException();
 } catch (RuntimeException e) { System.out.println("catch");}
 }}
การปิดแฟ้ม เช่น f.close() ต้องทำใน try เพราะทำใน finally จะเกิด compilation fail
ถ้ามี throw exception ที่ไม่กำหนดใน catch จะเลิกงานหลังพบ error ใน try แล้วมาทำ finally จึงจะ หยุดทำงานเพราะ runtime
assert ใช้ตรวจจับสภาวะไม่พึงประสงค์ ถ้าเป็นเท็จก็จะเป็น runtime พร้อมแสดงข้อความหลังเครื่องหมาย :class X {
 public static void main(String args[]) {
 int j = 1;
 assert(j > 1) : "show here";
 }}
 compile: javac -source 1.4 X.java
 run : java -ea X
 run : java -da X
boolean x = false; if (x = true) { x = false; }
 บทที่ 5 : OO, Overloading, Overriding, Constructors, Return Types
override มี name, argument list และ return type เหมือนกัน ส่วน modifier ต้องไม่จำกัดกว่าเดิม และไม่ throw exception มากไปกว่าเดิม
final, constructor ไม่สามารถ override
overload ต้องมี argument list ที่ต่างกัน ส่วน return type ต่างกันก็ได้
มีชื่อเดียวกันซ้ำกันใน class เดียวกัน เรียกว่า overload จึงต้องมี argument ต่างกัน
polymorphism เกี่ยวข้องกับ override ไม่ใช่ overload
IS-A คือ inheritance จากปู่ สู่พ่อ
HAS-A คือ การอ้างอิงซึ่งกันและกันธรรมดา ไม่ใช่การสืบทอด
super และ this ต้องเขียนบรรทัดแรกของ constructor เท่านั้น
Constructor ไม่มี return type ถ้ามีก็ไม่ใช่ constructor
Constructor ไม่สามารถสืบทอด จึงไม่สามารถ Override
 บทที่ 6 : Java.lang, Math Class, Strings and Wrappers
random() ให้ค่าแบบ double ตั้งแต่ 0.0 แต่ไม่ถึง 1.0
xxxValue() ใช้แปลงค่าของ wrapper เป็น primitive
parseXxx() รับ String แล้วเปลี่ยนเป็น primitive
valueOf() รับ String แล้วเปลี่ยนเป็น wrapper
i==1 ต่างกับ s.equals("aa")
NaN (Not a Number) เช่น square root ของค่าติดลบ และ NaN จะไม่เท่ากับอะไรเลย แม้แต่ตัวเอง
NaN is not equal to anything, not even itself.
float ถูกหารด้วย 0 ได้ และค่าจะเป็น Infinity
การสร้าง object แบบ wrapperRight: 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.
java.lang.StringBuffer ไม่สามารถ override equals() และ hashCode()
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 จึงจะเรียกใช้ได้
 บทที่ 9 : Threads
thread อาจ blocked/waiting โดย wait(), sleep() หรือ join()
Object class มี wait(), notify() และ notifyAll()
sleep() จะ delay เป็น millisecond คือ 1000 เท่ากับ 1 วินาที
wait(), notify() และ notifyAll() ต้องอยู่ใน synchronized 
wait(), sleep() ต้องอยู่ใน try catch
 แนะนำเว็บ (Web Guides)
http://www.perlphpasp.com/class/sl-110.htm หลักสูตรที่ 1
http://www.perlphpasp.com/class/sl-275.htm หลักสูตรที่ 2
http://www.perlphpasp.com/class/javacert.php รายละเอียดโครงการ JavaPiwat
http://maxlearn.eng.ku.ac.th/online_training/java/ (132 + 150 = 282 ชั่วโมง)
1. ชุดวิชา JAVA web pro 35,000 บาท
 2. ชุดวิชา Advance Java 45,000 บาท (สำหรับ SCJP)
http://www.thaiall.com/class แนะนำจาวาและตัวอย่างข้อสอบ
http://www.thaiall.com/scjp ฝึกสอบ 10 ข้อ
http://lampang.thcity.com/scjp ฝึกสอบ 10 ข้อ
http://www.thaiall.com/quiz ฝึกสอบอย่างง่าย
 |