2009年2月9日 星期一

SCJP考題中的陷阱

SCJP考題中的陷阱
原著 Ajith Kallambella

(1) Two public classes in the same file. (illegal)
 一個source file只能有一個public class

(2) Main method calling a non-static method. (illegal)
 static 不能夠存取非static成員。(程式進入點的main(),是static method)

(3) Methods with the same name as the constructor(s). (這種題常有)
 方法名稱可以跟Constructor和Class名稱相同
 constructor不能有回傳值、也不能夠被繼承

(4) Thread initiation with classes that do not have a run() method. (常考之題)
 繼承自Thread的類別,必須要override Thread class的run()(預設是無
內容)

(5) Local inner classes trying to access non-final vars. (illegal)
 Local inner class 絕對不能夠存取非final的 local var

(6) Case statements with values out of permissible range. (byte,int, short, chat)
 注意「四大金剛」的值的範圍
 範圍:
byte :
char :
short :
int :
double 8 bytes,變精度,不精確 15 位十進制數字精度,有效位數 15~16位
long 8 bytes,大範圍的整數,-9223372036854775808 到 9223372036854775807 (19位)
int 4 bytes,常用的整數,-2147483648 到 +2147483647 (10位)
short 2 bytes,小範圍整數,-32768 到 +32767 (5位)
byte byte, -128~127
(7) Math class being an option for immutable classes !! (totally wrong!)
 Math Class無法改變,也無法覆寫。它的方法是static、final的method

(8) instanceOf is not same as instanceof. (注意O大小寫之分)
 instanceof是用來判斷某物件屬於何種類別物件的運運算元。

(9) Private constructors. (legal)
 Constructor可以用 public、private、protected、(default)修飾

(10) An assignment statement which looks like a comparison.
 比如說if(a=true) [應該是if(a = = true)]
 在Java中, 「= =」比較的回傳值是boolean,所以a = = b = = c這種敘述可能會造成exception,因為 c不一定是boolean value。

(11) System.exit() in try-catch-finally blocks. (finally block不會執行)
 finally block一定會執行,而且會蓋掉前面try / catch block所執行的結果。
 finally block 除非執行前,遇到System.exit(),否則一定會執行。
(12) Order of try-catch-finally blocks matters. (若順序錯的話: error: No try before catch)
 try / catch / finally 這三個block的順序不可顛倒

(13) main() can be declared final. (OK)
 main()當然可以宣告final,因為這樣是overloading 的main()

(14) -0.0 = = 0.0 is true.

(15) A class without abstract methods can still be declared abstract.
 abstract類別可以有已經實作的方法(但是必須至少要有一個abstract method)

(16) RandomAccessFile descends from Object and implements DataInput and DataOutput.

(17) Map does not implement Collection.
 Map 介面並不屬於Collection介面的子介面,更不會實作Collection介面方法

(18) Dictionary is a class, not an interface.

(19) Collection is an Interface where as Collections is a helper class. (這題我倒沒見過,但還真容易看混)
 Collection是集合介面
 Collections 是類別不是介面

(20) Class declarations can come in any order.
 Java 是free style的語言



(21) Forward references to variables gives compiler error.

(22) Multi dimensional arrays can be sparce.
(這句話是說: 多維陣列中子陣列不一定必須有一定個數的元素,比如我們把一個二維陣列看成一個矩陣,那麼行與列中的元素可以不完整,可以不對齊.)

(23) Arrays, whether local or class-level, are always initialized.
 陣列不論是在區域或是類別都必須要初始化(廢話…=.=)

(24) Strings are initialized to null, not 「empty」 String.
 String類別初始化為null,並非為「空的」String(只是尚未賦予reference)

(25) An empty String is NOT the same as a null String.
 String = “” 和 String = null 是不同東西

(26) A declaration cannot be labelled.
 宣告不能夠「標籤」

(27) "continue" must be in a loop(for, do, while). It cannot appear in case constructs.
 continue必須要在loop迴圈(像是for、do、while)內

(28) Primitive array types can never be assigned to each other, eventhough the primitives themselves can be assigned.
 Array of Long Primitives = Array of Integer Primitives 會編譯出錯,但 long
var = int var 是合法的

(29) A constructor can throw any exception.
 建構子不會丟出任何例外

(30) Initilializer blocks are executed in the order of declaration.
 初始區塊的執行順序依照宣告時的順序而定

(31) Instance initializer(s) gets executed ONLY IF the objects are constructed.
 實體的實體化方法只有在物件建立時才會執行

(32) All comparisons involving NaN and a non-Nan would always result false.
 所以牽涉到NaN(not a number)和非NaN的比較運算,一律回傳false

(33) Default type of a numeric literal with a decimal point is double.
成員變數類型 取值
byte 0
short 0
int 0
long 0L
char '\u0000'
float 0.0F
double 0.0D
boolean false
所有參考型別 null

(34) integer (and long ) operations / and % can throw ArithmeticException while float / and % will never, even in case of division by zero.

(35) = = gives compiler error if the operands are cast-incompatible.
 比較運算(==)會編譯出錯的原因常出於不相容的型別轉換

(36) You can never cast objects of sibling classes( sharing the same parent ), even with an explicit cast.
 「兄弟類別」(同一父類別)無法做型別轉換

(37) equals returns false if the object types are different.It does not raise a compiler
error.
 當物件不相同時,equal()會回傳false,這並不會造成編譯錯誤

(38) No inner class can have a static member.(but static inner class can)
 一般Inner Class不能夠宣告有類別成員(static member),但是static inner
class當然可以宣告有類別成員。

(39) File class has NO methods to deal with the contents of the file.(also the existing directory)
(40) InputStream and OutputStream are abstract classes, while DataInput and DataOutput are interfaces.

當然以上這些都是從題目中總結出來的技巧,其實書上也都有,大家還是多看看書噢.
本文章來自於神魂顛倒論壇 http://bbs.flash2u.com.tw
原文網址:http://bbs.flash2u.com.tw/dispbbs_65_17347.html

1 則留言:

  1. 你好:)
    關於第15個好像寫錯了
    抽象類別不一定要有抽象方法
    但是有抽象方法的類別一定要是抽象

    回覆刪除