SCJP考题中的陷阱---来自水木清华BBS
發信人:?ifyr?(元旦加措),?信區:?Java????????
標??題:?SCJP考題中的陷阱[from:chinajavaworld]?
發信站:?BBS?水木清華站?(Fri?May?24?08:23:55?2002)?
?
SCJP考題中的陷阱?原著?Ajith?Kallambella?[from:chinajavaworld]?
==========?
(1)?Two?public?classes?in?the?same?file.?(illegal)?
???同一個文件里有兩個public類。(非法)?-------------最基礎了
(2)?Main?method?calling?a?non-static?method.?(illegal)?
???在main(String[]?args)方法內調用一個非靜態方法。(非法)?
(3)?Methods?with?the?same?name?as?the?constructor(s).?(這種題常有)?
???與Constructor(s)有相同名字的方法。?
(4)?Thread?initiation?with?classes?that?do?not?have?a?run()?method.?(??贾}?
)?
???初始化了一個沒有run()方法的線程類。?
(5)?Local?inner?classes?trying?to?access?non-final?vars.?(illegal)?
???內部類嘗試訪問非final變量(非法)?
(6)?Case?statements?with?values?out?of?permissible?range.?(byte,int,?short,??
chat)?
???選擇語句case中,沒有使用允許的值。如(byte,int,short,char)等?
(7)?Math?class?being?an?option?for?immutable?classes?!!?(totally?wrong!)?
???Math類作為不可改變類。(完全錯誤)???(請提意見)?
(8)?instanceOf?is?not?same?as?instanceof.?
???instanceOf?不是?instanceof。?
(9)?Private?constructors.?(legal)?
???私有?的Constructor。?(合法)?
(10)?An?assignment?statement?which?looks?like?a?comparison.?
????一個?賦值語句?看起來像?比較語句。?
????比如說if(a=true),和if(a==true)。對于這種題眼睛亮一點。?
(11)?System.exit()?in?try-catch-finally?blocks.?(finally?不會執行)?
????在try-catch-final塊中的退出語句。?(finally不會執行)?
(12)?Order?of?try-catch-finally?blocks?matters.?(若順序錯的話:?error:?No?try?
?before?catch)?
????try-catch-final塊的順序問題。?
(13)?main()?can?be?declared?final.?(OK)?
????main()方法?可以聲明為?final.?
(14)?-0.0?==?0.0?is?true.?
????如題。?
(15)?A?class?without?abstract?methods?can?still?be?declared?abstract?
????沒有?抽象方法的類,仍然可以定義為抽象類。?
(16)?RandomAccessFile?descends?from?Object?and?implements?DataInput?and?Data?
Output.?
????RandomAccessFile?類繼承Object,并且實現了DataInput和DataOutput接口。?
(17)?Map?does?not?implement?Collection.?
????Map?并不實現?Collection.?
(18)?Dictionary?is?a?class,?not?an?interface.?
????Dictionary?是一個類,不是接口。?
(19)?Collection?is?an?Interface?where?as?Collections?is?a?helper?class.?(這題?
我倒沒見過,但還真容易看混)?
????Collection是一個接口,但?Collections卻是一個輔助類。?
(20)?Class?declarations?can?come?in?any?order.?
?????(也就是說:?class?Child?extends?Parents{}?
????????????????class?Parents{}?
??????這種順序是可以的.)?
????可以以任何順序申明類。?
(21)?Forward?references?to?variables?gives?compiler?error.?
????把?reference?給?變量,會產生編譯錯誤。?(請提意見)?
(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,不是空字符。?
(25)?An?empty?string?is?NOT?the?same?as?a?null?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”已經要在一個循環里(如for,do,while),它不能在case語句中出現。?
?
(28)?Primitive?array?types?can?never?be?assigned?to?each?other,?eventhough?t?
he?primitives?themselves?can?be?assigned.?
???(也就是說:?ArrayofLongPrimitives?=?ArrayofIntegerPrimitives?會編譯出錯,但?
?longvar?=?intvar?是合法的)?
???Primitive(int,char,long等)數組是不能互相賦值的,即使它們本身可以。?
(29)?A?constructor?can?throw?any?exception.?
????一個Constructor可以拋出任何異常。?
(30)?Initilializer?blocks?are?executed?in?the?order?of?declaration.?
????初始化塊是按照聲明的順序執行的。?
(31)?Instance?initializer(s)?gets?executed?ONLY?IF?the?objects?are?construct?
ed.?
????實例初始化語句塊只有在它建立后才會被執行。?
(32)?All?comparisons?involving?NaN?and?a?non-Nan?would?always?result?false.??
(對大多數朋友來說這可是個盲點噢)?
????所有關于?NaN(Not?a?Number)?和?non-NaN?的比較,都返回false.?
????這條很重要。?
(33)?Default?type?of?a?numeric?literal?with?a?decimal?point?is?double.?
????我在這里把Java成員變量默認初始化原則寫一下:?
??????????成員變量類型????????取值?
???????????????????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?wh?
ile?float?/?and?%?will?never,?even?in?case?of?division?by?zero.?
????integer和long?操作?/和%?的話,?會拋出ArithmeticException,但是?float形不?
會,即使是除以0。?
(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.?
?????你永遠不可能?轉化具有同一個超類的類的對象,即使是刻意轉化。class?A?
class?sonA?extends?A?
class?daughterA?extens?A?
對這種情況:?
sonA?和?daughterA?之間不能相互轉化。?
即:sonA?son?=?(sonA)?daughterA();是非法的。?
而:sonA?son?=?(sonA)?A();A?father?=?(A)?sonA();是合法的。?
(37)?.equals?returns?false?if?the?object?types?are?different.It?does?not?rai?
se?a?compiler?error.?
????equals()?返回?false?如果對象類型不同,但不產生?編譯錯誤。?
(38)?No?inner?class?can?have?a?static?member.(but?static?inner?class?can)?
????沒有內部類?可以擁有?靜態成員。(但靜態內部類可以)?
(39)?File?class?has?NO?methods?to?deal?with?the?contents?of?the?file.(also?t?
he?existing?directory)?
????File類沒有?任何?處理文件內容的方法。(當然,存在的目錄也一樣)?
(40)?InputStream?and?OutputStream?are?abstract?classes,?while?DataInput?and??
DataOutput?are?interfaces.?
????InputStream?和?OutputStream?是?抽象類,但是?DataInput?和?DataOutput是?接?
口。?
?
總結
以上是生活随笔為你收集整理的SCJP考题中的陷阱---来自水木清华BBS的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《MongoDB入门教程》第07篇 CR
- 下一篇: 《MongoDB入门教程》第21篇 CR