ï»??xml version="1.0" encoding="utf-8" standalone="yes"?>免费av一区,最新亚洲人成网站在线观看,日韩精品系列http://www.aygfsteel.com/balajinima/category/42196.htmlzh-cnFri, 16 Oct 2009 15:53:00 GMTFri, 16 Oct 2009 15:53:00 GMT60Java认证之精屁æ€È»“http://www.aygfsteel.com/balajinima/articles/298543.html李云æ³?/dc:creator>李云æ³?/author>Fri, 16 Oct 2009 03:50:00 GMThttp://www.aygfsteel.com/balajinima/articles/298543.htmlhttp://www.aygfsteel.com/balajinima/comments/298543.htmlhttp://www.aygfsteel.com/balajinima/articles/298543.html#Feedback0http://www.aygfsteel.com/balajinima/comments/commentRss/298543.htmlhttp://www.aygfsteel.com/balajinima/services/trackbacks/298543.html3.switch里面只能是short,int,char,byteçš?
4.if (a="a") ¾~–译é”? if(a=false)¾~–译ok,原因是a=falseçš„a代表了个booleanå€?br /> 6.˜q™ç§å†™æ³•Outer.Inner i = new Outer().new Inner(); OK!
7.æ–‡äšgé‡? 8 默认是int, 8.0默认是double
8.八进制是 0开�不是O(英文o)
9.byte -128~127 ,-128是二˜q›åˆ¶çš„多ž®?br /> 11. -1>>32˜q˜æ˜¯-1, -1>>>32ä¸ÞZ»€ä¹ˆè¿˜æ˜?1
12. char c='c'; String s ="s"; s+=c;¾l“æžœæ˜?sc!!!
13. boolean b1=true; boolean b2=true; System.out.println(b1|b2); ¾l“果是true.¾~–译没错!
java的默认import包有哪些
AWT 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1. 所有组件flowLayout会被压羃为它们的最ž®å°ºå¯?br /> 2. BorderLayoutçš„North,South,West,East,Centerž®±æ˜¯˜q™ä¸ªæ ·å­,四个单词大小写区åˆ?å¿…é¡»˜q™ä¹ˆå†?否则é”?br /> 2.1如果只是add,而不写东南西北中,默认是中
2.2如果不加componentåˆîC¸­,那中ž®Þq©ºç€
2.3如果某个方位有个component,如果你还往上面加component,后来的会覆盖原来�br /> 3. applet,panel默认布局是flowlayout
4. frame,dialog默认布局是borderlayout
5. 各种awe¾cȝš„¾l§æ‰¿å…³ç³»,frame,window,component,...
6. window,frame,dialog不能被嵌入到容器�注意:window!
7. action event作用于button和textfeild的回车时�br /> 8. item event作用于list,choice,checkbox的选择改变时刻
9. 如果容器不可è§?æ–°layout½Ž¡ç†å™¨åŽå‰çš„componentž®†è·Ÿéšè¿™ä¸ªæ–°çš„layout变化,如果容器可见,˜q™äº›componentž®†ä¸å—后来的layout影响
10.gridLayout里的componentž®ºå¯¸ä¸€æ ?br /> 11.gridBagLayouté‡?component可以占用多个grid
12.System.exit();在Applet里面不允许调�
AWT事äšg
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1.事äšg¾cÀLœ‰
(symantic)语义事äšg:ActionEvent,AdjustEvent,ItemEvent,TextEvent
低çñ”事äšg:ComponentEvent,ContainerEvent,FocusEvent,InputEvent,KeyEvent,MouseEvent,PaintEvent,WindowEvent
2.监听�
ActionListener,AdjustListener,CompentListener,ContainerListener,FocusListener,ItemListener,
KeyListener,MouseListener,MouseMotionListener,TextListener,WindwosListener, �1个Listener,
七个adpter,ž®‘çš„4个是ActionLisenter,AdjustListener,ItemListener,TextListener,它们只有一个方æ³?
3,鼠标MouseListener�个方�clicked,pressed,released,entered,exited
4.鼠标MouseMotionListener�个方�mouseDragged,mouseMoved
¾cÕd’Œå¯¹è±¡(Class and Object)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0.最外层的类可以声明成final: "$file a":< final class a{}> ok!,但是不能是private和static�
1.overload是同¾c»é‡ŒåŒæ ·çš„æ–¹æ³•名,override是父子的¾l§æ‰¿
2.override的返回结果必™åÖM¸€æ ?否则¾~–译错哦
The return type of an overriding method is identical to the return type of the method it overrides.
2.1 overrideçš„modifier可以扩大,但是不能¾~©å°.比如父类private void test(){} 子类:public void test(){} ,没问é¢?如果反了,ž®±æ­»¾˜˜ç¿˜äº?
3.super.super(),�错的,没这么写�br /> 4.static和非static之间永远无法override!
5. 看程�br /> public class A{
void test1() throws BaseEx{hi();}
void hi(){System.out.println("say hi,a");}
}
class AA extends A{
void hi(){System.out.println("say hi,aa");}
}
class test{
static void main(String b[]) throws Exception{
A a = new AA();
a.test1();
}
}
¾l“æžœæ˜?"say hi,aa",˜q™è¯´æ˜Žä»€ä¹ˆè¯´æ˜?æ–ÒŽ³•永远跟着¾cȝš„原来面目èµ?è€?变量恰恰相反!
6.一个非抽象æ–ÒŽ³•æ­ÀL´»ä¹Ÿåˆ«æƒ³override成一个抽象方æ³?br /> 7.override的子¾cÈš„æ–ÒŽ³•抛出的的异常只能是父¾cÀL–¹æ³•抛出异常的子异常类,或者无!
8.构造器不能是native,final,static,synchronized�可以是public,private,什么都没有�呵呵
9.构造器函数里还可以写returnå‘?但后面什么都不许æœ?甚至null(˜q™ä¸æ˜¯åºŸè¯å—,哈哈)
10.构造器不能˜q”回å€?˜q™å¤§å®‰™ƒ½çŸ¥é“,但如果有ä¸?构造器"反å€égº†,别紧å¼?它就不是构造器å–?只是个普通函æ•?br /> 11.super();this();˜q™ä¸¤ä¸ªå‡½æ•°åªèƒ½åœ¨æž„造函数里调用.
12,成员变量声明时候赋�比构造函数还�int i=1; ealier than Test(){}
13.æ–ÒŽ³•的参数变量可以是final.
14. hashCode˜q”回一个int
15. void wait() throws InterruptException wait扔InterruptException异常
16. java.lang.Void 是void的包装类
17. Byte,Interger,Double...所有的跟数有关的包装类都是¾l§æ‰¿äºŽNumber

接口Interface)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1.接口的所有方法默认都是public,abstract和non-static�br /> 2.接口的所有变量默认都是public,static,final�所�接口的变量不能改�在它的实现类�
3.接口的实现类实现的方法必™åÕd’ŒæŽ¥å£çš„æ–¹æ³•抛出同æ ïLš„异常,不许是子¾c?˜q™å’Œoverride不一æ ?同样,如果接口æ–ÒŽ³•没有æŠ?实现æ–ÒŽ³•也不能抛.
4.实现¾cÕd®žçŽ°çš„æ–ÒŽ³•必须昑ּçš„声明成public,什么都不写都不è¡?å•?!!
5.接口不能声明成final,要不它怎么被实现呢(˜q™å¥½åƒåˆæ˜¯åºŸè¯å•Š
6.一个类实现两个接口,如果两个接口有相同的æ–ÒŽ³•,实现¾cÕd°±å®žçް˜q™ä¸ªæ–ÒŽ³•,没问题的.

内嵌¾c»Inner Class)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1.内嵌¾cÕd¯ä»¥è®¿é—®outer¾cȝš„ä»ÖM½•变量,包括¿Uæœ‰çš?
2.静态inner¾c?只能讉K—®outer¾cȝš„ä»ÖM½•static变量
2.1内嵌¾cÕd¯ä»¥æ˜¯final,abstractçš?br /> 3.我靠,æ–ÒŽ³•内的内嵌¾cÖM¸èƒ½äØ“static: void test(){ static class A{}} XXXXX!!!!
4.我靠,æ–ÒŽ³•内的内嵌¾cÖM¹Ÿä¸èƒ½å¸¦ä“Q何modifier,void test(){ public class A{}} XXXXX!!!!
5.我靠,æ–ÒŽ³•内的内嵌¾cÕdªèƒ½è®¿é—®æ–¹æ³•内的final变量,但是,可以讉K—®outer¾cȝš„ä»ÖM½•变量.
6.匿名¾cÖM¸èƒ½æœ‰æž„造器,但声明时候带参数,相当于构造器的参æ•îC¼ é€?
class ABC{}
class ABCD{private ABCD(int i){}}
ABC test3(){return new ABC(){};}
ABCD test4(){return new ABCD(3){};}
interface iii{}
iii test5(){return new iii(){};}
//class BCD extends ABCD{} compile error,因䨓,
看上面就知道,new iii(){};实际上匿名类实现了iii接口;new ABC(){};实际上是匿名¾cȝ‘ô承了ABC.
8.
class A {private A(){System.out.println("a!");}}
class B extends A{}
我靠,没错!B实例的时候会ä¸ÕdŠ¨è°ƒç”¨çˆ¶ç±»A的构é€?即ä‹É是privateçš?看来也没问题!!!
9.内部¾cÕd¯ä»¥æœ‰synchronizedæ–ÒŽ³•,那么锁是˜q™ä¸ªå†…部¾c?跟外部类没一点关¾p?内外分别çš?在锁的问题上.
10.外部¾cÖM¸èƒ½é€šè¿‡this被访é—?this˜q™æ—¶å€™åº”该指的是内部¾c?享用外部¾cÈš„æˆå‘˜ž®Þq›´æŽ¥ç”¨,不用加ä“Q何限定词
11.如何用this呢请�
class Outer{ int i;
class Inner{
class InnerInner{
void Test(){
Outer.this.i=1;
}
}
}
}
看见了吧,¾cÕd.this.变量å?可以引用到i,½W¬ä¸€‹Æ¡çœ‹åˆ°å§,嘿嘿,孤陋寡闻.
12.注意˜q™ä¸¤¿Uå†™æ³•都可以
Class Outer.Inner i = new Outer().new Inner();
或� Class o= new Outer(); Class Outer.Inner i=o.new Inner();

¾U¿ç¨‹Thread)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
åŽÈœ‹Threadçš„API!!!!
1.¾U¿ç¨‹å¯åŠ¨å¿…é¡»é€šè¿‡start函数.
2.run函数不许也只能是public�
3.¾U¿ç¨‹æœ‰ä¼˜å…ˆçñ”ä»?åˆ?0,通过Thread.setPriority(int);来改å?不能­‘…过10,否则会出现运行异å¸?br /> 4.¾U¿ç¨‹ä¼˜å…ˆ¾U§é»˜è®¤æ˜¯5,即NORM_PRIORITY.NORM_PRIORITY是Thread的静态变量吗
5.Thread.yeild();是静态方æ³?所ä»?使用格式是Thread.yield();她强˜q«å½“前的˜q›ç¨‹æ”‘Ö¼ƒCUP.
6.sleep(1000),是说¾U¿ç¨‹ç¡è§‰1¿U?然后,˜q›å…¥Ready状æ€?注意,不是˜qè¡Œçжæ€?它还要等OS来调度来获得CUP.

java.lang.*;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1.数组的科� int ia[][]={{1,2},null}; int ib[][]=(int[][])ia.clone();
2.什么是NaN然后看ceil(NaN),floor(NaN),...
3.Math.floor(-1.1f);//-2.0
Math.ceil(-1.1f);//-1.0
Math.round(-1.6d)//-2
4.0=<1
5.Math,Interger,Boolean...½{‰ç±»åž‹åŒ…装类都是finalçš?不可¾l§æ‰¿
6.int round(float); long round(double);å”?round永远˜q”回不了ž®æ•°ç‚?br /> 7.static double ceil(double)
8.static double floor(double)注意åQŒceil,floor的只有这个double版本,什么都转成double!
9.static double sin(double 弧度); ˜q˜æœ‰cos,tan
10. new String; 可以是byte[];char[];String;StringBuffer
11. String的一些函� int length(); char charAt(int); String toUpperCase(); String toLowerCase();
12. String("Abc").equals(String("abc"))不相½{‰çš„,不然ž®×ƒ¸ä¼šæœ‰boolean equalsIgnoreCase(String)函数
13."012345678"是一个串的顺序号ç ?indexOf('1'),indexOf("1")都返å›?,subString(1,5)æ˜?345,嘿嘿:æ˜?[)"的感è§?br /> 14, trim()˜qžtab都退æ¯?"\t\n java ",trim()一下就只剩ä¸?java"äº?br /> 15. 关于对象的科éš?åŽÕd–³å–³API
16. "abcd".trim(),"abcd" + new String("ef")都是合理的写æ³?br /> 17. StringBufferçš?个构造器: ()åˆå§‹åŒ–å®¹é‡äØ“16,(int 初始化容é‡?,(String),åˆå§‹åŒ–å®¹é‡äØ“ä¸²é•¿åŠ ä¸Š16
18. StringBuffer的一些函� String toString(); append();reverse();insert();delete(int start,int end);deleteCharAt(int);setLength(int newLength);
19. String s=""; StringBuffer sb=new StringBuffer(); if (s==sb){}¾~–译é”?因䨓,s,sb¾cÕdž‹ä¸ä¸€æ ?不能比较
集合:
1.各接口和¾cȝš„关系,只有最后一个是¾c?br /> Collection:List:vector,ArrayList,LinkedList
Map:SortedMap:TreeMap
Collection:Set:SortedSet:TreeSet
Map:HashTable
Collection:Set:HashSet

基础Base)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1.java applicationçš„main可以不是publicçš?但必™åÀL˜¯staticçš?br /> 2.一个文件只能有一个public¾c?而且˜q˜å¾—跟文件名一æ ?包括大小å†?br /> 3.变量只能是字æ¯?$,_å¼€å¤?接下来的½W¬äºŒä¸ªå¯ä»¥æ˜¯,æ•°å­—
4.ch\u0061r='a'; char \u0063='b'; char c='\u0063';都是合法çš?br /> 5.1e-5d,合法.e-5d不合æ³?必须有前面的¾pÀL•°
6.int[] i[]={null{1,2}}正确! int i[]={1,2,3,} 正确!","和没有的效果一æ ?br /> 7.局部array,跟变量一æ ?使用前要初始åŒ?br /> 8.mainæ–ÒŽ³•可以为final

操作½W¦å’Œåˆ†é…½W?Operator and Assignment)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1.System.out.printl(1+2+"3");//33 System.out.printl("1"+2+3);//123
2.int i=0; i=i++;i=i++;i=i++; ¾l“æžœ,i=0!
3.int i[]; index=0; i[index]=index=5; ¾l“æžœæ˜?i[0]=5;!!!
4.byte b=10;可以,因䨓,10可以被自动从int转成byte
5.接下æ?byte b=b+10;不可ä»?!因䨓,10+b后的int无法被自动从int转成byte,问我,æˆ‘ä¹Ÿä¸çŸ¥é“äØ“ä»€ä¹?é?
6.byte b1 = 4; byte b2 = 6; b1 = b1 + b2;é”?¾~–译é”?b1 + b2å?必须强制转成byte,b1x1+b2);
7.XOR 一æ ïLš„ä¸?åQŒä¸ä¸€æ ·äØ“1 1,1=0;0,0=0;1,0/0,1=1
8. x == Float.NaN¾~–译错,应该是Float.IsNaN
9. x == Double.POSITIVE_INFINITY¾~–译可以
10.-1æ˜?111.... 1111,<<永远双™¡¥é›Óž¼Œ>>正补é›Óž¼Œè´Ÿè¡¥ä¸€åQ?gt;>>恒补é›?br /> 10.1 -1>>多少位都æ˜?1 ; 1<<31变成了最ž®è´Ÿæ•?1000....0000
11.最大正数是01111....1111
12.最ž®è´Ÿæ•°æ˜¯1000....0000(-2147483648)
13. a instanceof b,b必须是类/接口åQŒä¸èƒ½æ˜¯å®žä¾‹
--------补充------------------------------
1. byte,short,char 都存åœ?var = -var;¾~–译错误,因䨓,˜q™æ—¶å€™çš„-var已经自动转成个int¾cÕdž‹äº?br /> 2. int/0会扔出ArithmeticException
double,float/0得INF或�INF
0/0å¾—NaN
3. int a-b-c;是不½W¦åˆå‘½åè§„则的变量名¾~–译会出é”?
4. char a='\u0001';å¯? char b=\u0001;¾~–译é”?
5. boolean b1,b2,b3,b4,b5;
b1 = b2==b3;
b1 = b2<=b3 && b4==b5;
b1 = b2==b3==true
都是对的!�变�
b1 = b2==b3==b4 XXXXXXX¾~–译é”?
6. 1>>1 �
7. %= <<= =>> =>>>都是合法½W¦å·
8. --1-10*4 ˜q™ç§å†™æ³•没错,ž®±æ˜¯ (--1)-10*4
9. k=1;++k + k++ + +k ;¾l“æžœæ˜?,相当äº?(++2)+(2++)+(+3)
10.标号不能标示声明.
hi:
if {
break hi;
//break hi1;不行,不能向后调è{
}
//hi1:不行,不能攑֜¨å£°æ˜Žå‰?br /> int i;
hi1:
i=1;
11.public static void main(String s[]) throws Exception{}可以�main可以扔出异常
12. hi:
if(b==true){break hi;}
break 标号åQŒå¯ä»¥ç”¨åœ¨if里面.别的ä»ÖM½•都不è¡?包括break,continue 标号.
13.int x = i*-j; 我靠,没问题的!!!¾~–译没错! int x = i*j++ + +i++; ˜q™ä¸ªä¹Ÿæ²¡é—®é¢˜,

变量修饰½W?Modifier)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1.最外面一层的¾cÖM¸èƒ½å£°æ˜Žæˆprotectå’Œpraviteçš?br /> 2.同一个类的两个实例之间可以访问彼此的¿Uæœ‰æ–ÒŽ³•和私有变é‡?é…?br /> 3.protect和无修饰½W¦çš„具体区别关于外包可以讉K—®è¢«å­¾c»è®¿é—®çš„æ˜¯å“ªä¸?br /> 4.成员变量被修饰成finalå?必须声明时候就赋初值或者在构造器里面赋初å€?别指望她可以得到defaultå€?
5.抽象æ–ÒŽ³•不能是staticçš?!!
6.静态方法将随着¾cȝš„变化而变åŒ?看例å­?
class Parent{
static void test(){System.out.println("hi,parent")};
}
class Child extends Parent{
static void test(){System.out.println("hi,child")};
}
Parent p = new Child();
p.test(); //打出来的是hi,parent!
7.静态方法可以通过¾cȝš„实例调用.
new Child().test(); �Child.test(); 都OK!
8.transient只能用在¾cÈš„æˆå‘˜å˜é‡ä¸?不能用在æ–ÒŽ³•é‡?
9.transient变量不能是finalå’Œstaticçš?br /> 10.nativeæ–ÒŽ³•可以是private,abstractdçš?/p>

‹¹ç¨‹æŽ§åˆ¶
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1。不可到辄¡š„语句声明为错åQšwhile(false){} ;for(;false{};if(false){}都无法编è¯?br /> 2。for(½W¬ä¸€éƒ¨åˆ†;的第一部分可以用来声明或者赋å€û|¼Œä½†ä¸èƒ½ä¸¤è€…都
3。byte b; switch  { case 200: // 200 not in range of byteåQŒå› ä¸?00­‘…过b的范å›ß_¼Œž®†ç¼–译错è¯?br /> 4。带标签的continue回达到标½{„¡š„位置åQŒä»Žæ–°è¿›å…¥ç´§æŽ¥åœ¨æ ‡ç­¾åŽé¢çš„åó@çŽ?br /> 5。带标签的break会中断当前åó@环,òq¶è{¿UÕdˆ°æ ‡ç­¾æ ‡ç¤ºçš„的循环的末ž®?/p>

转型和上�Converting and Casting)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Binary operators convert the smaller(less precise) operand to the type of the larger(more precise) operand.
All operators will convert things smaller than ints to ints or larger. This includes char 's!
1.byte,char,short默认转成int
2.byte->short->int->long->float->double
char^
˜q™æ˜¯é»˜è®¤å¯ä»¥è½¬åž‹çš„æ–¹å?反了必须要显式cast! 特别注意:long->float是默认的,别看long64,float32,嘿嘿
˜q˜æœ‰ž®±æ˜¯çœ‹å¾—出来,charå’?byte,short之间无法互相默认转换
3.float f=1/3; OK!float f=1.0/3.0;¾~–译出错,因䨓1.0/3.0¾l“果是doubleçš?噢噢~,错喽!!
4.int i=1; byte b=i;�需要显式cast.
final i=1; byte b=i;ž®±ok! æˆ‘ä¹Ÿä¸çŸ¥é“äØ“ä»€ä¹?finalž®±å¯ä»?而且,据我实验只有intå’Œbyte的关¾p»è¿™æ ?其他不行.
5.int i[]; Object[] obj=i;� Object obj=i;� 数组只能转成Object,而不能是Object[]
6.int i[]; Object[] obj;i=(int[])obj; å¯? 对象可以通过昑ּæ¥è{成一个数¾l?

I/O
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1.File¾cȝš„一些重要方æ³?isFile();isDirectory();String[] list();exists();getAbsolutePath();getParent();
2.通过delete();mkdir();rename(File newname);可以操纵文äšg,但是却改变不了文件的内容
2.1 File¾cÀL— æ³•改变当前目å½?除非重新创徏一个File对象.
3.InputStreamReader(InputStream in,String encodingName);
OutputStreamReader(OutputStream in,String encodingName);
Encoding: 8859_1是Latin-1,包含ASCII
4.关闭close一个流,ž®Þp‡ªåŠ¨è°ƒç”¨äº†flush.
5.System.in,System.out,System.err,由JVM自动创徏
6.RandomAccessFile(File file,String mode);mode�r,rw



]]>
½W¬ä¹éƒ¨åˆ†åQšé›†åˆç±»æ¡†æž¶http://www.aygfsteel.com/balajinima/articles/298540.html李云æ³?/dc:creator>李云æ³?/author>Fri, 16 Oct 2009 03:44:00 GMThttp://www.aygfsteel.com/balajinima/articles/298540.htmlhttp://www.aygfsteel.com/balajinima/comments/298540.htmlhttp://www.aygfsteel.com/balajinima/articles/298540.html#Feedback0http://www.aygfsteel.com/balajinima/comments/commentRss/298540.htmlhttp://www.aygfsteel.com/balajinima/services/trackbacks/298540.html½W¬ä¹éƒ¨åˆ†åQšé›†åˆç±»æ¡†æž¶

  • 知道如何在特定的条äšg下选择适合的集合类/接口ã€?
  • 能正¼‹®åœ°å®žçްhashcodeæ–ÒŽ³•ã€?

½W¬ä¸€èŠ?nbsp;         所有接口和class定义

1:Set

public interface Set extends Collection

A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction.

 

2:SortedSet

public interface SortedSet extends Set.

A set that further guarantees that its iterator will traverse the set in ascending element order, sorted according to the natural ordering of its elements (see Comparable), or by a Comparator provided at sorted set creation time.

 

3:HashSet

public class HashSet extends AbstractSet implements Set, Cloneable, Serializable

This class implements the Set interface, backed by a hash table (actually a HashMap instance). It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time. This class permits the null element.

 

4:TreeSet

public class TreeSet extends AbstractSet implements SortedSet, Cloneable, Serializable

This class implements the Set interface, backed by a TreeMap instance. This class guarantees that the sorted set will be in ascending element order, sorted according to the natural order of the elements (see Comparable), or by the comparator provided at set creation time, depending on which constructor is used.

 

5:LinkedHashSet:

public class LinkedHashSet extends HashSet implements Set, Cloneable, Serializable

Hash table and linked list implementation of the Set interface, with predictable iteration order. This implementation differs from HashSet in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is the order in which elements were inserted into the set (insertion-order). Note that insertion order is not affected if an element is re-inserted into the set. (An element e is reinserted into a set s if s.add(e) is invoked when s.contains(e) would return true immediately prior to the invocation.)

 

 

 

5:Map

public interface Map

An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.

 

6:SortedMap

public interface SortedMap extends Map

A map that further guarantees that it will be in ascending key order, sorted according to the natural ordering of its keys (see the Comparable interface), or by a comparator provided at sorted map creation time. (This interface is the map analogue of the SortedSet interface.)

 

7:HashMap(允许key和value为NULL)

public class HashMap extends AbstractMap implements Map, Cloneable, Serializable

Hash table based implementation of the Map interface. This implementation provides all of the optional map operations, and permits null values and the null key. (The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.) This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time

 

8:TreeMap

public class TreeMap extends AbstractMap implements SortedMap, Cloneable, Serializable

Red-Black tree based implementation of the SortedMap interface. This class guarantees that the map will be in ascending key order, sorted according to the natural order for the key's class (see Comparable), or by the comparator provided at creation time, depending on which constructor is used.

 

9:HashTable(不允许key和value为NULL)

public class Hashtable extends Dictionary implements Map, Cloneable, Serializable.

This class implements a hashtable, which maps keys to values. Any non-null object can be used as a key or as a value.

 

9:LinkedHashMap

public class LinkedHashMap extends HashMap

Hash table and linked list implementation of the Map interface, with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order). Note that insertion order is not affected if a key is re-inserted into the map. (A key k is reinserted into a map m if m.put(k, v) is invoked when m.containsKey(k) would return true immediately prior to the invocation.)

 

9:IdentityHashMap

public class IdentityHashMap extends AbstractMap implements Map, Serializable, Cloneable

This class implements the Map interface with a hash table, using reference-equality in place of object-equality when comparing keys (and values). In other words, in an IdentityHashMap, two keys k1 and k2 are considered equal if and only if (k1==k2). (In normal Map implementations (like HashMap) two keys k1 and k2 are considered equal if and only if (k1==null ? k2==null : k1.equals(k2)).)

This class is not a general-purpose Map implementation! While this class implements the Map interface, it intentionally violates Map's general contract, which mandates the use of the equals method when comparing objects. This class is designed for use only in the rare cases wherein reference-equality semantics are required.

 

 

10:List

public interface List extends Collection

An ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list.

 

Unlike sets, lists typically allow duplicate elements. More formally, lists typically allow pairs of elements e1 and e2 such that e1.equals(e2), and they typically allow multiple null elements if they allow null elements at all.

 

11:ArrayList

public class ArrayList extends AbstractList implements List, Cloneable, Serializable

Resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null. In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list. (This class is roughly equivalent to Vector, except that it is unsynchronized.)

 

12:LinkedList

public class LinkedList extends AbstractSequentialList implements List, Cloneable, Serializable

Linked list implementation of the List interface. Implements all optional list operations, and permits all elements (including null). In addition to implementing the List interface, the LinkedList class provides uniformly named methods to get, remove and insert an element at the beginning and end of the list. These operations allow linked lists to be used as a stack, queue, or double-ended queue (deque).

 

13:Vector

public class Vector extends AbstractList implements List, Cloneable, Serializable

The Vector class implements a growable array of objects. Like an array, it contains components that can be accessed using an integer index. However, the size of a Vector can grow or shrink as needed to accommodate adding and removing items after the Vector has been created.

 

14:Collection

public interface Collection

The root interface in the collection hierarchy. A collection represents a group of objects, known as its elements. Some collections allow duplicate elements and others do not. Some are ordered and others unordered. The SDK does not provide any direct implementations of this interface: it provides implementations of more specific subinterfaces like Set and List. This interface is typically used to pass collections around and manipulate them where maximum generality is desired.

15:Collections

public class Collections extends Object

This class consists exclusively of static methods that operate on or return collections. It contains polymorphic algorithms that operate on collections, "wrappers", which return a new collection backed by a specified collection, and a few other odds and ends.



]]>
½W¬ä¸ƒéƒ¨åˆ†åQšçº¿½E?/title><link>http://www.aygfsteel.com/balajinima/articles/298538.html</link><dc:creator>李云æ³?/dc:creator><author>李云æ³?/author><pubDate>Fri, 16 Oct 2009 03:43:00 GMT</pubDate><guid>http://www.aygfsteel.com/balajinima/articles/298538.html</guid><wfw:comment>http://www.aygfsteel.com/balajinima/comments/298538.html</wfw:comment><comments>http://www.aygfsteel.com/balajinima/articles/298538.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/balajinima/comments/commentRss/298538.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/balajinima/services/trackbacks/298538.html</trackback:ping><description><![CDATA[<p align="left">½W¬ä¸ƒéƒ¨åˆ†åQšçº¿½E?/p> <ul type="disc"> <li>能用java.lang,Thread或java.lang.Runnable两种æ–ÒŽ³•定义åQŒå®žä¾‹åŒ–和开始一个新的线½E‹ã€? <li>知道哪些情况下可能阻止一个线½E‹çš„æ‰§è¡Œã€? <li>能ä‹É用synchronized,wait,notifyå’ŒnotifyAllåŽ»é¿å…åÆˆå‘è®¿é—®çš„é—®é¢˜åQŒä»¥åŠçº¿½E‹é—´ç›æ€º’通讯的问题ã€? <li>当执行synchronized,wait,notifyå’ŒnotifyAllæ—Óž¼ŒçŸ¥é“¾U¿ç¨‹å’Œå¯¹è±¡é”ä¹‹é—´çš„交互作用ã€?</li> </ul> <div> <h3><a name="_Toc74451196">§1.1.1 <wbr> <wbr> <wbr> <wbr> 一</a></h3> <p>What will happen when you attempt to compile and run the following code?</p> <p>class MyThread extends Thread</p> <p>{</p> <p>public void run()</p> <p>{</p> <p>System.out.println("MyThread: run()");</p> <p>}</p> <p>public void start()</p> <p>{</p> <p>System.out.println("MyThread: start()");</p> <p>}</p> <p>}</p> <p>class MyRunnable implements Runnable</p> <p>{</p> <p>public void run()</p> <p>{</p> <p>System.out.println("MyRunnable: run()");</p> <p>}</p> <p>public void start()</p> <p>{</p> <p>System.out.println("MyRunnable: start()");</p> <p>}</p> <p>}</p> <p>public class MyTest</p> <p>{</p> <p>public static void main(String args[])</p> <p>{</p> <p>MyThread myThread  <wbr>=  <wbr>new MyThread();</p> <p>MyRunnable myRunnable = new MyRunnable();</p> <p>Thread thread  <wbr>=  <wbr>new Thread(myRunnable);</p> <p>myThread.start();</p> <p>thread.start();</p> <p>}</p> <p>}</p> <p>Choices:</p> <p>a. Prints : MyThread: start() followed by MyRunnable:run()</p> <p>b. Prints : MyThread: run() followed by MyRunnable:start()</p> <p>c. Prints : MyThread: start() followed by MyRunnable:start()</p> <p>d. Prints : MyThread: run() followed by MyRunnable:run()</p> <p>e. Compile time error</p> <p>f. None of the above</p> <p>――――――――――――――â€?/p> <p>A is the correct choice. In the above code there is not any compilation error. Thus choice E is incorrect. Inside main() method, objects of MyThread and MyRunnable class are created followed by creation of Thread with object of MyRunnable class. Note that MyThread class extends Thread class and overrides the start() method of the Thread class. Thus on execution of "myThread.start()" statement, the start() method of the MyThread class will be executed and as a result "MyThread:start()" will be printed. Had the start() method not there in MyThread class, the start() method of the Thread class would be called which in turn would call the run() method of the MyThread class. On execution of "thread.start();", the start() method of the Thread class would be called which in turn will call the run() method of the class which is passed to Thread constructor (i.e. MyRunnable class). Thus "MyRunnable:run()" will be printed out. Thus choice A is correct.</p> <p> <wbr></p> <h3><a name="_Toc74451197">§1.1.2 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> äº?/a></h3> <p>What will be the output on compiling/running the following code?</p> <p>public class MyThread implements Runnable</p> <p>{</p> <p> <wbr> String myString = "Yes ";</p> <p> <wbr> public void run()</p> <p> <wbr> {</p> <p> <wbr>  <wbr> this.myString = "No ";</p> <p> <wbr> }</p> <p> <wbr> public static void main(String[] args)</p> <p> <wbr> {</p> <p> <wbr>  <wbr> MyThread t = new MyThread();</p> <p> <wbr>  <wbr> new Thread(t).start();</p> <p> <wbr>  <wbr> for (int i=0; i < 10; i++)</p> <p> <wbr>  <wbr>  <wbr>System.out.print(t.myString);</p> <p> <wbr> }</p> <p>}</p> <div> <p>Choices:</p> <p>a. Compilation Error  <wbr></p> <p>b. Prints : Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes and so on.</p> <p>c. Prints : No No No No No No No No No No and so on.</p> <p>d. Prints : Yes No Yes No Yes No Yes No Yes No and so on.</p> <p>e. The Output cannot be determined.</p> </div> <p>E is correct. Please note that there will not be any compilation error when the above code is compiled. Also note that calling start() method on a Thread doesn't start the Thread. It only makes a Thread ready to be called. Depending on the operation system and other running threads, the thread on which start is called will get executed. In the above case it is not guaranteed that the thread will be executed(i.e. run() method will be called), always before "for" loop is executed. Thus the output cannot be determined.</p> <p> <wbr></p> <h3><a name="_Toc74451198">§1.1.3 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> ä¸?/a></h3> <p>Multiple objects of MyClass (given below) are used in a program that uses</p> <p>multiple Threadsto create new integer count. What will happen when other threads</p> <p>use the following code?</p> <p>class MyClass</p> <p>{</p> <p>static private int myCount = 0;</p> <p>int yourNumber;</p> <p>private static synchronized int nextCount()</p> <p>{</p> <p>return ++myCount;</p> <p>}</p> <p>public void getYourNumber()</p> <p>{</p> <p>yourNumber = nextCount();</p> <p>}</p> <p>}</p> <p>Choices:</p> <p>a. The code will give compilation error.</p> <p>b. The code will give runtime error.</p> <p>c. Each thread will get a unique number.</p> <p>d. The uniqueness of the number among different Threads can't be guaranteed.</p> <p>――――――――――――â€?/p> <p>C is correct. The use of synchronized ensures that the number generated will not be duplicated, no matter how many Threads are trying to create the number. Thus D is incorrect. A and B are incorrect as the above code will not give any compiletime or runtime error.</p> <h3><a name="_Toc74451199">§1.1.4 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> å›?/a></h3> <p>What will happen when you attempt to compile and run the following code?</p> <p>public class MyThread extends Thread</p> <p>{</p> <p>String myName;</p> <p>MyThread(String name)</p> <p>{</p> <p>myName = name;</p> <p>}</p> <p>public void run()</p> <p>{</p> <p>for(int i=0; i<100;i++)</p> <p>{</p> <p>System.out.println(myName);</p> <p>}</p> <p>}</p> <p>public static void main(String args[])</p> <p>{</p> <p>try</p> <p>{</p> <p>MyThread mt1 = new MyThread("mt1");</p> <p>MyThread mt2 = new MyThread("mt2");</p> <p>mt1.start();</p> <p>// XXX</p> <p>mt2.start();</p> <p>}</p> <p>catch(InterruptedException ex)</p> <p>{</p> <p>}</p> <p>}</p> <p>}</p> <p>Choices:</p> <p>a.The above code in its current condition will not compile.</p> <p>b. In order to make the MyThread class prints "mt1" (100 times) followed by</p> <p>"mt2" (100 times), mt1.join(); can be placed at //XXX position.</p> <p>c. In order to make the MyThread class prints "mt1" (100 times) followed by</p> <p>"mt2" (100 times), mt1.sleep(100); can be placed at //XXX position.</p> <p>d. In order to make the MyThread class prints "mt1" (100 times) followed by</p> <p>"mt2" (100 times), mt1.run(); can be placed at //XXX position.</p> <p>e. In order to make the MyThread class prints "mt1" (100 times) followed by</p> <p>"mt2" (100 times), there is no need to write any code.</p> <p>―――――――――――――â€?/p> <p>A and B are correct. In its current condition, the above code will not compile as "InterruptedException" is never thrown in the try block. The compiler will give following exception: "Exception java.lang.InterruptedException is never thrown in the body of the corresponding try statement." Note that calling start() method on a Thread doesn't start the Thread. It only makes a Thread ready to be called. Depending on the operating system and other running threads, the thread on which start is called will get executed. After making the above code to compile (by changing the InterruptedException to some other type like Exception), the output can't be predicted (the order in which mt1 and mt2 will be printed can't be guaranteed). In order to make the MyThread class prints "mt1" (100 times) followed by "mt2" (100 times), mt1.join() can be placed at //XXX position. The join() method waits for the Thread on which it is called to die. Thus on calling join() on mt1, it is assured that mt2 will not be executed before mt1 is completed. Also note that the join() method throws InterruptedException, which will cause the above program to compile successfully. Thus choice A and B are correct.</p> <p> <wbr></p> <h3><a name="_Toc74451200">§1.1.5 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> äº?/a></h3> <p>What will happen when you attempt to compile and run the following code?</p> <p>public class MyThread extends Thread{</p> <p>String myName;</p> <p>MyThread(String name){</p> <p>myName = name;</p> <p>}</p> <p>public void run(){</p> <p>for(int i=0; i<100;i++){</p> <p>System.out.println(myName);</p> <p>}</p> <p>}</p> <p>public static void main(String args[]){</p> <p>try{</p> <p>MyThread mt1 = new MyThread("mt1");</p> <p>MyThread mt2 = new MyThread("mt2");</p> <p>mt1.start();</p> <p>// XXX</p> <p>mt2.start();</p> <p>}catch(InterruptedException ex){}</p> <p>}</p> <p>}</p> <p>A. compile error</p> <p>B. mt1.join();</p> <p>C. mt1.sleep(100);</p> <p>D. mt1.run()</p> <p>E. nothing need</p> <p> <wbr></p> <p>Choice A and B are correct. In its current condition, the above code will not compile as "InterruptedException" is never thrown in the try block. The compiler will give following exception: "Exception java.lang.InterruptedException is never thrown in the body of the corresponding try statement."</p> <p>Note that calling start() method on a Thread doesn't start the Thread. It only makes a Thread ready to be called. Depending on the operating system and other running threads, the thread on which start is called will get executed. After making the above code to compile (by changing the InterruptedException to some other type like Exception), the output can't be predicted (the order in which mt1 and mt2 will be printed can't be guaranteed). In order to make the MyThread class prints "mt1" (100 times) followed by "mt2" (100 times), mt1.join() can be placed at //XXX position. The join() method waits for the Thread on which it is called to die. Thus on calling join() on mt1, it is assured that mt2 will not be executed before mt1 is completed. Also note that the join() method throws InterruptedException, which will cause the above program to compile successfully. Thus choice A and B are correct.</p> <h3><a name="_Toc74451201">§1.1.6 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> å…?/a></h3> <p>Multiple objects of MyClass (given below) are used in a program that uses multiple Threads to create new integer count. What will happen when other threads use the following code?</p> <p>class MyClass{</p> <p>static private int myCount = 0;</p> <p>int yourNumber;</p> <p>private static synchronized int nextCount(){</p> <p>return ++myCount; <wbr> <wbr> //myCount为static</p> <p>}</p> <p>public void getYourNumber(){</p> <p>yourNumber = nextCount();</p> <p>}</p> <p>}</p> <p>A. the code ill give ompilation error</p> <p>B. the code ill give runtime error</p> <p>C. each thread will get a unique number</p> <p>D. the uniqueness of the number different Threads can’t be guaranteed.</p> <p> <wbr></p> <p>C is correct. The use of synchronized ensures that the number generated will not be duplicated, no matter how many Threads are trying to create the number. Thus D is incorrect. A and B are incorrect as the above code will not give any compiletime or runtime error.</p> <p> <wbr></p> <h3><a name="_Toc74451202">§1.1.7 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> ä¸?/a></h3> <p>What will be the output on compiling/running the following code?</p> <p>public class MyThread implements Runnable {</p> <p> <wbr> String myString = "Yes ";</p> <p> <wbr> public void run() {</p> <p> <wbr> <wbr> <wbr> this.myString = "No ";</p> <p> <wbr> }</p> <p> <wbr> public static void main(String[] args) <wbr> {</p> <p> <wbr> <wbr> <wbr> MyThread t = new MyThread();</p> <p> <wbr> <wbr> <wbr> new Thread(t).start();</p> <p> <wbr> <wbr> <wbr> for (int i=0; i < 10; i++)</p> <p> <wbr> <wbr> <wbr> <wbr> System.out.print(t.myString);</p> <p> <wbr> }</p> <p>}</p> <p>A. compile error</p> <p>B. prints: yes yes yes yes yes yes and so on</p> <p>C. prints: no no no no no no no no and so on</p> <p>D. prints: yes no yes no ye no ye no and so on</p> <p>E. the output cannot be determinated</p> <p> <wbr></p> <p>E is correct. Please note that there will not be any compilation error when the above code is compiled. Also note that calling start() method on a Thread doesn't start the Thread. It only makes a Thread ready to be called. Depending on the operation system and other running threads, the thread on which start is called will get executed. In the above case it is not guaranteed that the thread will be executed(i.e. run() method will be called), always before "for" loop is executed. Thus the output cannot be determined.</p> <h3><a name="_Toc74451203">§1.1.8 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> å…?/a></h3> <p>Which statements about thread are true?</p> <p>  A. Once a thread is created, it can star running immediately.</p> <p> <wbr></p> <p>  B. To use the start() method makes a thread runnable, but it does not</p> <p>necessarily start immediately.</p> <p> <wbr></p> <p>  C. When a thread stops running because of pre-emptive, it is placed at</p> <p>the front end of the runnable queue.</p> <p> <wbr></p> <p>  D. A thread may cease to be ready for a variety of reasons.</p> <p> <wbr></p> <p>  (bd)</p> <p> <wbr></p> <p>  题目åQšæœ‰å…³çº¿½E‹çš„哪些叙述是对的ã€?/p> <p> <wbr></p> <p>  A. 一旦一个线½E‹è¢«åˆ›å¾åQŒå®ƒž®Þq«‹å›_¼€å§‹è¿è¡Œã€?/p> <p> <wbr></p> <p>  B. 使用start()æ–ÒŽ³•可以使一个线½E‹æˆä¸ºå¯˜qè¡Œçš„,但是它不一定立卛_¼€å§‹è¿è¡Œã€?/p> <p> <wbr></p> <p>  C. 当一个线½E‹å› ä¸ºæŠ¢å…ˆæœºåˆ¶è€Œåœæ­¢è¿è¡Œï¼Œå®ƒè¢«æ”‘Öœ¨å¯è¿è¡Œé˜Ÿåˆ—的前面ã€?/p> <p> <wbr></p> <p>  D. 一个线½E‹å¯èƒ½å› ä¸ÞZ¸åŒçš„原因停止åQˆceaseåQ‰åƈ˜q›å…¥ž®ÞqÈA状态ã€?/p> <p> <wbr></p> <p>  一个新创徏的线½E‹åƈ不是自动的开始运行的åQŒå¿…™å»è°ƒç”¨å®ƒçš„start()æ–ÒŽ³•使之ž®†çº¿½E‹æ”¾å…?/p> <p>可运行态(runnable</p> <p>stateåQ‰ï¼Œ˜q™åªæ˜¯æ„å‘³ç€è¯¥çº¿½E‹å¯ä¸ºJVM的线½E‹è°ƒåº¦ç¨‹åºè°ƒåº¦è€Œä¸æ˜¯æ„å‘³ç€å®ƒå¯ä»¥ç«‹åŒ™¿è¡Œã€?/p> <p>¾U¿ç¨‹çš„调度是抢先式的åQŒè€Œä¸æ˜¯åˆ†æ—‰™—´ç‰‡å¼çš„。具有比当前˜qè¡Œ¾U¿ç¨‹é«˜ä¼˜å…ˆçñ”的线½E‹å¯ä»¥ä‹Éå½?/p> <p>前线½E‹åœæ­¢è¿è¡Œè€Œè¿›å…¥å°±¾lªçŠ¶æ€ï¼Œä¸åŒä¼˜å…ˆ¾U§çš„¾U¿ç¨‹é—´æ˜¯æŠ¢å…ˆå¼çš„åQŒè€ŒåŒ¾U§çº¿½E‹é—´æ˜¯è½®è½¬å¼çš?/p> <p>。一个线½E‹åœæ­¢è¿è¡Œå¯ä»¥æ˜¯å› äؓ不同原因åQŒå¯èƒ½æ˜¯å› äؓ更高优先¾U§çº¿½E‹çš„æŠ¢å åQŒä¹Ÿå¯èƒ½æ˜¯å› ä¸?/p> <p>调用sleep()æ–ÒŽ³•åQŒè€Œå³ä½¿æ˜¯å› äؓ抢先而停止也不一定就˜q›å…¥å¯è¿è¡Œé˜Ÿåˆ—的前面åQŒå› ä¸ºåŒ¾U§çº¿</p> <p>½E‹æ˜¯è½®æ¢å¼çš„åQŒå®ƒçš„è¿è¡Œå¯èƒ½å°±æ˜¯å› ä¸ø™½®æ¢ï¼Œè€Œå®ƒå› æŠ¢å è€Œåœæ­¢åŽåªèƒ½åœ¨è½®æ¢é˜Ÿåˆ—中排队而不</p> <p>能排在前面ã€?/p> <p> <wbr></p> <h3><a name="_Toc74451204">§1.1.9 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> ä¹?/a></h3> <p>Which two CANNOT directly cause a thread to stop executing? (Choose Two)</p> <p> <wbr></p> <p>A.Existing from a synchronized block</p> <p>B.Calling the wait method on an object</p> <p>C.Calling notify method on an object</p> <p>D.Calling read method on an InputStream object</p> <p>E.Calling the SetPriority method on a Thread object</p> <p>AnsweråQšAC。同55é¢?/p> <p> <wbr></p> <h3><a name="_Toc74451205">§1.1.10 å?/a></h3> <p> <wbr>public class SyncTest{</p> <p> <wbr>public static void main(String[] args) <wbr> {</p> <p> <wbr>final StringBuffer s1= new StringBuffer();</p> <p> <wbr>final StringBuffer s2= new StringBuffer();</p> <p> <wbr>new Thread () <wbr> <wbr> {</p> <p> <wbr> <wbr> public void run() {</p> <p> <wbr> <wbr> <wbr> <wbr> synchronized(s1) {</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> s2.append(“A”);</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> synchronized(s2) {</p> <p> <wbr> <wbr> s2.append(“B”);</p> <p> <wbr> <wbr> <wbr> <wbr> System.out.print(s1);</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> System.out.print(s2);</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> }</p> <p> <wbr> <wbr> <wbr> <wbr> }</p> <p> <wbr> <wbr> } <wbr> <wbr></p> <p> <wbr>}.start();</p> <p> <wbr>new Thread() {</p> <p> <wbr> <wbr> public void run() {</p> <p> <wbr> <wbr> <wbr> <wbr> synchronized(s2) {</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> s2.append(“C”);</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> synchronized(s1) {</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> s1.append(“D”);</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> System.out.print(s2);</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> System.out.print(s1);</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> }</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> }</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> }</p> <p> <wbr> <wbr> <wbr> }.start();</p> <p> <wbr> <wbr> }</p> <p> <wbr>}</p> <p>Which two statements are true? (Choose Two)</p> <p>A. The program prints “ABBCAD”</p> <p>B. The program prints “CDDACB”</p> <p>C. The program prints “ADCBADBC”</p> <p>D. The output is a non-deterministic point because of a possible deadlock condition</p> <p>E. The output is dependent on the threading model of the system the program is running on.</p> <p>AnsweråQšDE</p> <p> <wbr></p> <h3><a name="_Toc74451206">§1.1.11 十一</a></h3> <p>What will happen when you attempt to compile and run the following code?</p> <p>public class Test{ <wbr> <wbr></p> <p> <wbr> <wbr> int i = 0; <wbr> <wbr></p> <p> <wbr> <wbr> public static void main(String argv[]) { <wbr> <wbr> <wbr> <wbr> <wbr> <wbr></p> <p> <wbr> <wbr> <wbr> <wbr> Test t = new Test(); <wbr> <wbr> <wbr> <wbr> <wbr> <wbr></p> <p> <wbr> <wbr> <wbr> <wbr> t.myMethod(); <wbr> <wbr></p> <p> <wbr> <wbr> } <wbr> <wbr></p> <p> <wbr> <wbr> public void myMethod(){ <wbr>  <wbr> <wbr> <wbr> <wbr> <wbr></p> <p> <wbr> <wbr> <wbr> <wbr> while(true) { <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr></p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> try { <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr></p> <p> <wbr> <wbr> <wbr> <wbr>  <wbr>  <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>  <wbr> <wbr>wait(); <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr></p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> }catch (InterruptedException e) {} <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr></p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> i++; <wbr> <wbr> <wbr> <wbr> <wbr> <wbr></p> <p> <wbr> <wbr> <wbr> <wbr> } <wbr> <wbr></p> <p> <wbr> <wbr> }</p> <p>}</p> <p>A. Compile time error, no matching notify within the method.</p> <p>B. Compile and run but an infinite looping of the while method.</p> <p>C. Compilation and run without any output.</p> <p>E. Runtime Exception "IllegalMonitorStatException".</p> <p>Answer: E</p> <p>Note: The wait/notify protocol can only be used within code that is synchronized. In this case calling code does not have a lock on the object(not synchronized) and will thus cause an Exception at runtime.</p> <p> <wbr></p> <h3><a name="_Toc74451207">§1.1.12 十二</a></h3> <p>1.10 What is the result of compiling and executing the following code?</p> <p>public class ThreadTest extends Thread { <wbr></p> <p> <wbr> <wbr> <wbr> <wbr> public void run() { <wbr> <wbr> <wbr> <wbr> <wbr></p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> System.out.println("In run"); <wbr> <wbr> <wbr> <wbr> <wbr></p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> yield(); <wbr> <wbr> <wbr> <wbr></p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> System.out.println("Leaving run"); <wbr></p> <p> <wbr> <wbr> <wbr> <wbr> } <wbr> <wbr></p> <p> <wbr> <wbr> <wbr> <wbr> public static void main(String args []) { <wbr> <wbr></p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> (new ThreadTest()).start(); <wbr></p> <p> <wbr> <wbr> <wbr> <wbr> }</p> <p>}</p> <p>A. The code fails to compile in the main() method.</p> <p>B. The code fails to compile in the run() method.</p> <p>C. Only the text "In run" will be displayed.</p> <p>D. The text "In run" followed by "Leaving run" will be displayed.</p> <p>E. The code compiles correctly, but nothing is displayed.</p> <p>Answer: D</p> <p> <wbr></p> <h3><a name="_Toc74451208">§1.1.13 十三</a></h3> <p>Which of the following will definitely stop a thread from executing?A. wait()B. notify()C. yield()D. suspend()E. sleep()Answer: ACDE</p> <p> <wbr></p> <h3><a name="_Toc74451209">§1.1.14 十四</a></h3> <p>Which of the following will definitely stop a thread from executing?</p> <p>A. wait()</p> <p>B. notify()</p> <p>C. yield()</p> <p>D. suspend()</p> <p>E. sleep()</p> <p>Answer: ACDE</p> <p> <wbr></p> <h3><a name="_Toc74451210">§1.1.15 十五</a></h3> <p> <wbr>Which of the following statements about threading are true?</p> <p>A. You can only obtain a mutually exclusive lock on methods in a class that extends Thread or implements runnable.</p> <p>B. You can obtain a mutually exclusive lock on any object.</p> <p>C. You can't obtain a mutually exclusive lock on any object.</p> <p>D. Thread scheduling algorithms are platform dependent.</p> <p>Answer: BD</p> <p>8:</p> <p>Consider the following statement: <wbr> <wbr></p> <p>Thread myThread = new Thread();</p> <p>Which of the following statements are true regarding myThread?</p> <p>A. The thread myThread is now in a runnable state.</p> <p>B. The thread myThread has a priority of 5.</p> <p>C. On calling the start() method on myThread, the run method in the Thread class will be executed.</p> <p>D. On calling the start() method on myThread, the run method in the calling class will be executed.</p> <p>Answer: C</p> <p>Note: the priority of myThread will be inherited from the Thread that called the constructor.</p> <h3><a name="_Toc74451211">§1.1.16 十六</a></h3> <p>What is the effect of issuing a wait() method on an objectåQŸï¼ˆMutipleåQ?/p> <p>1) If a notify() method has already been sent to that object then it has no effect</p> <p>2) The object issuing the call to wait() will halt until another object sends a notify() or notifyAll() method</p> <p>3) An exception will be raised</p> <p>4) The object issuing the call to wait() will be automatically synchronized with any other objects using the receiving object.</p> <p>ANSWER 1)</p> <p>10:</p> <p>Pick all the true statements below.</p> <p>1) If a thread wants to call wait() on an object, the thread must own that object's lock.</p> <p>2) There is a method that you can call on an instance of the Thread class that puts the instance to sleep for a specified <wbr> <wbr> <wbr> number of milliseconds.</p> <p>3) At the moment when a thread is notified, it automatically gets the lock of the object for which it was waiting.</p> <p>ANSWER 1</p> <p> <wbr></p> </div> <img src ="http://www.aygfsteel.com/balajinima/aggbug/298538.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/balajinima/" target="_blank">李云æ³?/a> 2009-10-16 11:43 <a href="http://www.aygfsteel.com/balajinima/articles/298538.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>½W¬å…«éƒ¨åˆ†åQšåœ¨java.lang包中的基¼‹€¾c?/title><link>http://www.aygfsteel.com/balajinima/articles/298539.html</link><dc:creator>李云æ³?/dc:creator><author>李云æ³?/author><pubDate>Fri, 16 Oct 2009 03:43:00 GMT</pubDate><guid>http://www.aygfsteel.com/balajinima/articles/298539.html</guid><wfw:comment>http://www.aygfsteel.com/balajinima/comments/298539.html</wfw:comment><comments>http://www.aygfsteel.com/balajinima/articles/298539.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/balajinima/comments/commentRss/298539.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/balajinima/services/trackbacks/298539.html</trackback:ping><description><![CDATA[<p align="left">½W¬å…«éƒ¨åˆ†åQšåœ¨java.lang包中的基¼‹€¾c?/p> <ul type="disc"> <li>能够应用Math¾cÖM¸­çš„abs,ceil,floor,max,min,random,round,sin,cos,tan,sqrtæ–ÒŽ³•ã€? <li>正确理解String¾cÖM¸å¯æ”¹å˜çš„æ„ä¹‰ã€? <li>当执行一ŒD늨‹åºï¼Œä¸­é—´åŒ…含有wrapper¾cȝš„一个实例,知道它运行的前提条äšg˜qè¡Œ¾l“果。能用下面wrapper¾c»ï¼ˆä¾‹å¦‚Integer,Double,½{‰ç­‰åQ‰çš„æ–ÒŽ³•来写½E‹åºåQ?</li> </ul> <p align="left"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> doublevalue</p> <p align="left"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> floatvalue</p> <p align="left"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> intvalue</p> <p align="left"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> longvalue</p> <p align="left"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> parseXxx</p> <p align="left"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> getXxx</p> <p align="left"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> toString</p> <p align="left"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> toHexString</p> <img src ="http://www.aygfsteel.com/balajinima/aggbug/298539.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/balajinima/" target="_blank">李云æ³?/a> 2009-10-16 11:43 <a href="http://www.aygfsteel.com/balajinima/articles/298539.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>½W¬å…­éƒ¨åˆ†åQšè¦†ç›–,重蝲åQŒè¿è¡Œæ—¶æœŸç±»åž‹åŠå…‰™¢å‘对è±?/title><link>http://www.aygfsteel.com/balajinima/articles/298537.html</link><dc:creator>李云æ³?/dc:creator><author>李云æ³?/author><pubDate>Fri, 16 Oct 2009 03:42:00 GMT</pubDate><guid>http://www.aygfsteel.com/balajinima/articles/298537.html</guid><wfw:comment>http://www.aygfsteel.com/balajinima/comments/298537.html</wfw:comment><comments>http://www.aygfsteel.com/balajinima/articles/298537.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/balajinima/comments/commentRss/298537.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/balajinima/services/trackbacks/298537.html</trackback:ping><description><![CDATA[<p align="left">½W¬å…­éƒ¨åˆ†åQšè¦†ç›–,重蝲åQŒè¿è¡Œæ—¶æœŸç±»åž‹åŠå…‰™¢å‘对è±?/p> <ul type="disc"> <li>知道面向对象设计中封装的好处以及如何实现åQŒèƒ½çŸ¥é“is <wbr>aå’Œhas <wbr>a的意义ã€? <li>能正¼‹®ä‹É用覆盖和重蝲的方法,能正¼‹®è°ƒç”¨çˆ¶¾cÀLˆ–重蝲的构造方æ³?constructor)åQŒçŸ¥é“调用这些方法后的结果ã€? <li>能正¼‹®å®žä¾‹åŒ–¾cÀLˆ–内部¾c?</li> </ul> <div> <h3><a name="_Toc74451188">§1.1.1 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 一</a></h3> <p>3. What will happen when you attempt to compile and run the following code?</p> <p>class MyParent</p> <p>{</p> <p>int x, y;</p> <p>MyParent(int x, int y)</p> <p>{</p> <p>this.x = x;</p> <p>this.y = y;</p> <p>}</p> <p>public int addMe(int x, int y)</p> <p>{</p> <p>return this.x + x + y + this.y;</p> <p>}</p> <p>public int addMe(MyParent myPar)</p> <p>{</p> <p>return addMe(myPar.x, myPar.y);</p> <p>}</p> <p>}</p> <p>class MyChild extends MyParent</p> <p>{</p> <p>int z;</p> <p>MyChild (int x, int y, int z)</p> <p>{</p> <p>super(x,y);</p> <p>this.z = z;</p> <p>}</p> <p>public int addMe(int x, int y, int z)</p> <p>{</p> <p>return this.x + x + this.y + y + this.z + z;</p> <p>}</p> <p>public int addMe(MyChild myChi)</p> <p>{</p> <p>return addMe(myChi.x, myChi.y, myChi.z);</p> <p>}</p> <p>public int addMe(int x, int y)</p> <p>{</p> <p>return this.x + x + this.y + y;</p> <p>}</p> <p>}</p> <p>public class MySomeOne</p> <p>{</p> <p>public static void main(String args[])</p> <p>{</p> <p>MyChild myChi = new MyChild(10, 20, 30);</p> <p>MyParent myPar = new MyParent(10, 20);</p> <p>int x = myChi.addMe(10, 20, 30);</p> <p>int y = myChi.addMe(myChi);</p> <p>int z = myPar.addMe(myPar);</p> <p>System.out.println(x + y + z);</p> <p>}</p> <p>}</p> <p> <wbr></p> <p>Choices:</p> <p>a. 300</p> <p>b. 240</p> <p>c. 120</p> <p>d. 180</p> <p>e. Compilation error</p> <p>f. None of the above</p> <p>―――――――――――â€?/p> <p>A is the correct choice. In the code, MyChild class overrides the addMe(int x, int y) method of the MyParent class. And in both the MyChild and MyParent class, addMe() method is overloaded. There is no compilation error anywhere in the above code. On execution, first, the object of MyChild class will be constructed. Please note that there is a super() call from the constructor of MyChild class, which will call the constructor of MyParent class. This will cause the value of z variable of MyChild class to be 30 and x, y variables of MyParent class will become 10 and 20 respectively. The next statement will again call the constructor of MyParent class with same x and y values. This is followed by execution of addMe() method of MyChild class with x as 10, y as 20 and z as 30. Also x and y are inherited by MyChild class from the MyParent class. Thus in the addMe() method of the MyChild class, the value of this.x will be 10, this.y will be 20 and this.z will be 30. The return value of this method will be "10 + 10 + 20 + 20 + 30 + 30", which is equal to 120. Thus x will become 120. This is followed by the invocation of the other addMe() method which takes object reference of the MyChild class. From this method, the method which was called earlier is invoked. This call is exactly the same as the earlier one. Thus the value of y will also be 120 like x. Now the addMe() method of MyParent class is invoked. This method invokes another addMe() method of the same class. Its equivalent to the invocation of addMe(int x, int y) method with x as 10 and y as 20. Also the value of instance variables x and y of My Parent class is 10 and 20 respectively. The value of z will be evaluated to "10 + 10 + 20 + 20", which is equal to 60. Thus the value of x, y and z after all the invocations will be 120, 120 and 60 respectively. As a result of this finally, "120 + 120 + 60" which is equal to 300 will be printed. Thus A is the correct choice.</p> <p align="left"> <wbr></p> <h3><a name="_Toc74451189">§1.1.2 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> äº?/a></h3> <p>Given the code below, and making no other changes, which access modifiers</p> <p>(public, protected or private) can legally be placed before myMethod() on line 3?</p> <p>If line 3 is left as it is, which keywords can legally be placed before myMethod</p> <p>on line 8?</p> <p>1.class HumptyDumpty</p> <p>2.{</p> <p>3.void myMethod() {}</p> <p>4.}</p> <p>5.</p> <p>6.class HankyPanky extends HumptyDumpty</p> <p>7.{</p> <p>8.void myMethod() {}</p> <p>9.}</p> <div> <p>Choices:</p> <p>a. private or nothing(i.e. leaving it as it is) on line 3.</p> <p>Nothing(i.e. leaving it as it is) or protected or public</p> <p>on line 8.</p> <p>b. public or protected on line 3. private or nothing(i.e. leaving it</p> <p>as it is) on line 8.</p> <p>c. nothing(i.e. leaving it as it is) or protected or public on</p> <p>line 3. private or nothing(i.e. leaving it as it is) on line 8.</p> <p>d. None of the above.</p> </div> <p>A is correct. The basic principle is that a method cannot be overridden to be more private. Since the method is being overridden to be friendly(default modifier) it can only be private or friendly in the superclass. Secondly if the method in superclass is left as it is(i.e. friendly access) the method in subclass can be friendly, protected or public.</p> <h3><a name="_Toc74451190">§1.1.3 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> ä¸?/a></h3> <p>What results from the following code?</p> <p>1.class MyClass</p> <p>2.{</p> <p>3.void myMethod(int i) {System.out.println("int version");}</p> <p>4.void myMethod(String s) {System.out.println("String version");}</p> <p>5.public static void main(String args[])</p> <p>6.{</p> <p>7.MyClass obj = new MyClass();</p> <p>8.char ch = 'c';</p> <p>9.obj.myMethod(ch);</p> <p>10.}</p> <p>11.}</p> <p>Choices:</p> <p>a. Line 4 will not compile as void methods can't be overridden.</p> <p>b. An exception at line 9.</p> <p>c. Line 9 will not compile as there is no version of myMethod which takes a char as argument.</p> <p>d. The code compiles and produces output: int version.</p> <p>e. The code compiles and produces output: String version.</p> <p>―――――――――â€?/p> <p>D is correct. A is incorrect as void methods can be overridden without any problem. B is incorrect as char ch declaration is valid. C is incorrect as char type in java is internally stored as integer and there is a method which takes int as an input. D is correct, on line 9 char ch is widened to an int and passed to int version of the myMethod(). E is incorrect as int version of myMethod() is called.</p> <h3><a name="_Toc74451191">§1.1.4 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> å›?/a></h3> <p>What is displayed when the following is executed?</p> <p>class Parent</p> <p>{</p> <p>private void method1()</p> <p>{</p> <p>System.out.println("Parent's method1()");</p> <p>}</p> <p>public void method2()</p> <p>{</p> <p>System.out.println("Parent's method2()");</p> <p>method1();</p> <p>}</p> <p>}</p> <p>class Child extends Parent</p> <p>{</p> <p>public void method1()</p> <p>{</p> <p>System.out.println("Child's method1()");</p> <p>}</p> <p>public static void main(String args[])</p> <p>{</p> <p>Parent p = new Child();</p> <p>p.method2();</p> <p>}</p> <p>}</p> <p>Choices:</p> <p>a. Compile time error</p> <p>b. Run time error</p> <p>c. prints : Parent's method2()</p> <p> <wbr>  <wbr>  <wbr>  <wbr>  <wbr> Parent's method1()</p> <p>d. prints : Parent's method2()</p> <p> <wbr>  <wbr>  <wbr>  <wbr>  <wbr> Child's method1()</p> <p align="left">――――――â€?/p> <p>C is correct. The code will compile without any error and also will not give any run time error. The variable p refers to the Child class object. The statement p.method2() on execution will first look for method2() in Child class. Since there is no method2() in child class, the method2() of Parent class will be invoked and thus "Parent's method2()" will be printed. Now from the method2() , there is a call to method1(). Please note that method1() of Parent class is private, because of which the same method (method1() of Parent class) will be invoked. Had this method(method1() of Parent class) been public/protected/friendly (default), Child's class method1() would be called. Thus C is correct answer.</p> <p> <wbr></p> <h3><a name="_Toc74451192">§1.1.5 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> äº?/a></h3> <p>What is displayed when the following is executed?</p> <p>class Parent{</p> <p>private void method1(){</p> <p>System.out.println("Parent's method1()");</p> <p>}</p> <p>public void method2(){</p> <p>System.out.println("Parent's method2()");</p> <p>method1();</p> <p>}</p> <p>}</p> <p>class Child extends Parent{</p> <p>public void method1(){</p> <p>System.out.println("Child's method1()");</p> <p>}</p> <p>public static void main(String args[]){</p> <p>Parent p = new Child();</p> <p>p.method2();</p> <p>}</p> <p>}</p> <p> <wbr></p> <p>A. compile time error</p> <p>B. run time error</p> <p>C. prints: parent’s method2() <wbr> parent’s method1()</p> <p>D. prints: parent’s method2() <wbr> child’s method1()</p> <p> <wbr></p> <p>C is correct. The code will compile without any error and also will not give any run time error. The variable p refers to the Child class object. The statement p.method2() on execution will first look for method2() in Child class. Since there is no method2() in child class, the method2() of Parent class will be invoked and thus "Parent's method2()" will be printed. Now from the method2() , there is a call to method1(). Please note that method1() of Parent class is private, because of which the same method (method1() of Parent class) will be invoked. Had this method(method1() of Parent class) been public/protected/friendly (default), Child's class method1() would be called. Thus C is correct answer.</p> <p> <wbr></p> <h3><a name="_Toc74451193">§1.1.6 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> å…?/a></h3> <p>  1) class Person {</p> <p> <wbr></p> <p>  2) public void printValue(int i, int j) { }</p> <p> <wbr></p> <p>  3) public void printValue(int i){ }</p> <p> <wbr></p> <p>  4) }</p> <p> <wbr></p> <p>  5) public class Teacher extends Person {</p> <p> <wbr></p> <p>  6) public void printValue() { }</p> <p> <wbr></p> <p>  7) public void printValue(int i) {}</p> <p> <wbr></p> <p>  8) public static void main(String args[]){</p> <p> <wbr></p> <p>  9) Person t = new Teacher();</p> <p> <wbr></p> <p>  10) t.printValue(10);</p> <p> <wbr></p> <p>  11) }</p> <p> <wbr></p> <p>  12) }</p> <p> <wbr></p> <p>  Which method will the statement on line 10 call?</p> <p>  A. on line 2</p> <p> <wbr></p> <p>  B. on line 3</p> <p> <wbr></p> <p>  C. on line 6</p> <p> <wbr></p> <p>  D. on line 7</p> <p>  ¾˜»è¯‘</p> <p>  ½W¬åè¡Œçš„声明ž®†è°ƒç”¨å“ªäº›æ–¹æ³•ã€?/p> <p> <wbr></p> <p>  ½{”案 D</p> <p> <wbr></p> <p>  解析 变量t是一个Person对象åQŒä½†æ˜¯å®ƒæ˜¯ç”¨Teacher实例化的åQŒè¿™ä¸ªé—®é¢˜æ¶‰åŠåˆ°javaçš?/p> <p>¾~–译时多态和˜qè¡Œæ—¶å¤šæ€çš„问题åQŒå°±¾~–译时多态来è¯ß_¼Œt实际上是一个Person¾c»ï¼Œ˜q™æ¶‰åŠåˆ°¾c?/p> <p>型的自动转换åQˆå°†ä¸€ä¸ªå­¾cȝš„实例赋值给一个父¾cȝš„变量是不用进行强制类型è{换,反之则需</p> <p>要进行强制类型è{换,而且被赋值的变量实际上应该是一个子¾cȝš„对象åQ‰ï¼Œå¦‚果对t调用了子</p> <p>¾cÖM¸­æ–°å¢žçš„æ–¹æ³•则造成¾~–译旉™”™è¯¯ç¼–译将不能通过åQŒè€Œåœ¨˜qè¡Œæ—Óž¼Œ˜qè¡Œæ—¶ç³»¾lŸå°†æ ÒŽ®t实际æŒ?/p> <p>向的¾cÕdž‹è°ƒç”¨å¯¹åº”的方法,对于本例来说åQŒt.print(10)ž®†è°ƒç”¨t实际指向的Teacher¾cȝš„对应</p> <p>æ–ÒŽ³•。在java中,可以用一个子¾cȝš„实例实例化父¾cȝš„一个变量,而变量在¾~–译时是一个父¾c?/p> <p>实例åQŒåœ¨˜qè¡Œæ—¶å¯èƒ½æ˜¯ä¸€ä¸ªå­¾cÕd®žä¾‹ã€?/p> <p> <wbr></p> <h3><a name="_Toc74451194">§1.1.7 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> ä¸?/a></h3> <p>35、public class Parent {</p> <p>  public int addValue( int a, int b) {</p> <p>  int s;</p> <p>  s = a+b;</p> <p>  return s;</p> <p>  }</p> <p>  }</p> <p>  class Child extends Parent {</p> <p> <wbr></p> <p>  }</p> <p>  Which methods can be added into class Child?</p> <p>  A. int addValue( int a, int b ){// do something...}</p> <p> <wbr></p> <p>  B. public void addValue (){// do something...}</p> <p> <wbr></p> <p>  C. public int addValue( int a ){// do something...}</p> <p> <wbr></p> <p>  D. public int addValue( int a, int b )throws MyException {//do</p> <p>something...}</p> <p> <wbr></p> <p>  (bc)</p> <p> <wbr></p> <p>  题目åQšå“ªäº›æ–¹æ³•可以加入类Child中ã€?/p> <p> <wbr></p> <p>  此题涉及æ–ÒŽ³•重蝲åQˆoverloadåQ‰ï¼Œæ–ÒŽ³•重写åQˆoverrideåQ‰ä»¥åŠç±»‹z„¡”Ÿæ—¶æ–¹æ³•重写的规则</p> <p>。方法重载的规则是:一、参数列表必™åÖM¸åŒï¼Œä¸ªæ•°çš„不同完全可以,如果个数相同则参数类</p> <p>型的不同不能引è“v歧意åQŒä¾‹å¦‚int</p> <p>å’Œlong,floatå’Œdoublež®×ƒ¸èƒ½ä½œä¸ºå”¯ä¸€çš„类型不同;二、返回值可以不同,但是不能是重è½?/p> <p>时唯一的不同点åQˆè¿™ç‚¹å’Œc++中不同,c++中返回类型必™åÖM¸€è‡ß_¼‰ã€‚方法重写发生在¾cȝ‘ô承时åQ?/p> <p>子类可以重写一个父¾cÖM¸­å·²æœ‰çš„æ–¹æ³•,必须在返回类型和参数列表一æ äh—¶æ‰èƒ½è¯´æ˜¯é‡å†™åQŒå¦åˆ?/p> <p>ž®±æ˜¯é‡è²åQŒjava中方法重写的一个重要而且å®ÒŽ˜“被忽略的规则是重写的æ–ÒŽ³•的访问权限不能比</p> <p>被重写的æ–ÒŽ³•的访问权限低åQé‡å†™çš„另一个规则是重写的方法不能比被重写的æ–ÒŽ³•抛弃(thro</p> <p>ws)更多¿Uç±»çš„异常,其抛弃的异常只能ž®‘,或者是其子¾c»ï¼Œä¸èƒ½ä»¥æŠ›å¼ƒå¼‚常的个数来判断种</p> <p>¾c»ï¼Œè€Œåº”该是异常¾cÕd±‚‹Æ¡ç»“果上的种¾c…R€‚此题中½{”案a的错误就是重写的讉K—®æƒé™æ¯”被重写çš?/p> <p>æ–ÒŽ³•的低åQŒè€Œb,c都属于重载,d的错误在于比被重写的æ–ÒŽ³•抛弃了更多种¾cȝš„异常ã€?/p> </div> <img src ="http://www.aygfsteel.com/balajinima/aggbug/298537.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/balajinima/" target="_blank">李云æ³?/a> 2009-10-16 11:42 <a href="http://www.aygfsteel.com/balajinima/articles/298537.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>½W¬äº”部分åQšæ“ä½œä¸Žèµ‹å€?/title><link>http://www.aygfsteel.com/balajinima/articles/298536.html</link><dc:creator>李云æ³?/dc:creator><author>李云æ³?/author><pubDate>Fri, 16 Oct 2009 03:41:00 GMT</pubDate><guid>http://www.aygfsteel.com/balajinima/articles/298536.html</guid><wfw:comment>http://www.aygfsteel.com/balajinima/comments/298536.html</wfw:comment><comments>http://www.aygfsteel.com/balajinima/articles/298536.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/balajinima/comments/commentRss/298536.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/balajinima/services/trackbacks/298536.html</trackback:ping><description><![CDATA[<p align="left">½W¬äº”部分åQšæ“ä½œä¸Žèµ‹å€?/p> <ul type="disc"> <li>能知道各¿Uæ“ä½œç¬¦åQˆåŒ…括赋值操作符和intanceof操作½W¦ï¼‰åº”用于不同类型的¾l“æžœã€? <li>知道StringåQŒBoolean和类使用equals()æ–ÒŽ³•后的¾l“æžœã€? <li>知道当对已经知道值的变量˜q›è¡Œ&,|,&&,||操作æ—Óž¼Œå“ªäº›æ“ä½œæ•°è¢«˜qç®—了,表达式最¾lˆçš„¾l“果是怎样的ã€? <li>知道对象和原始类型数据传入方法后åQŒåœ¨æ–ÒŽ³•内部˜q›è¡Œèµ‹å€¼æˆ–其它修改操作的结果ã€?</li> </ul> <div> <h3>§1.1.1 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 一</h3> <p>Which of the following lines will print false?</p> <p>1.public class MyClass</p> <p>2.{</p> <p>3.static String s1 = "I am unique!";</p> <p>4.public static void main(String args[])</p> <p>5.{</p> <p>6.String s2 = "I am unique!";</p> <p>7.String s3 = new String(s1);</p> <p>8.System.out.println(s1 == s2);</p> <p>9.System.out.println(s1.equals(s2));</p> <p>10.System.out.println(s3 == s1);</p> <p>11.System.out.println(s3.equals(s1));</p> <p>12.System.out.println(TestClass.s4 == s1);</p> <p>13.}</p> <p>14.}</p> <p>15.</p> <p>16.class TestClass</p> <p>17.{</p> <p>18.static String s4 = "I am unique!";</p> <p>19.}</p> <p>Choices:</p> <p>a. Lines 10 and 12</p> <p>b. Line 12 only</p> <p>c. Line 8 and 10</p> <p>d. None of these</p> <p align="left">―――――――――â€?/p> <p align="left">D is correct. Only line 10 will print false. Strings are immutable objects. That is, a string is read only once the string has been created and initialized, and Java optimizes handling of string literals; only one anonymous string object is shared by all string literals with the same contents. Hence in the above code the strings s1, s2 and s4 refer to the same anonymous string object, initialized with the character string: "I am unique!". Thus s1 == s2 and TestClass.s4 will both return true and obviously s1.equals(s2) will return true. But creating string objects using the constructor String(String s) creates a new string, hence s3 == s1 will return false even though s3.equals(s1) will return true because s1 and s3 are referring to two different string objects whose contents are same.</p> <p> <wbr></p> <h3><a name="_Toc74451179">§1.1.2 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> äº?/a></h3> <p>What is displayed when the following code is compiled and executed?</p> <p>String s1 = new String("Test");</p> <p>String s2 = new String("Test");</p> <p>if (s1==s2)</p> <p> <wbr>  <wbr>  <wbr>  <wbr>  <wbr>System.out.println("Same");</p> <p>if (s1.equals(s2))</p> <p> <wbr>  <wbr>  <wbr>  <wbr>  <wbr>System.out.println("Equals");</p> <p>Choices:</p> <p>a. Same</p> <p>Equals</p> <p>b. Equals</p> <p>c. Same</p> <p>d. The code compiles, but nothing is displayed upon execution.</p> <p>e. The code fails to compile.</p> <div> <p align="left"> <wbr></p> </div> <p align="left">B is correct. Here s1 and s2 are two different object references, referring to different objects in memory. Please note that operator == checks for the memory address of two object references being compared and not their value. The "equals()" method of String class compares the values of two Strings. Thus s1==s2 will return "false" while s1.equals(s2) will return "true". Thus only "Equals" will be printed.</p> <h3>§1.1.3 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> ä¸?/h3> <p>Given the following code, what will be the output?</p> <p>class Value</p> <p>{</p> <p>public int i = 15;</p> <p>}</p> <p>public class Test</p> <p>{</p> <p>public static void main(String argv[])</p> <p>{</p> <p> <wbr>  <wbr>  <wbr>  <wbr> Test t = new Test();</p> <p>t.first();</p> <p> <wbr>  <wbr>}</p> <p>public void first()</p> <p>{</p> <p> <wbr>  <wbr>  <wbr> int i = 5;</p> <p> <wbr>  <wbr>  <wbr>  <wbr> Value v = new Value();</p> <p>v.i = 25;</p> <p>second(v, i);</p> <p> <wbr>  <wbr> System.out.println(v.i);</p> <p>}</p> <p>public void second(Value v, int i)</p> <p>{</p> <p>i = 0;</p> <p> <wbr>  <wbr>  <wbr>  <wbr> v.i = 20;</p> <p>Value val = new Value();</p> <p> <wbr>  <wbr>  <wbr>  <wbr> v =  <wbr>val;</p> <p> <wbr>  <wbr> System.out.println(v.i + " " + i);</p> <p> <wbr>  <wbr>  <wbr>  <wbr>  <wbr>  <wbr>  <wbr>  <wbr></p> <p>}</p> <p>}</p> <p>Choices:</p> <p>a. 15 0</p> <p> <wbr>  <wbr>20</p> <p>b. 15 0</p> <p> <wbr>  <wbr>15</p> <p>c. 20 0</p> <p> <wbr> 20</p> <p>d. 0 15</p> <p> <wbr> 20</p> <p align="left">――――――――――――――â€?/p> <p align="left">A is correct. When we pass references in Java what actually gets passed is the value of that reference (i.e. memory address of the object being referenced and not the actual object referenced by that reference) and it gets passed as value (i.e a copy of the reference is made). Now when we make changes to the object referenced by that reference it reflects on that object even outside of the method being called but any changes made to the reference itself is not reflected on that reference outside of the method which is called. In the example above when the reference v is passed from method first() to second() the value of v is passed. When we assign the value val to v it is valid only inside the method second() and thus inside the method second() what gets printed is 15 (initial value of i in the object referenced by val), then a blank space and then 0 (value of local variable i). After this when we return to the method first() v actually refers to the same object to which it was referring before the method second() was called, but one thing should be noted here that the value of i in that object (referred by v inside the method first()) was changed to 20 in the method second() and this change does reflect even outside the method second(), hence 20 gets printed in the method first(). Thus overall output of the code in consideration is 15 0 20</p> <p> <wbr></p> <h3>§1.1.4 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> å›?/h3> <p> <wbr>What will happen when you attempt to compile and run the following code?</p> <p>interface MyInterface</p> <p>{</p> <p>}</p> <p>public class MyInstanceTest implements MyInterface</p> <p>{</p> <p>static String s;</p> <p>public static void main(String args[])</p> <p>{</p> <p>MyInstanceTest t = new MyInstanceTest();</p> <p>if(t instanceof MyInterface)</p> <p>{</p> <p>System.out.println("I am true interface");</p> <p>}</p> <p>else</p> <p>{</p> <p>System.out.println("I am false interface");</p> <p>}</p> <p>if(s instanceof String)</p> <p>{</p> <p>System.out.println("I am true String");</p> <p>}</p> <p>else</p> <p>{</p> <p>System.out.println("I am false String");</p> <p>}</p> <p>}</p> <p>}</p> <p>Choices:</p> <p>a. Compiletime error</p> <p>b. Runtime error</p> <p>c. Prints : "I am true interface" followed by " I am true String"</p> <p>d. Prints : "I am false interface" followed by " I am false String"</p> <p>e. Prints : "I am true interface" followed by " I am false String"</p> <p>f. Prints : "I am false interface" followed by " I am true String"</p> <p>―――――――――――â€?/p> <p>E is the correct choice. The "instanceof" operator tests the class of an object at runtime. It returns true if the class of the left-hand argument is the same as, or is some subclass of, the class specified by the right-hand operand. The right-hand operand may equally well be an interface. In such a case, the test determines if the object at left-hand argument implements the specified interface. In the above case there will not be any compiletime or runtime error. The result of "t instance of MyInterface" will be true as "t" is the object of MyInstanceTest class which implements the MyInstance interface. But the result of "s instanceof String" will be false as "s" refers to null. Thus the output of the above program will be : "I am true interface" followed by " I am false String". Thus choice E is correct and others are incorrect.</p> <p> <wbr></p> <p> <wbr></p> <h3>§1.1.5 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>  <wbr>äº?/h3> <p>What will happen when you attempt to compile and run the following code snippet?</p> <p>String str = "Java";</p> <p>StringBuffer buffer = new StringBuffer(str);</p> <p>if(str.equals(buffer)){</p> <p>System.out.println("Both are equal");</p> <p>}else{</p> <p>System.out.println("Both are not equal");</p> <p>}</p> <p>A. it will print – both are not equal</p> <p>B. it will print – both are equal</p> <p>C. compile time error</p> <p>D. Runtime error</p> <p> <wbr></p> <p>A is the correct choice. The equals method overridden in String class returns true if and only if the argument is not null and is a String object that represents the same sequence of characters as this String object. Hence, though the contents of both str and buffer contain "Java", the str.equals(buffer) call results in false.</p> <p>The equals method of Object class is of form -public boolean equals(Object anObject). Hence, comparing objects of different classes will never result in compile time or runtime error.</p> <p> <wbr></p> <h3><a name="_Toc74451183">§1.1.6 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> å…?/a></h3> <p>10. Which of the following statements are true?</p> <p>  A. The equals() method determines if reference values refer to the same</p> <p>object.</p> <p> <wbr></p> <p>  B. The == operator determines if the contents and type of two separate</p> <p>objects match.</p> <p> <wbr></p> <p>  C. The equals() method returns true only when the contents of two</p> <p>objects match.</p> <p> <wbr></p> <p>  D. The class File overrides equals() to return true if the contents and</p> <p>type of two separate objects match.</p> <p>  ¾˜»è¯‘</p> <p>  下面的哪些叙˜qîCؓ真ã€?/p> <p>  A. equals()æ–ÒŽ³•判定引用值是否指向同一对象ã€?/p> <p> <wbr></p> <p>  B. == 操作½W¦åˆ¤å®šä¸¤ä¸ªåˆ†ç«‹çš„对象的内容和¾cÕdž‹æ˜¯å¦ä¸€è‡´ã€?/p> <p> <wbr></p> <p>  C. equals()æ–ÒŽ³•只有在两个对象的内容一致时˜q”回trueã€?/p> <p> <wbr></p> <p>  D. ¾c»File重写æ–ÒŽ³•equals()在两个分立的对象的内容和¾cÕdž‹ä¸€è‡´æ—¶˜q”回trueã€?/p> <p> <wbr></p> <p>  ½{”案 A,D</p> <p> </p> <p>  解析 严格来说˜q™ä¸ªé—®é¢˜çš„答案是不确定的åQŒå› ä¸ºequals()æ–ÒŽ³•是可以被重蝲的,但是</p> <p>按照java语言的本意来è¯ß_¼šå¦‚果没有重写åQˆoverrideåQ‰æ–°¾cȝš„equals()åQŒåˆ™è¯¥æ–¹æ³•å’Œ</p> <p>==</p> <p>操作½W¦ä¸€æ ·åœ¨ä¸¤ä¸ªå˜é‡æŒ‡å‘同一对象时返回真åQŒä½†æ˜¯java推荐的是使用equals()æ–ÒŽ³•来判æ–?/p> <p>两个对象的内å®ÒŽ˜¯å¦ä¸€æ øP¼Œž®±åƒString¾cȝš„equals()æ–ÒŽ³•所做的那样åQšåˆ¤å®šä¸¤ä¸ªString对象çš?/p> <p>内容是否相同åQŒè€?=操作½W¦è¿”回true的唯一条äšg是两个变量指向同一对象。从˜q™ä¸ªæ„ä¹‰ä¸Šæ¥è¯?/p> <p>选择¾l™å®šçš„答案。从更严格的意义来说正确½{”案应该只有dã€?/p> <p> <wbr></p> <h3><a name="_Toc74451184">§1.1.7 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> ä¸?/a></h3> <p>Use the operators "<<", ">>", which statements are true?</p> <p>  A. 0000 0100 0000 0000 0000 0000 0000 0000<<5 gives</p> <p> <wbr> <wbr>   1000 0000 0000 0000 0000 0000 0000 0000</p> <p> <wbr></p> <p>  B. 0000 0100 0000 0000 0000 0000 0000 0000<<5 gives</p> <p> <wbr> <wbr>   1111 1100 0000 0000 0000 0000 0000 0000</p> <p> <wbr></p> <p>  C. 1100 0000 0000 0000 0000 0000 0000 0000>>5 gives</p> <p> <wbr> <wbr> <wbr> <wbr>  1111 1110 0000 0000 0000 0000 0000 0000</p> <p> <wbr></p> <p>  D. 1100 0000 0000 0000 0000 0000 0000 0000>>5 gives</p> <p> <wbr> <wbr>   0000 0110 0000 0000 0000 0000 0000 0000</p> <p>  ¾˜»è¯‘</p> <p>  使用"<<"å’?">>"操作½W¦çš„哪些陈述是对的ã€?/p> <p> <wbr></p> <p>  ½{”案 A,C</p> <p>  </p> <p>  解析 Java的移位操作符一共有三种åQŒåˆ†åˆ«æ˜¯”>>”,”>>>”,”<<”,执行的操作分åˆ?/p> <p>是有½W¦å·å³ç§»åQŒæ— ½W¦å·å³ç§»åQŒå·¦¿U»ï¼Œæœ‰ç¬¦å·å³¿UÈš„æ„æ€æ˜¯è¯´ç§»å…¥çš„æœ€é«˜ä½å’ŒåŽŸæœ€é«˜ç¬¦å·ä½ç›¸åŒ</p> <p>åQŒæ— ½W¦å·å³ç§»æ˜¯ç§»å…¥ä½å§‹ç»ˆè¡¥é›¶åQŒå·¦¿UÀL—¶æœ€ä½Žä½å§‹ç»ˆè¡¥é›¶åQŒæœ€é«˜ä½è¢«èˆå¼ƒã€‚移位操作符另一ä¸?/p> <p>非常值得注意的特ç‚ÒŽ˜¯å…¶å³æ“ä½œæ•°æ˜¯å–模˜qç®—的,意思是说对于一个int型数据而言åQŒå¯¹å®ƒç§»</p> <p>ä½?2位的¾l“果是保持不变而非变成é›Óž¼Œå»I¼ša>>32的结果是a而不æ˜?åQŒåŒç†ï¼Œå¯¹long型数是对</p> <p>åÏx“ä½œæ•°å?4的模åQŒa>>64==aåQ›è¿˜æœ‰ä¸€ç‚šwœ€è¦æ³¨æ„çš„æ˜¯ç§»ä½æ“ä½œç¬¦”>>>”只对int型和longåž?/p> <p>有效åQŒå¯¹byte或者shortçš„æ“ä½œå°†å¯ÆD‡´è‡ªåЍ¾cÕdž‹è½¬æ¢åQŒè€Œä¸”是带½W¦å·çš„ã€?/p> <p> <wbr></p> <h3><a name="_Toc74451185">§1.1.8 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> å…?/a></h3> <p>  String s= "hello";</p> <p>  String t = "hello";</p> <p>  char c[] = {'h','e','l','l','o'} ;</p> <p>  Which return true?</p> <p>  A. s.equals(t);</p> <p> <wbr></p> <p>  B. t.equals(c);</p> <p> <wbr></p> <p>  C. s==t;</p> <p> <wbr></p> <p>  D. t.equals(new String("hello"));</p> <p> <wbr></p> <p>  E. t==c.</p> <p> <wbr></p> <p>  (acd)</p> <p> <wbr></p> <p>  题目åQšå“ªäº›è¿”回trueã€?/p> <p> <wbr></p> <p>  ˜q™ä¸ªåœ¨å‰é¢ç¬¬10题的equals()æ–ÒŽ³•å’?=操作½W¦çš„讨论中论˜q°è¿‡ã€?=操作½W¦æ¯”较的是操ä½?/p> <p>½W¦ä¸¤ç«¯çš„æ“ä½œæ•°æ˜¯å¦æ˜¯åŒä¸€ä¸ªå¯¹è±¡ï¼Œè€ŒStringçš„equals()æ–ÒŽ³•比较的是两个String对象的内å®?/p> <p>æ˜¯å¦ä¸€æ øP¼Œå…¶å‚数是一个String对象时才有可能返回trueåQŒå…¶å®ƒå¯¹è±¡éƒ½˜q”回假。需要指出的æ˜?/p> <p>ç”׃ºŽså’Œtòq‰™žä½¿ç”¨new创徏的,他们指向内存池中的同一个字½W¦ä¸²å¸”R‡åQŒå› æ­¤å…¶åœ°å€å®žé™…上是</p> <p>相同的(˜q™ä¸ªå¯ä»¥ä»Žå¾~–译一个简单的‹¹‹è¯•½E‹åºçš„结果得刎ͼŒé™äºŽ½‹‡å¹…不列出测试代码和反编</p> <p>译的分析åQ‰ï¼Œå› æ­¤½{”案c也是正确的ã€?/p> <h3><a name="_Toc74451186">§1.1.9 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> ä¹?/a></h3> <p>Class Teacher and Student are subclass of class Person.</p> <p>  Person p;</p> <p>  Teacher t;</p> <p>  Student s;</p> <p>  p, t and s are all non-null.</p> <p>  if(t instanceof Person) { s = (Student)t; }</p> <p>  What is the result of this sentence?</p> <p>  A. It will construct a Student object.</p> <p> <wbr></p> <p>  B. The expression_r is legal.</p> <p> <wbr></p> <p>  C. It is illegal at compilation.</p> <p> <wbr></p> <p>  D. It is legal at compilation but possible illegal at runtime.</p> <p> <wbr></p> <p>  (c)</p> <p> <wbr></p> <p>  题目åQšç±»Teacherå’ŒStudent都是¾c»Person的子¾c?/p> <p>  …</p> <p>  p,tå’Œs都是非空å€?/p> <p>  …</p> <p>  ˜q™ä¸ªè¯­å¥å¯ÆD‡´çš„结果是什ä¹?/p> <p> <wbr></p> <p>  A. ž®†æž„造一个Student对象ã€?/p> <p> <wbr></p> <p>  B. 表达式合法ã€?/p> <p> <wbr></p> <p>  C. ¾~–译旉™žæ³•ã€?/p> <p> <wbr></p> <p>  D. ¾~–译时合法而在˜qè¡Œæ—¶å¯èƒ½éžæ³•ã€?/p> <p> <wbr></p> <p>  instanceof操作½W¦çš„作用是判断一个变量是否是åÏx“ä½œæ•°æŒ‡å‡ºçš„类的一个对象,ç”׃ºŽja</p> <p>va语言的多态性ä‹É得可以用一个子¾cȝš„实例赋值给一个父¾cȝš„变量åQŒè€Œåœ¨ä¸€äº›æƒ…况下需要判æ–?/p> <p>变量到底是一个什么类型的对象åQŒè¿™æ—¶å°±å¯ä»¥ä½¿ç”¨instanceof了。当左操作数是右操作数指å‡?/p> <p>的类的实例或者是子类的实例时都返回真åQŒå¦‚果是ž®†ä¸€ä¸ªå­¾cȝš„实例赋值给一个父¾cȝš„变量åQ?/p> <p>用instanceof判断该变量是否是子类的一个实例时也将˜q”回真。此题中的if语句的判断没有问</p> <p>题,而且ž®†è¿”回真åQŒä½†æ˜¯åŽé¢çš„¾cÕdž‹è½¬æ¢æ˜¯éžæ³•çš„åQŒå› ä¸ºt是一个Teacher对象åQŒå®ƒä¸èƒ½è¢«å¼ºåˆ?/p> <p>转换ä¸ÞZ¸€ä¸ªStudent对象åQŒå³ä½¿è¿™ä¸¤ä¸ª¾cÀLœ‰å…±åŒçš„父¾c…R€‚如果是ž®†t转换ä¸ÞZ¸€ä¸ªPerson对象则可</p> <p>以,而且不需要强制è{换。这个错误在¾~–译时就可以发现åQŒå› æ­¤ç¼–译不能通过ã€?/p> <p> <wbr></p> </div> <img src ="http://www.aygfsteel.com/balajinima/aggbug/298536.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/balajinima/" target="_blank">李云æ³?/a> 2009-10-16 11:41 <a href="http://www.aygfsteel.com/balajinima/articles/298536.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>½W¬å››éƒ¨åˆ†åQšè¯­­a€åŸºç¡€http://www.aygfsteel.com/balajinima/articles/298535.html李云æ³?/dc:creator>李云æ³?/author>Fri, 16 Oct 2009 03:40:00 GMThttp://www.aygfsteel.com/balajinima/articles/298535.htmlhttp://www.aygfsteel.com/balajinima/comments/298535.htmlhttp://www.aygfsteel.com/balajinima/articles/298535.html#Feedback0http://www.aygfsteel.com/balajinima/comments/commentRss/298535.htmlhttp://www.aygfsteel.com/balajinima/services/trackbacks/298535.html½W¬å››éƒ¨åˆ†åQšè¯­­a€åŸºç¡€

  • 能正¼‹®å£°æ˜ŽåŒ…、import、类åQˆåŒ…括内部类åQ‰ã€æŽ¥å£ã€æ–¹æ³•(包括用于开始一个类的执行的mainæ–ÒŽ³•åQ‰ã€å˜é‡ï¼Œòq¶æ­£¼‹®ä‹É用修饰符ã€?
  • 能够正确使用实现了接口的¾c»ï¼ŒåŒ…括java.lang.Runnable接口或题目中指定的接口ã€?
  • 知道命ä×o行参数是如何传递给¾cȝš„mainæ–ÒŽ³•çš„ã€?
  • 知道哪些是JAVA的合法keyword。注意:考试中不会出现要你解释keyword和各¿Uå¸¸æ•°è¿™¿Uæ·±å¥¥çš„问题ã€?
  • 明白如果没有昑ּåœ°èµ‹å€û|¼Œå„种变量或者数¾l„的默认值是什么ã€?
  • æŽŒæ¡æ‰€æœ‰åŽŸå§‹æ•°æ®ç±»åž‹çš„å–å€ÆDŒƒå›ß_¼Œå¦‚何声明各种数据¾cÕdž‹ã€?

§1.1.1       一

1. What will happen when you attempt to compile and run the following code?

public class Static

{

static

{

int x = 5;

}

static int x,y;

public static void main(String args[])

{

       x--;

       myMethod();

       System.out.println(x + y + ++x);

}

public static void myMethod()

{

y = x++ + ++x;

}

}

Choices:

a. Compiletime error

b. prints : 1

c. prints : 2

d. prints : 3

e. prints : 7

f. prints : 8

―――――――――――――――――――�/p>

1) D is the correct choice. The above code will not give any compilation error. Note that "Static" is a valid class name. Thus choice A is incorrect. In the above code, on execution, first the static variables (x and y) will be initialized to 0. Then static block will be called and finally main() method will be called. The execution of static block will have no effect on the output as it declares a new variable (int x). The first statement inside main (x--) will result in x to be -1. After that myMethod() will be executed. The statement "y = x++ + ++x;" will be evaluated to y = -1 + 1 and x will become 1. In case the statement be "y =++x + ++x", it would be evaluated to y = 0 + 1 and x would become 1. Finally when System.out is executed "x + y + ++x" will be evaluated to "1 + 0 + 2" which result in 3 as the output. Thus choice D is correct.

 

§1.1.2       äº?/a>

Considering the following code, Which variables may be referenced correctly at line 12?

1.public class Outer

2.{

3.public int a = 1;

4.private int b = 2;

5.public void method(final int c)

6.{

7.int d = 3;

8.class Inner

9.{

10.private void iMethod(int e)

11. {

12.

13.}

14.}

15.}

16.}

Choices:

a. a

b. b

c. c

d. d

e. e

A, B, C and E are correct. Since Inner is not a static inner class, it has a reference to an enclosing object, and all the variables of that object are accessible. Therefore A and B are correct, even if b is private. Variables in the enclosing method are only accessible when they are marked as final hence c is accessible but not d. E is obviously correct as it is a parameter to the method containing line 12 itself.

 

§1.1.3       ä¸?/a>

What will be the result of executing the following code?

// Filename; SuperclassX.java

package packageX;

public class SuperclassX

{

protected void superclassMethodX()

{

}

int superclassVarX;

}

 

// Filename SubclassY.java

1.package packageX.packageY;

2.

3.public class SubclassY extends SuperclassX

4.{

5.SuperclassX objX = new SubclassY();

6.SubclassY objY = new SubclassY();

7.void subclassMethodY()

8.{

9.objY.superclassMethodX();

10.int i;

11.i = objY.superclassVarX;

12.}

13.}

Choices:

a.Compilation error at line 5

b. Compilation error at line 9

c. Runtime exception at line 11

d. None of these

―――――――�/p>

D is correct. When no access modifier is specified for a member, it is only accessible by another class in the package where its class is defined. Even if its class is visible in another package, the member is not accessible there. In the question above the variable superclassVarX has no access modifier specified and hence it cannot be accessed in the packageY even though the class SuperclassX is visible and the protected method superclassMethodX() can be accessed. Thus the compiler will raise an error at line 11.

 

§1.1.4       å›?/a>

 Consider the class hierarchy shown below:

     --------------------------------------------------------------------

class FourWheeler implements DrivingUtilities

class Car extends FourWheeler

class Truck extends FourWheeler

class Bus extends FourWheeler

class Crane extends FourWheeler

----------------------------------------------------------------------

   

Consider the following code below:

1.DrivingUtilities du;

2.FourWheeler fw;

3.Truck myTruck = new Truck();

4.du = (DrivingUtilities)myTruck;

5.fw = new Crane();

6.fw = du;

Which of the statements below are true?

Choices:

a. Line 4 will not compile because an interface cannot refer to an object.

b. The code will compile and run.

c. The code will not compile without an explicit cast at line 6, because going

down the hierarchy without casting is not allowed.

d.The code at line 4 will compile even without the explicit cast.

e.The code will compile if we put an explicit cast at line 6 but will throw an exception at runtime.

――――――――――�/p>

C and D are correct. A and B are obviously wrong because there is nothing wrong in an interface referring to an object. C is correct because an explicit cast is needed to go down the hierarchy. D is correct because no explicit cast is needed at line 4, because we are going up the hierarchy. E is incorrect because if we put an explicit cast at line 6, the code will compile and run perfectly fine, no exception will be thrown because the runtime class of du (that is Truck) can be converted to type FourWheeler without any problem.

 

§1.1.5       äº?/a>

What will be printed when you execute the following code?

class X

{

Y b = new Y();

       X()

{

System.out.print("X");

}

}

class Y

{

       Y()

{

System.out.print("Y");

}

}

public class Z extends X

{

       Y y = new Y();

       Z()

{

System.out.print("Z");

}

       public static void main(String[] args)

{

               new Z();

       }

}

Choices:

a. Z

b. YZ

c. XYZ

d. YXYZ

――――――――�/p>

D is correct. A difficult but a fundamental question, please observe carefully. Before any object is constructed the object of the parent class is constructed(as there is a default call to the parent's constructor from the constructor of the child class via the super() statement). Also note that when an object is constructed the variables are initialized first and then the constructor is executed. So when new Z() is executed , the object of class X will be constructed, which means Y b = new Y() will be executed and "Y" will be printed as a result. After that constructor of X will be called which implies "X" will be printed. Now the object of Z will be constructed and thus Y y = new Y() will be executed and Y will be printed and finally the constructor Z() will be called and thus "Z" will be printed. Thus YXYZ will be printed.

 

§1.1.6       å…?/a>

What will happen when you attempt to compile and run the following code?

class Base

{

int i = 99;

public void amethod()

{

       System.out.println("Base.amethod()");

     }

     Base()

{

     amethod();

     }

}

public class Derived extends Base

{

int i = -1;

       

public static void main(String argv[])

{

     Base b = new Derived();

       System.out.println(b.i);

       b.amethod();

     }

     public void amethod()

{

       System.out.println("Derived.amethod()");

     }

}

Choices:

a. Derived.amethod()

-1

Derived.amethod()

b. Derived.amethod()

99

c.Derived.amethod()

99

d. Derived.amethod()

e.Compile time error

―――――――�/p>

B is correct. The reason is that this code creates an instance of the Derived class but assigns it to a reference of a the Base class. In this situation a reference to any of the fields such as i will refer to the value in the Base class, but a call to a method will refer to the method in the class type rather than its reference handle. But note that if the amethod() was not present in the base class then compilation error would be reported as at compile time, when compiler sees the statement like b.amethod(), it checks if the method is present in the base class or not. Only at the run time it decides to call the method from the derived class.

 

§1.1.7       ä¸?/a>

Given the following code fragment:

 

  1) public void create() {

 

  2) Vector myVect;

 

  3) myVect = new Vector();

 

  4) }

 

  Which of the following statements are true?

  A. The declaration on line 2 does not allocate memory space for the

variable myVect.

 

  B. The declaration on line 2 allocates memory space for a reference to a

Vector object.

 

  C. The statement on line 2 creates an object of class Vector.

 

  D. The statement on line 3 creates an object of class Vector.

 

  E. The statement on line 3 allocates memory space for an object of class

Vector

  ¾˜»è¯‘

  ¾l™å‡ºä¸‹é¢çš„代码片断。。。下面的哪些陈述为true(çœ??

  A. ½W¬äºŒè¡Œçš„声明不会为变量myVect分配内存½Iºé—´ã€?/p>

  B. ½W¬äºŒè¡Œçš„声明分配一个到Vector对象的引用的内存½Iºé—´ã€?/p>

  C. ½W¬äºŒè¡Œè¯­å¥åˆ›å»ÞZ¸€ä¸ªVector¾cÕd¯¹è±¡ã€?/p>

  D. ½W¬ä¸‰è¡Œè¯­å¥åˆ›å»ÞZ¸€ä¸ªVector¾cÕd¯¹è±¡ã€?/p>

  E. ½W¬ä¸‰è¡Œè¯­å¥äؓ一个Vector¾cÕd¯¹è±¡åˆ†é…å†…存空间ã€?/p>

 

  ½{”案 A,D,E

 

  解析

  SL-275ä¸­æŒ‡å‡ºï¼šè¦äØ“ä¸€ä¸ªæ–°å¯¹è±¡åˆ†é…½Iºé—´å¿…须执行new

Xxx()调用åQŒnew调用执行以下的操作:

 

  1åQŽäØ“æ–°å¯¹è±¡åˆ†é…ç©ºé—´åÆˆž®†å…¶æˆå‘˜åˆå§‹åŒ–䨓0或者nullã€?/p>

 

   2åQŽæ‰§è¡Œç±»ä½“中的初始化。(例如在类中有一个成员声明int a=10;在第一步后a=0

åQŒæ‰§è¡Œåˆ°½W¬äºŒæ­¥åŽa=10åQ?/p>

 

  3åQŽæ‰§è¡Œæž„造函数ã€?/p>

 

  4åQŽå˜é‡è¢«åˆ†é…ä¸ÞZ¸€ä¸ªåˆ°å†…存堆中的新对象的引用ã€?/p>

§1.1.8       å…?/a>

Which of the following statements about variables and their scopes

are true?

 

  A. Instance variables are member variables of a class.

 

  B. Instance variables are declared with the static keyword.

 

  C. Local variables defined inside a method are created when the method

is executed.

 

  D. Local variables must be initialized before they are used.

 

  (acd)

 

  题目åQšä¸‹é¢å…³äºŽå˜é‡åŠå…¶èŒƒå›´çš„陈述哪些是对的ã€?/p>

 

  A. 实例变量是类的成员变量�/p>

 

  B. 实例变量用关键字static声明�/p>

 

  C. 在方法中定义的局部变量在该方法被执行时创�/p>

 

  D. 局部变量在使用前必™å»è¢«åˆå§‹åŒ–ã€?/p>

 

  ¾cÖM¸­æœ‰å‡ ¿Uå˜é‡ï¼Œåˆ†åˆ«æ˜¯ï¼šå±€éƒ¨å˜é‡ï¼ˆè‹±æ–‡å¯ä»¥ä¸ºï¼šlocal\automatic\temporary\stac

k

variableåQ‰æ˜¯å®šä¹‰åœ¨æ–¹æ³•里的变量;实例变量åQˆè‹±æ–‡äØ“åQšinstance

variableåQ‰æ˜¯åœ¨æ–¹æ³•外而在¾cÕd£°æ˜Žå†…定义的变量,有时也叫成员变量åQ›ç±»å˜é‡åQˆè‹±æ–‡äØ“åQšcl

ass

variableåQ‰æ˜¯ç”¨å…³é”®å­—static声明的实例变量,他们的生存期分别是:局部变量在定义该变

量的æ–ÒŽ³•被调用时被创建,而在该方法退出后被撤销åQ›å®žä¾‹å˜é‡åœ¨ä½¿ç”¨new

Xxxx()创徏该类的实例时被创建,而其生存期和该类的实例对象的生存期相同;¾cÕd˜é‡åœ¨è¯?/p>

¾c»è¢«åŠ è²æ—¶è¢«åˆ›å¾åQŒä¸ä¸€å®šè¦ç”¨new

Xxxx()创徏åQŒæ‰€æœ‰è¯¥¾cȝš„实例对象å…׃ín该类变量åQŒå…¶ç”Ÿå­˜æœŸæ˜¯¾cȝš„生存期。ä“Q何变量在使用

前都必须初始åŒ?但是需要指出的是局部变量必™åÀL˜¾å¼åˆå§‹åŒ–åQŒè€Œå®žä¾‹å˜é‡ä¸å¿…,原始¾cÕdž‹çš?/p>

实例变量在该¾cÈš„æž„é€ æ–¹æ³•è¢«è°ƒç”¨æ—¶äØ“å®ƒåˆ†é…çš„¾~ºçœçš„å€û|¼Œæ•´åž‹æ˜?åQŒå¸ƒž®”型是falseåQŒè€ŒæÕQç‚?/p>

型是0.0fåQŒå¼•用类型(¾cȝ±»åž‹ï¼‰çš„实例变量的¾~ºçœå€¼æ˜¯nullåQˆæ²¡æœ‰è¿›è¡Œå®žé™…的初始化,对它çš?/p>

使用ž®†å¼•èµ·NullPointExceptionåQ‰ï¼Œ¾cÕd˜é‡çš„è§„åˆ™å’Œå®žä¾‹å˜é‡ä¸€æ øP¼Œä¸åŒçš„æ˜¯¾cÕd˜é‡çš„初始åŒ?/p>

是在¾c»è¢«åŠ è²æ—¶ã€?/p>

§1.1.9       ä¹?/a>

  public class Parent {

  int change() {…}

  }

  class Child extends Parent {

 

  }

  Which methods can be added into class Child?

  A. public int change(){}

 

  B. int chang(int i){}

 

  C. private int change(){}

 

  D. abstract int chang(){}

 

  (ab)

 

  题目åQšå“ªäº›æ–¹æ³•可被加入类Childã€?/p>

 

  需要注意的是答案Dçš„å†…å®¹ï¼Œå­ç±»å¯ä»¥é‡å†™çˆ¶ç±»çš„æ–¹æ³•åÆˆž®†ä¹‹å£°æ˜Žä¸ºæŠ½è±¡æ–¹æ³•,但是˜q™å¼•发的问题是类必须声明为抽象类åQŒå¦åˆ™ç¼–译不能通过åQŒè€Œä¸”抽象æ–ÒŽ³•不能有方法体åQŒä¹Ÿž®±æ˜¯æ–ÒŽ³•声明后面不能带上那两个大括号åQˆ{}åQ‰ï¼Œ˜q™äº›D都不能满­‘Ÿë€?/p>



]]>
½W¬ä¸‰éƒ¨åˆ†åQšåžƒåœ¾æ”¶é›?/title><link>http://www.aygfsteel.com/balajinima/articles/298534.html</link><dc:creator>李云æ³?/dc:creator><author>李云æ³?/author><pubDate>Fri, 16 Oct 2009 03:39:00 GMT</pubDate><guid>http://www.aygfsteel.com/balajinima/articles/298534.html</guid><wfw:comment>http://www.aygfsteel.com/balajinima/comments/298534.html</wfw:comment><comments>http://www.aygfsteel.com/balajinima/articles/298534.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/balajinima/comments/commentRss/298534.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/balajinima/services/trackbacks/298534.html</trackback:ping><description><![CDATA[<p align="left">½W¬ä¸‰éƒ¨åˆ†åQšåžƒåœ¾æ”¶é›?/p> <ul type="disc"> <li>了解垃圾攉™›†æœºåˆ¶èƒ½ä¿è¯æ‰§è¡Œä»€ä¹ˆã€? <li>能通过代码操作一个对象,使它能被垃圾攉™›†å™¨æ”¶é›†ã€? <li>知道在程序的哪一行,垃圾攉™›†å™¨èƒ½åˆæ³•地收集某个对象ã€?</li> </ul> <h2>½W¬ä¸€èŠ?nbsp;<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 垃圾攉™›†è§£æž</h2> <p> <wbr>垃圾攉™›†å™¨æ˜¯Java语言区别于其他程序设计语­a€çš„一大特艌Ӏ‚它把程序员从手工回收内存空间的¾Jé‡å·¥ä½œä¸­è§£è„׃º†å‡ºæ¥ã€‚在SUN公司的Java½E‹åºå‘˜ï¼ˆJava ProgrammeråQ‰è®¤è¯è€ƒè¯•中,垃圾攉™›†å™¨æ˜¯å¿…考的内容åQŒä¸€èˆ¬æœ€å¤šå¯ä»¥å æ€Õdˆ†å€¼çš„6%左右。但是由于SUN公司的Java Programming Language SL-275 评¡¨‹çš„æ ‡å‡†æ•™æä¸­åQŒå¯¹æœ‰å…³åžƒåœ¾æ”‰™›†å™¨çš„内容只做了非常简单的介绍åQŒè€Œå¦å¤–的一些关于Java技术的书籍åQŒæ¯”如《Java 2 核心技术》(Core Java 2åQ‰ã€ã€ŠJava¾~–程思想》(Thinking in JavaåQ‰ã€ã€Šç²¾é€šJava 2》等½{‰ï¼Œé‡Œé¢å…³äºŽåžƒåœ¾æ”‰™›†å™¨çš„内容也几乎没有,或者只是简单地提两句,所以很多参加Java Programmer认证考试的中国考生åQŒåœ¨åžƒåœ¾æ”‰™›†å™¨è¿™ä¸€éƒ¨åˆ†çš„得分都ä¸?分(½W”者曾认识一位SUN公司授权的中国Java培训班的老师åQŒå…¶è€ƒè¯•æ€Õdˆ†ä¸?9%åQŒä½†åžƒåœ¾æ”‰™›†å™¨çš„éƒ¨åˆ†ç«Ÿç„¶ä¹ŸäØ“0分)。鉴于此åQŒç¬”者æ€È»“了这个垃圾收集器的专题,希望对广大Java技术的爱好者和准备认证考试的考生们有所帮助ã€?/p> <p> <wbr> <wbr> <wbr></p> <p> <wbr> <wbr> <wbr> 我们知道åQŒè®¸å¤šç¨‹åºè®¾è®¡è¯­­a€éƒ½å…è®¸åœ¨½E‹åº˜qè¡ŒæœŸåŠ¨æ€åœ°åˆ†é…å†…å­˜½Iºé—´ã€‚分配内存的方式多种多样åQŒå–决于该种语言的语法结构。但不论是哪一¿Uè¯­­a€çš„内存分配方式,最后都要返回所分配的内存块的è“v始地址åQŒå³˜q”回一个指针到内存块的首地址ã€?/p> <p>当已¾låˆ†é…çš„内存½Iºé—´ä¸å†éœ€è¦æ—¶åQŒæ¢å¥è¯è¯´å½“指向该内存块的句柄超å‡ÞZº†ä½¿ç”¨èŒƒå›´çš„æ—¶å€™ï¼Œè¯¥ç¨‹åºæˆ–其运行环境就应该回收该内存空é—ß_¼Œä»¥èŠ‚çœå®è´ëŠš„内存资源ã€?/p> <p> <wbr></p> <p> <wbr> <wbr> <wbr> 在CåQŒC++或其他程序设计语­a€ä¸­ï¼Œæ— è®ºæ˜¯å¯¹è±¡è¿˜æ˜¯åŠ¨æ€é…¾|®çš„资源或内存,都必™åȝ”±½E‹åºå‘˜è‡ªè¡Œå£°æ˜Žäñ”生和回收åQŒå¦åˆ™å…¶ä¸­çš„资源ž®†æ¶ˆè€—,造成资源的浪费甚è‡Ïx­»æœºã€‚但手工回收内存往往是一™å¹å¤æ‚è€Œè‰°å·¨çš„å·¥ä½œã€‚å› ä¸ø™¦é¢„å…ˆ¼‹®å®šå ç”¨çš„内存空间是否应该被回收是非常困隄¡š„åQå¦‚果一ŒD늨‹åºä¸èƒ½å›žæ”¶å†…存空é—ß_¼Œè€Œä¸”在程序运行时¾pȝ»Ÿä¸­åˆæ²¡æœ‰äº†å¯ä»¥åˆ†é…çš„内存½Iºé—´æ—Óž¼Œ˜q™æ®µ½E‹åºž®±åªèƒ½å´©æºƒã€‚通常åQŒæˆ‘们把分配出去后,却无法回收的内存½Iºé—´¿UîCØ“"内存渗漏体(Memory LeaksåQ?ã€?/p> <p>以上˜q™ç§½E‹åºè®¾è®¡çš„æ½œåœ¨å±é™©æ€§åœ¨Java˜q™æ ·ä»¥ä¸¥è°¨ã€å®‰å…¨è‘—¿U°çš„语言中是不允许的。但是Java语言既不能限制程序员¾~–写½E‹åºçš„自由性,又不能把声明对象的部分去除(否则ž®×ƒ¸æ˜¯é¢å‘对象的½E‹åºè¯­è¨€äº†ï¼‰åQŒé‚£ä¹ˆæœ€å¥½çš„解决办法ž®±æ˜¯ä»ŽJava½E‹åºè¯­è¨€æœ¬èín的特性入手。于是,Java技术提供了一个系¾lŸçñ”的线½E‹ï¼ˆThreadåQ‰ï¼Œå›_žƒåœ¾æ”¶é›†å™¨¾U¿ç¨‹åQˆGarbage Collection ThreadåQ‰ï¼Œæ¥è·Ÿítªæ¯ä¸€å—分配出åŽÈš„内存½Iºé—´åQŒå½“Java 虚拟机(Java Virtual MachineåQ‰å¤„于空闲åó@环时åQŒåžƒåœ¾æ”¶é›†å™¨¾U¿ç¨‹ä¼šè‡ªåŠ¨æ£€æŸ¥æ¯ä¸€å¿«åˆ†é…å‡ºåŽÈš„内存½Iºé—´åQŒç„¶åŽè‡ªåŠ¨å›žæ”¶æ¯ä¸€å¿«å¯ä»¥å›žæ”¶çš„æ— ç”¨çš„å†…å­˜å—ã€?/p> <p> <wbr></p> <p> <wbr> <wbr> <wbr> 垃圾攉™›†å™¨çº¿½E‹æ˜¯ä¸€¿Uä½Žä¼˜å…ˆ¾U§çš„¾U¿ç¨‹åQŒåœ¨ä¸€ä¸ªJava½E‹åºçš„生命周期中åQŒå®ƒåªæœ‰åœ¨å†…存空闲的时候才有机会运行。它有效地防止了内存渗漏体的出现åQŒåƈ极大可能地节省了宝贵的内存资源。但是,通过Java虚拟机来执行垃圾攉™›†å™¨çš„æ–ÒŽ¡ˆå¯ä»¥æ˜¯å¤š¿Uå¤šæ ïLš„ã€?/p> <p>下面介绍垃圾攉™›†å™¨çš„特点和它的执行机åˆÓž¼š</p> <p> <wbr> <wbr> <wbr> 垃圾攉™›†å™¨ç³»¾lŸæœ‰è‡ªå·±çš„一套方案来判断哪个内存块是应该被回收的åQŒå“ªä¸ªæ˜¯ä¸ç¬¦åˆè¦æ±‚暂不回收的。垃圾收集器在一个Java½E‹åºä¸­çš„æ‰§è¡Œæ˜¯è‡ªåŠ¨çš„åQŒä¸èƒ½å¼ºåˆ¶æ‰§è¡Œï¼Œå³ä‹É½E‹åºå‘˜èƒ½æ˜Žç¡®åœ°åˆ¤æ–­å‡ºæœ‰ä¸€å—内存已¾læ— ç”¨äº†åQŒæ˜¯åº”该回收的,½E‹åºå‘˜ä¹Ÿä¸èƒ½å¼ºåˆ¶åžƒåœ¾æ”‰™›†å™¨å›žæ”¶è¯¥å†…存块。程序员唯一能做的就是通过调用System. gc æ–ÒŽ³•æ?廸™®®"执行垃圾攉™›†å™¨ï¼Œä½†å…¶æ˜¯å¦å¯ä»¥æ‰§è¡ŒåQŒä»€ä¹ˆæ—¶å€™æ‰§è¡Œå´éƒ½æ˜¯ä¸å¯çŸ¥çš„。这也是垃圾攉™›†å™¨çš„æœ€ä¸»è¦çš„缺炏V€‚当然相对于它给½E‹åºå‘˜å¸¦æ¥çš„巨大方便性而言åQŒè¿™ä¸ªç¼ºç‚ÒŽ˜¯ç‘•不掩瑜的ã€?/p> <p>垃圾攉™›†å™¨çš„主要特点有:</p> <p>1åQŽåžƒåœ¾æ”¶é›†å™¨çš„工作目标是回收已经无用的对象的内存½Iºé—´åQŒä»Žè€Œé¿å…å†…存渗漏体的äñ”生,节省内存资源åQŒé¿å…ç¨‹åºä»£ç çš„崩溃ã€?/p> <p>2åQŽåžƒåœ¾æ”¶é›†å™¨åˆ¤æ–­ä¸€ä¸ªå¯¹è±¡çš„内存½Iºé—´æ˜¯å¦æ— ç”¨çš„æ ‡å‡†æ˜¯åQšå¦‚果该对象不能再被½E‹åºä¸­ä“Q何一ä¸?‹zÕdŠ¨çš„éƒ¨åˆ?所引用åQŒæ­¤æ—¶æˆ‘们就è¯ß_¼Œè¯¥å¯¹è±¡çš„内存½Iºé—´å·²ç»æ— ç”¨ã€‚所è°?‹zÕdŠ¨çš„éƒ¨åˆ?åQŒæ˜¯æŒ‡ç¨‹åºä¸­æŸéƒ¨åˆ†å‚与程序的调用åQŒæ­£åœ¨æ‰§è¡Œè¿‡½E‹ä¸­åQŒå°šæœªæ‰§è¡Œå®Œæ¯•ã€?/p> <p>3åQŽåžƒåœ¾æ”¶é›†å™¨¾U¿ç¨‹è™½ç„¶æ˜¯ä½œä¸ÞZ½Žä¼˜å…ˆ¾U§çš„¾U¿ç¨‹˜qè¡ŒåQŒä½†åœ¨ç³»¾lŸå¯ç”¨å†…存量˜q‡ä½Žçš„æ—¶å€™ï¼Œå®ƒå¯èƒ½ä¼š½Hå‘地执行来挽救内存资源。当然其执行与否也是不可预知的ã€?/p> <p>4åQŽåžƒåœ¾æ”¶é›†å™¨ä¸å¯ä»¥è¢«å¼ºåˆ¶æ‰§è¡ŒåQŒä½†½E‹åºå‘˜å¯ä»¥é€šè¿‡è°ƒç”¨System. gcæ–ÒŽ³•来徏议执行垃圾收集器ã€?/p> <p>5åQŽä¸èƒ½ä¿è¯ä¸€ä¸ªæ— ç”¨çš„对象一定会被垃圾收集器攉™›†åQŒä¹Ÿä¸èƒ½ä¿è¯åžƒåœ¾æ”‰™›†å™¨åœ¨ä¸€ŒDµJava语言代码中一定会执行。因此在½E‹åºæ‰§è¡Œ˜q‡ç¨‹ä¸­è¢«åˆ†é…å‡ºåŽ»çš„å†…å­˜ç©ºé—´å¯èƒ½ä¼šä¸€ç›´ä¿ç•™åˆ°è¯¥ç¨‹åºæ‰§è¡Œå®Œæ¯•ï¼Œé™¤éžè¯¥ç©ºé—´è¢«é‡æ–°åˆ†é…æˆ–è¢«å…¶ä»–æ–ÒŽ³•回收。由此可见,完全å½Õdº•地根¾lå†…存渗漏体的äñ”生也是不可能的。但是请不要忘记åQŒJava的垃圾收集器毕竟使程序员从手工回收内存空间的¾Jé‡å·¥ä½œä¸­è§£è„׃º†å‡ºæ¥ã€‚设想一个程序员要用C或C++来编写一ŒD?0万行语句的代码,那么他一定会充分体会到Java的垃圾收集器的优点!</p> <p>6åQŽåŒæ äh²¡æœ‰åŠžæ³•é¢„çŸ¥åœ¨ä¸€¾l„均½W¦åˆåžƒåœ¾æ”‰™›†å™¨æ”¶é›†æ ‡å‡†çš„对象中,哪一个会被首先收集ã€?/p> <p>7åQŽåó@环引用对象不会媄响其被垃圾收集器攉™›†ã€?/p> <p>8åQŽå¯ä»¥é€šè¿‡ž®†å¯¹è±¡çš„引用变量åQˆreference variablesåQŒå³å¥æŸ„handlesåQ‰åˆå§‹åŒ–为nullå€û|¼Œæ¥æš—½Cºåžƒåœ¾æ”¶é›†å™¨æ¥æ”¶é›†è¯¥å¯¹è±¡ã€‚但此时åQŒå¦‚果该对象˜qžæŽ¥æœ‰äº‹ä»¶ç›‘听器åQˆå…¸åž‹çš„ AWT¾l„äšgåQ‰ï¼Œé‚£å®ƒ˜q˜æ˜¯ä¸å¯ä»¥è¢«æ”‰™›†ã€‚æ‰€ä»¥åœ¨è®¾ä¸€ä¸ªå¼•ç”¨å˜é‡äØ“nullå€ég¹‹å‰ï¼Œåº”注意该引用变量指向的对象是否被监听åQŒè‹¥æœ‰ï¼Œè¦é¦–先除åŽÈ›‘听器åQŒç„¶åŽæ‰å¯ä»¥èµ‹ç©ºå€¹{€?/p> <p>9åQŽæ¯ä¸€ä¸ªå¯¹è±¡éƒ½æœ‰ä¸€ä¸ªfinalize( )æ–ÒŽ³•åQŒè¿™ä¸ªæ–¹æ³•是从Object¾cȝ‘ô承来的ã€?/p> <p>10åQŽfinalize( )æ–ÒŽ³•用来回收内存以外的系¾lŸèµ„源,ž®±åƒæ˜¯æ–‡ä»¶å¤„理器和网¾lœè¿žæŽ¥å™¨ã€‚该æ–ÒŽ³•的调用顺序和用来调用该方法的对象的创建顺序是无关的。换句话è¯ß_¼Œä¹¦å†™½E‹åºæ—¶è¯¥æ–ÒŽ³•的顺序和æ–ÒŽ³•的实际调用顺序是不相òq²çš„。请注意˜q™åªæ˜¯finalize( )æ–ÒŽ³•的特炏V€?/p> <p>11åQŽæ¯ä¸ªå¯¹è±¡åªèƒ½è°ƒç”¨finalize( )æ–ÒŽ³•一‹Æ¡ã€‚如果在finalize( )æ–ÒŽ³•执行时äñ”生异常(exceptionåQ‰ï¼Œåˆ™è¯¥å¯¹è±¡ä»å¯ä»¥è¢«åžƒåœ¾æ”‰™›†å™¨æ”¶é›†ã€?/p> <p>12åQŽåžƒåœ¾æ”¶é›†å™¨è·Ÿè¸ªæ¯ä¸€ä¸ªå¯¹è±¡ï¼Œæ”‰™›†é‚£äº›ä¸å¯åˆ°è¾¾çš„对象(卌™¯¥å¯¹è±¡æ²¡æœ‰è¢«ç¨‹åºçš„ä»ÖM½•"‹zȝš„部分"所调用åQ‰ï¼Œå›žæ”¶å…¶å æœ‰çš„内存½Iºé—´ã€‚但在进行垃圾收集的时候,垃圾攉™›†å™¨ä¼šè°ƒç”¨finalize( )æ–ÒŽ³•åQŒé€šè¿‡è®©å…¶ä»–对象知道它的存在,而ä‹É不可到达的对象再‹Æ?复苏"为可到达的对象。既然每个对象只能调用一‹Æ¡finalize( )æ–ÒŽ³•åQŒæ‰€ä»¥æ¯ä¸ªå¯¹è±¡ä¹Ÿåªå¯èƒ?复苏"一‹Æ¡ã€?/p> <p>13åQŽfinalize( )æ–ÒŽ³•可以明确地被调用åQŒä½†å®ƒå´ä¸èƒ½˜q›è¡Œåžƒåœ¾æ”‰™›†ã€?/p> <p>14åQŽfinalize( )æ–ÒŽ³•可以被重载(overloadåQ‰ï¼Œä½†åªæœ‰å…·å¤‡åˆå§‹çš„finalize( )æ–ÒŽ³•特点的方法才可以被垃圾收集器调用ã€?/p> <p>15åQŽå­¾cȝš„finalize( )æ–ÒŽ³•可以明确地调用父¾cȝš„finalize( )æ–ÒŽ³•åQŒä½œä¸ø™¯¥å­ç±»å¯¹è±¡çš„æœ€åŽä¸€‹Æ¡é€‚当的操作。但Java¾~–è¯‘å™¨å´ä¸è®¤ä¸ø™¿™æ˜¯ä¸€‹Æ¡è¦†ç›–操作(overridingåQ‰ï¼Œæ‰€ä»¥ä¹Ÿä¸ä¼šå¯¹å…¶è°ƒç”¨˜q›è¡Œ‹‚€æŸ¥ã€?/p> <p>16åQŽå½“finalize( )æ–ÒŽ³•ž®šæœªè¢«è°ƒç”¨æ—¶åQŒSystem. runFinalization( )æ–ÒŽ³•可以用来调用finalize( )æ–ÒŽ³•åQŒåƈ实现相同的效果,å¯ÒŽ— ç”¨å¯¹è±¡è¿›è¡Œåžƒåœ¾æ”¶é›†ã€?/p> <p>17åQŽå½“一个方法执行完毕,其中的局部变量就会超å‡ÞZ‹É用范å›ß_¼Œæ­¤æ—¶å¯ä»¥è¢«å½“作垃圾收集,但以后每当该æ–ÒŽ³•再次被调用时åQŒå…¶ä¸­çš„局部变量便会被重新创徏ã€?/p> <p>18åQŽJava语言使用了一¿U?标记交换区的垃圾攉™›†½Ž—法"。该½Ž—法会遍历程序中每一个对象的句柄åQŒäؓ被引用的对象做标讎ͼŒç„¶åŽå›žæ”¶ž®šæœªåšæ ‡è®°çš„对象。所谓遍历可以简单地理解ä¸?‹‚€æŸ¥æ¯ä¸€ä¸?ã€?/p> <p>19åQŽJava语言允许½E‹åºå‘˜äØ“ä»ÖM½•æ–ÒŽ³•æ·ÕdŠ finalize( )æ–ÒŽ³•åQŒè¯¥æ–ÒŽ³•会在垃圾攉™›†å™¨äº¤æ¢å›žæ”¶å¯¹è±¡ä¹‹å‰è¢«è°ƒç”¨ã€‚但不要˜q‡åˆ†ä¾èµ–该方法对¾pȝ»Ÿèµ„源˜q›è¡Œå›žæ”¶å’Œå†åˆ©ç”¨åQŒå› ä¸ø™¯¥æ–ÒŽ³•调用后的执行¾l“果是不可预知的ã€?/p> <p>通过以上对垃圾收集器特点的了解,你应该可以明¼‹®åžƒåœ¾æ”¶é›†å™¨çš„作用,和垃圾收集器判断一块内存空间是否无用的标准。简单地è¯ß_¼Œå½“ä½ ä¸ÞZ¸€ä¸ªå¯¹è±¡èµ‹å€égØ“nullòq¶ä¸”重新定向了该对象的引用者,此时该对象就½W¦åˆåžƒåœ¾æ”‰™›†å™¨çš„æ”‰™›†æ ‡å‡†ã€?/p> <p>判断一个对象是否符合垃圾收集器的收集标准,˜q™æ˜¯SUN公司½E‹åºå‘˜è®¤è¯è€ƒè¯•中垃圾收集器部分的重要考点åQˆå¯ä»¥è¯´åQŒè¿™æ˜¯å”¯ä¸€çš„考点åQ‰ã€‚所以,考生在一ŒD늻™å®šçš„代码中,应该能够判断出哪个对象符合垃圾收集器攉™›†çš„æ ‡å‡†ï¼Œå“ªä¸ªä¸ç¬¦åˆã€‚下面结合几¿Uè®¤è¯è€ƒè¯•中可能出现的题型来具体讲解:</p> <p>Object obj = new Object ( ) ;</p> <p>我们知道åQŒobj为Object的一个句柄。当出现new关键字时åQŒå°±¾l™æ–°å»ºçš„对象分配内存½Iºé—´åQŒè€Œobj的值就是新分配的内存空间的首地址åQŒå³è¯¥å¯¹è±¡çš„å€?è¯ïL‰¹åˆ«æ³¨æ„ï¼Œå¯¹è±¡çš„值和对象的内å®ÒŽ˜¯ä¸åŒå«ä¹‰çš„两个概念:对象的值就是指其内存块的首地址åQŒå³å¯¹è±¡çš„句柄;而对象的内容则是其具体的内存å?。此时如果有 obj = nullåQ›åˆ™obj指向的内存块此时ž®±æ— ç”¨äº†åQŒå› ä¸ÞZ¸‹é¢å†æ²¡æœ‰è°ƒç”¨è¯¥å˜é‡äº†ã€?/p> <p>请再看以下三¿Uè®¤è¯è€ƒè¯•时可能出现的题型åQ?/p> <p>½E‹åºŒD?åQ?/p> <p>1åQŽfobj = new Object ( ) ;</p> <p>2åQŽfobj. Method ( ) ;</p> <p>3åQŽfobj = new Object ( ) ;</p> <p>4åQŽfobj. Method ( ) ;</p> <p>问:˜q™æ®µä»£ç ä¸­ï¼Œ½W¬å‡ è¡Œçš„fobj ½W¦åˆåžƒåœ¾æ”‰™›†å™¨çš„æ”‰™›†æ ‡å‡†åQ?/p> <p>½{”:½W?行。因为第3行的fobj被赋了新å€û|¼Œäº§ç”Ÿäº†ä¸€ä¸ªæ–°çš„对象,åÏx¢äº†ä¸€å—新的内存空é—ß_¼Œä¹Ÿç›¸å½“于为第1行中的fobj赋了null倹{€‚è¿™¿Uç±»åž‹çš„题在认证0考试中是最½Ž€å•çš„ã€?/p> <p>½E‹åºŒD?åQ?/p> <p>1åQŽObject sobj = new Object ( ) ;</p> <p>2åQŽObject sobj = null ;</p> <p>3åQŽObject sobj = new Object ( ) ;</p> <p>4åQŽsobj = new Object ( ) ;</p> <p>问:˜q™æ®µä»£ç ä¸­ï¼Œ½W¬å‡ è¡Œçš„内存½Iºé—´½W¦åˆåžƒåœ¾æ”‰™›†å™¨çš„æ”‰™›†æ ‡å‡†åQ?/p> <p>½{”:½W?行和½W?行。因为第2è¡ŒäØ“sobj赋å€égØ“nullåQŒæ‰€ä»¥åœ¨æ­¤ç¬¬1行的sobj½W¦åˆåžƒåœ¾æ”‰™›†å™¨çš„æ”‰™›†æ ‡å‡†ã€‚而第4行相当于为sobj赋å€égØ“nullåQŒæ‰€ä»¥åœ¨æ­¤ç¬¬3行的sobj也符合垃圾收集器的收集标准ã€?/p> <p>如果有一个对象的句柄aåQŒä¸”你把aä½œäØ“æŸä¸ªæž„é€ å™¨çš„å‚æ•ŽÍ¼Œå?new Constructor ( a )的时候,即ä‹Éä½ ç»™a赋å€égØ“nullåQŒa也不½W¦åˆåžƒåœ¾æ”‰™›†å™¨çš„æ”‰™›†æ ‡å‡†ã€‚直到由上面构造器构造的新对象被赋空值时åQŒa才可以被垃圾攉™›†å™¨æ”¶é›†ã€?/p> <p>½E‹åºŒD?åQ?/p> <p>1åQŽObject aobj = new Object ( ) ;</p> <p>2åQŽObject bobj = new Object ( ) ;</p> <p>3åQŽObject cobj = new Object ( ) ;</p> <p>4åQŽaobj = bobj;</p> <p>5åQŽaobj = cobj;</p> <p>6åQŽcobj = null;</p> <p>7åQŽaobj = null;</p> <p>问:˜q™æ®µä»£ç ä¸­ï¼Œ½W¬å‡ è¡Œçš„内存½Iºé—´½W¦åˆåžƒåœ¾æ”‰™›†å™¨çš„æ”‰™›†æ ‡å‡†åQ?/p> <p>½{”:½W?行。注意这¾c»é¢˜åž‹æ˜¯è®¤è¯è€ƒè¯•中可能遇到的最éšùN¢˜åž‹äº†ã€?/p> <p>è¡?-3分别创徏了Object¾cȝš„三个对象åQšaobjåQŒbobjåQŒcobj</p> <p>è¡?åQšæ­¤æ—¶å¯¹è±¡aobj的句柄指向bobjåQŒæ‰€ä»¥è¯¥è¡Œçš„æ‰§è¡Œä¸èƒ½ä½¿aobj½W¦åˆåžƒåœ¾æ”‰™›†å™¨çš„æ”‰™›†æ ‡å‡†ã€?/p> <p>è¡?åQšæ­¤æ—¶å¯¹è±¡aobj的句柄指向cobjåQŒæ‰€ä»¥è¯¥è¡Œçš„æ‰§è¡Œä¸èƒ½ä½¿aobj½W¦åˆåžƒåœ¾æ”‰™›†å™¨çš„æ”‰™›†æ ‡å‡†ã€?/p> <p>è¡?åQšæ­¤æ—¶ä»æ²¡æœ‰ä»ÖM½•一个对象符合垃圾收集器的收集标准ã€?/p> <p>è¡?åQšå¯¹è±¡cobj½W¦åˆäº†åžƒåœ¾æ”¶é›†å™¨çš„æ”¶é›†æ ‡å‡†ï¼Œå› äØ“cobj的句柄指向单一的地址½Iºé—´ã€‚在½W?行的时候,cobj已经被赋å€égØ“nullåQŒä½†ç”±cobj同时˜q˜æŒ‡å‘了aobjåQˆç¬¬5行)åQŒæ‰€ä»¥æ­¤æ—¶cobjòq¶ä¸½W¦åˆåžƒåœ¾æ”‰™›†å™¨çš„æ”‰™›†æ ‡å‡†ã€‚而在½W?行,aobj所指向的地址½Iºé—´ä¹Ÿè¢«èµ‹äºˆäº†ç©ºå€¼nullåQŒè¿™ž®Þp¯´æ˜Žäº†åQŒç”±cobj所指向的地址½Iºé—´å·²ç»è¢«å®Œå…¨åœ°èµ‹äºˆäº†ç©ºå€¹{€‚所以此时cobj最¾lˆç¬¦åˆäº†åžƒåœ¾æ”‰™›†å™¨çš„æ”‰™›†æ ‡å‡†ã€‚但对于aobjå’ŒbobjåQŒä»ç„¶æ— æ³•判断其是否½W¦åˆæ”‰™›†æ ‡å‡†ã€?/p> <p>æ€ÖM¹‹åQŒåœ¨Java语言中,判断一块内存空间是否符合垃圾收集器攉™›†æ ‡å‡†çš„æ ‡å‡†åªæœ‰ä¸¤ä¸ªï¼š</p> <p>1åQŽç»™å¯¹è±¡èµ‹äºˆäº†ç©ºå€¼nullåQŒä»¥ä¸‹å†æ²¡æœ‰è°ƒç”¨˜q‡ã€?/p> <p>2åQŽç»™å¯¹è±¡èµ‹äºˆäº†æ–°å€û|¼Œæ—¢é‡æ–°åˆ†é…äº†å†…å­˜½Iºé—´ã€?/p> <p>最后再‹Æ¡æé†’一下,一块内存空间符合了垃圾攉™›†å™¨çš„æ”‰™›†æ ‡å‡†åQŒåƈ不意味着˜q™å—内存½Iºé—´ž®×ƒ¸€å®šä¼šè¢«åžƒåœ¾æ”¶é›†å™¨æ”‰™›†ã€?/p> <p> <wbr></p> <img src ="http://www.aygfsteel.com/balajinima/aggbug/298534.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/balajinima/" target="_blank">李云æ³?/a> 2009-10-16 11:39 <a href="http://www.aygfsteel.com/balajinima/articles/298534.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>½W¬äºŒéƒ¨åˆ†åQ𿵁½E‹æŽ§åˆÓž¼Œæ–­è¨€å’Œå¼‚常处ç?/title><link>http://www.aygfsteel.com/balajinima/articles/298533.html</link><dc:creator>李云æ³?/dc:creator><author>李云æ³?/author><pubDate>Fri, 16 Oct 2009 03:38:00 GMT</pubDate><guid>http://www.aygfsteel.com/balajinima/articles/298533.html</guid><wfw:comment>http://www.aygfsteel.com/balajinima/comments/298533.html</wfw:comment><comments>http://www.aygfsteel.com/balajinima/articles/298533.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/balajinima/comments/commentRss/298533.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/balajinima/services/trackbacks/298533.html</trackback:ping><description><![CDATA[<p align="left">½W¬äºŒéƒ¨åˆ†åQ𿵁½E‹æŽ§åˆÓž¼Œæ–­è¨€å’Œå¼‚常处ç?/p> <ul type="disc"> <li>能够正确使用if,switch语句åQŒåƈ且能正确使用合法的参数类型ã€? <li>能够正确使用所有带标签或不带标½{„¡š„循环语句åQŒèƒ½æ­£ç¡®ä½¿ç”¨break,continueåQŒèƒ½è®¡ç®—在åó@环中或åó@环后循环计数器的倹{€? <li>能够正确使用异常和异常处理语句(try,catch,finallyåQ‰ã€‚能正确声明抛出异常的方法,òq¶çŸ¥é“怎样覆盖它ã€? <li>了解代码ŒDµä¸­çš„异常对½E‹åºè·Œ™{的媄响。注意:异常可能是一个运行时异常åQˆruntime <wbr>exceptionåQ?一个已¾lå®šä¹‰çš„异常åQˆchecked <wbr>exceptionåQ‰ï¼Œä¹Ÿå¯èƒ½æ˜¯ä¸€ä¸ªerrorã€? <li>能正¼‹®ä‹É用断­a€åQŒåƈ了解关于断言机制的正¼‹®è¯´æ³•ã€?</li> </ul> <div> <ul type="disc"> <li>能够正确使用if,switch语句åQŒåƈ且能正确使用合法的参数类型ã€? <li>能够正确使用所有带标签或不带标½{„¡š„循环语句åQŒèƒ½æ­£ç¡®ä½¿ç”¨break,continueåQŒèƒ½è®¡ç®—在åó@环中或åó@环后循环计数器的倹{€? <li>能够正确使用异常和异常处理语句(try,catch,finallyåQ‰ã€‚能正确声明抛出异常的方法,òq¶çŸ¥é“怎样覆盖它ã€? <li>了解代码ŒDµä¸­çš„异常对½E‹åºè·Œ™{的媄响。注意:异常可能是一个运行时异常åQˆruntime <wbr>exceptionåQ?一个已¾lå®šä¹‰çš„异常åQˆchecked <wbr>exceptionåQ‰ï¼Œä¹Ÿå¯èƒ½æ˜¯ä¸€ä¸ªerrorã€? <li>能正¼‹®ä‹É用断­a€åQŒåƈ了解关于断言机制的正¼‹®è¯´æ³•ã€?</li> </ul> <p> <wbr></p> <p> <wbr></p> <h2>½W¬ä¸€èŠ?nbsp;<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 断言 assert</h2> <h3><a name="1">§1.1.1 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> assertion</a>的语法和语义</h3> <p>J2SE 1.4在语­a€ä¸Šæä¾›äº†ä¸€ä¸ªæ–°ç‰ÒŽ€§ï¼Œž®±æ˜¯assertion(断言)功能åQŒå®ƒæ˜¯è¯¥ç‰ˆæœ¬åœ¨Java语言斚w¢æœ€å¤§çš„革新。在软äšg开发中åQŒassertion是一¿Uç»å…¸çš„调试、测试方式,本文ž®†æ·±å…¥è§£æžassertion功能的ä‹É用以及其设计理念åQŒåƈ¾l™å‡ºç›¸å…³çš„例子ã€?/p> <p>assertion(断言)在èÊY件开发中是一¿Uå¸¸ç”¨çš„调试方式åQŒå¾ˆå¤šå¼€å‘语­a€ä¸­éƒ½æ”¯æŒ˜q™ç§æœºåˆ¶åQŒå¦‚CåQŒC++å’ŒEiffel½{‰ï¼Œä½†æ˜¯æ”¯æŒçš„åŞ式不ž®½ç›¸åŒï¼Œæœ‰çš„æ˜¯é€šè¿‡è¯­è¨€æœ¬èín、有的是通过库函数等。另外,从理è®ÞZ¸Šæ¥è¯´åQŒé€šè¿‡assertion方式可以证明½E‹åºçš„æ­£¼‹®æ€§ï¼Œä½†æ˜¯˜q™æ˜¯ä¸€™å¹ç›¸å½“复杂的工作åQŒç›®å‰è¿˜æ²¡æœ‰å¤ªå¤šçš„实跉|„ä¹‰ã€?/p> <p>在实çŽîC¸­åQŒassertionž®±æ˜¯åœ¨ç¨‹åºä¸­çš„一条语句,它对一个boolean表达式进行检查,一个正¼‹®ç¨‹åºå¿…™åÖM¿è¯è¿™ä¸ªboolean表达式的å€égØ“trueåQ›å¦‚果该å€égØ“falseåQŒè¯´æ˜Žç¨‹åºå·²¾lå¤„于不正确的状态下åQŒç³»¾lŸå°†¾l™å‡ºè­¦å‘Šæˆ–退出。一般来è¯ß_¼Œassertion用于保证½E‹åºæœ€åŸºæœ¬ã€å…³é”®çš„æ­£ç¡®æ€§ã€‚assertion‹‚€æŸ¥é€šå¸¸åœ¨å¼€å‘å’Œ‹¹‹è¯•æ—¶å¼€å¯ã€‚äØ“äº†æé«˜æ€§èƒ½åQŒåœ¨è½¯äšg发布后,assertion‹‚€æŸ¥é€šå¸¸æ˜¯å…³é—­çš„。下面简单介¾lä¸€ä¸‹Java中assertion的实现ã€?/p> <p><strong>1</strong><strong>åQ?/strong><strong>1)</strong> <strong>语法表示</strong></p> <p>在语法上åQŒäؓ了支持assertionåQŒJava增加了一个关键字assert。它包括两种表达式,分别如下åQ?/p> <p>1. <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> assert expression_r1;</p> <p>2. <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> assert expression_r1: expression_r2;</p> <p>在两¿Uè¡¨è¾‘Ö¼ä¸­ï¼Œexpression_r1表示一个boolean表达式,expression_r2表示一个基本类型或者是一个对è±?Object) åQŒåŸºæœ¬ç±»åž‹åŒ…括boolean,char,double,float,intå’Œlongã€‚ç”±äºŽæ‰€æœ‰ç±»éƒ½äØ“Object的子¾c»ï¼Œå› æ­¤˜q™ä¸ªå‚数可以用于所有对象ã€?/p> <p><strong>1</strong><strong>åQ?/strong><strong>2)</strong> <strong>语义含义</strong></p> <p>在运行时åQŒå¦‚果关闭了assertion功能åQŒè¿™äº›è¯­å¥å°†ä¸è“vä»ÖM½•作用。如果打开了assertion功能åQŒé‚£ä¹ˆexpression_r1的值将被计½Ž—,如果它的å€égØ“falseåQŒè¯¥è¯­å¥å¼ºæŠ›å‡ÞZ¸€ä¸ªAssertionError对象。如果assertion语句包括expression_r2参数åQŒç¨‹åºå°†è®¡ç®—出expression_r2的结果,然后ž®†è¿™ä¸ªç»“果作为AssertionError的构造函数的参数åQŒæ¥åˆ›å¾AssertionError对象åQŒåƈ抛出该对象;如果expression_r1å€égØ“trueåQŒexpression_r2ž®†ä¸è¢«è®¡½Ž—ã€?/p> <p>一¿Uç‰¹ŒDŠæƒ…冉|˜¯åQŒå¦‚果在计算表达式时åQŒè¡¨è¾‘Ö¼æœ¬èín抛出ExceptionåQŒé‚£ä¹ˆassertž®†åœæ­¢è¿è¡Œï¼Œè€ŒæŠ›å‡ø™¿™ä¸ªExceptionã€?/p> <p><strong>1</strong><strong>åQ?/strong><strong>3)</strong> <strong>一äº?/strong><strong>assertion</strong><strong>例子</strong></p> <p>下面是一些Assert的例子ã€?/p> <p>1. <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> assert  0 < value;</p> <p>2. <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> assert  0 < value:"value="+value;</p> <p>3. <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> assert  ref != null:"ref doesn't equal null";</p> <p>4. <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> assert  isBalanced();</p> <p><strong>1</strong><strong>åQ?/strong><strong>4)</strong> <strong>¾~–译</strong></p> <p>ç”׃ºŽassert是一个新关键字,使用老版本的JDK是无法编译带有assert的源½E‹åºã€‚因此,我们必须使用JDK1.4(或者更æ–?çš„Java¾~–译器,在ä‹É用Javac命ä×oæ—Óž¼Œæˆ‘们必须加上-source 1.4ä½œäØ“å‚æ•°ã€?source 1.4表示使用JDK 1.4版本的方式来¾~–译源代码,否则¾~–译ž®×ƒ¸èƒ½é€šè¿‡åQŒå› ä¸ºç¼ºçœçš„Javac¾~–译器ä‹É用JDK1.3的语法规则ã€?/p> <p>一个简单的例子如下åQ?/p> <p>javac <wbr> <wbr> <wbr> <wbr> <wbr> -source <wbr> <wbr> 1.4 <wbr> <wbr> <wbr> test.java</p> <p><strong>1</strong><strong>åQ?/strong><strong>5)</strong> <strong>˜qè¡Œ</strong></p> <p>ç”׃ºŽå¸¦æœ‰assert语句的程序运行时åQŒä‹É用了新的ClassLoaderå’ŒClass¾c»ï¼Œå› æ­¤åQŒè¿™¿Uç¨‹åºå¿…™åÕdœ¨JDK1.4(或者更高版æœ?çš„JRE下运行,而不能在老版本的JRE下运行ã€?/p> <p>1. <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> ç”׃ºŽæˆ‘们可以选择开启assertion功能åQŒæˆ–者不开启,另外我们˜q˜å¯ä»¥å¼€å¯ä¸€éƒ¨åˆ†¾cÀLˆ–包的assertion功能åQŒæ‰€ä»¥è¿è¡Œé€‰é¡¹å˜å¾—有些复杂。通过˜q™äº›é€‰é¡¹åQŒæˆ‘们可以过滤所有我们不兛_¿ƒçš„ç±»åQŒåªé€‰æ‹©æˆ‘们兛_¿ƒçš„类或包来观察ã€?/p> <h3><a name="2">§1.1.2 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> assertion</a>的设计问é¢?/h3> <p>首先åQŒæˆ‘们认为assertion是必要的。因为,如果没有¾lŸä¸€çš„assertion机制åQŒJava½E‹åºé€šå¸¸ä½¿ç”¨if-then-else或者switch-case语句˜q›è¡Œassertion‹‚€æŸ¥ï¼Œè€Œä¸”‹‚€æŸ¥çš„æ•°æ®¾cÕdž‹ä¹Ÿä¸å®Œå…¨ç›¸åŒã€‚assertion机制让Java½E‹åºå‘˜ç”¨¾lŸä¸€çš„æ–¹å¼å¤„理assertion问题åQŒè€Œä¸æ˜¯æŒ‰è‡ªå·±çš„æ–¹å¼å¤„理。另外,如果用户使用自己的方式进行检查,那么˜q™äº›ä»£ç åœ¨å‘布以后仍然将起作用,˜q™å¯èƒ½ä¼šå½±å“½E‹åºçš„æ€§èƒ½ã€‚而从语言­a€å±‚次支持assertion功能åQŒè¿™ž®†æŠŠassertionå¯ÒŽ€§èƒ½å¸¦æ¥çš„负面媄响降到最ž®ã€?/p> <p>Java是通过增强一个关键字assert实现支持assertionåQŒè€Œä¸æ˜¯ä‹É用一个库函数支持åQŒè¿™è¯´æ˜ŽJavaè®¤äØ“assertion对于语言本èín来说是非帔R‡è¦çš„。实际上åQŒåœ¨Java的早期的规范中,Java是能够支持assert的,但是ç”׃ºŽä¸€äº›å®žçŽ°çš„é™åˆ¶åQŒè¿™äº›ç‰¹æ€§ä»Žè§„范中除åŽÖMº†ã€‚因此,assert的再‹Æ¡å¼•入应该是恢复了Java对assert的支持。C语言ž®±æ˜¯é€šè¿‡Assert.h函数库实现断­a€çš„æ”¯æŒã€?/p> <p>Javaçš„assertion的开启也和Cè¯­è¨€ä¸å¤ªä¸€æ øP¼Œæˆ‘们都知道在C语言中,assertion的开启是在编译时候决定的。当我们使用debug方式¾~–译½E‹åºæ—¶å€™ï¼Œassertion被开启,而ä‹É用release方式¾~–译时候,assertion自动被关闭。而Javaçš„assertion却是在运行的时候进行决定的。其实,˜q™ä¸¤¿Uæ–¹å¼æ˜¯å„有优缺炏V€‚如果采用编译时军_®šæ–¹å¼åQŒå¼€å‘äh员将处理两种¾cÕdž‹çš„目标码åQŒdebug版本和release版本åQŒè¿™åŠ å¤§äº†æ–‡æ¡£ç®¡ç†çš„éš‘Öº¦åQŒä½†æ˜¯æé«˜äº†ä»£ç çš„运行效率。Java采用˜qè¡Œæ—¶å†³å®šçš„æ–¹å¼åQŒè¿™æ äh‰€æœ‰çš„assertion信息ž®†ç½®äºŽç›®æ ‡ä»£ç ä¸­åQŒåŒä¸€ç›®æ ‡ä»£ç å¯ä»¥é€‰æ‹©ä¸åŒæ–¹å¼˜qè¡ŒåQŒå¢žå¼ºç›®æ ‡ä»£ç çš„灉|´»æ€§ï¼Œä½†æ˜¯å®ƒå°†ç‰ºç‰²å› äØ“assertion而引起一部分性能损失。Java专家ž®ç»„è®¤äØ“åQŒæ‰€ç‰ºç‰²çš„æ€§èƒ½ç›¸å½“ž®ï¼Œå› æ­¤java采用了运行时军_®šæ–¹å¼ã€?/p> <p>另外åQŒæˆ‘们注意到AssertionErrorä½œäØ“Error的一个子¾c»ï¼Œè€Œä¸æ˜¯RuntimeException。关于这一点,专家¾l„也˜q›è¡Œäº†é•¿æœŸçš„讨论。Error代表一些异常的错误åQŒé€šå¸¸æ˜¯ä¸å¯ä»¥æ¢å¤çš„,而RuntimeExceptionå¼ø™°ƒè¯¥é”™è¯¯åœ¨˜qè¡Œæ—¶æ‰å‘生的特炏V€‚AssertionError通常为非常关键的错误åQŒè¿™äº›é”™è¯¯å¾€å¾€æ˜¯ä¸å®ÒŽ˜“恢复的,而且assertion机制也不鼓励½E‹åºå‘˜å¯¹˜q™ç§é”™è¯¯˜q›è¡Œæ¢å¤ã€‚因此,ä¸ÞZº†å¼ø™°ƒassertion的含义,Java专家ž®ç»„选择了让AssertError为Error的子¾c…R€?/p> <h3><a name="3">§1.1.3 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> assertion</a>与ç‘ôæ‰?/h3> <p>在本节,我们ž®†è€ƒè™‘assertion与ç‘ô承的关系åQŒç ”½I¶assert是如何定位的。如果开启一个子¾cȝš„assertionåQŒé‚£ä¹ˆå®ƒçš„父¾cȝš„assertion是否执行åQ?/p> <p>下面的例子将昄¡¤ºå¦‚果一个assert语句在父¾c»ï¼Œè€Œå½“它的子类调用它时åQŒè¯¥assert为false。我们看看在不同的情况下åQŒè¯¥assertion是否被处理ã€?/p> <p>class Base</p> <p>{</p> <p> <wbr> public void baseMethod()</p> <p> <wbr> { // æ€ÀL˜¯assertionå¤ÞpÓ|</p> <p> <wbr> <wbr> <wbr> assert <wbr> <wbr> <wbr> <wbr> <wbr> false : "Assertion failed:This is base ";</p> <p> <wbr> <wbr> <wbr> System.out.println("Base Method");</p> <p> <wbr> }</p> <p>}</p> <p> <wbr></p> <p>class Derived</p> <p> <wbr> extends Base</p> <p>{</p> <p> <wbr> public void derivedMethod()</p> <p> <wbr> { // æ€ÀL˜¯assertionå¤ÞpÓ|</p> <p> <wbr> <wbr> <wbr> assert false: "Assertion failed:This is derive";</p> <p> <wbr> <wbr> <wbr> System.out.println( "Derived Method" );</p> <p> <wbr> }</p> <p> <wbr></p> <p> <wbr> public static void main( String[] args )</p> <p> <wbr> {</p> <p> <wbr> <wbr> <wbr> try</p> <p> <wbr> <wbr> <wbr> {</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> Derived derived = new Derived();</p> <p> <wbr></p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> derived.baseMethod( <wbr> );</p> <p> <wbr></p> <p> <wbr> <wbr> <wbr> <wbr>  <wbr>derived.derivedMethod();</p> <p> <wbr> <wbr> <wbr> }</p> <p> <wbr> <wbr> <wbr> catch( AssertionError ae )</p> <p> <wbr> <wbr> <wbr> {</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> System.out.println(ae);</p> <p> <wbr> <wbr> <wbr> }</p> <p> <wbr> }</p> <p>}</p> <table border="1" cellpadding="0" width="98%"> <tbody> <tr> <td width="26%"> <p>˜qè¡Œå‘½ä×o</p> </td> <td width="32%"> <p>含义</p> </td> <td width="40%"> <p>¾l“æžœ</p> </td> </tr> <tr> <td width="26%"> <p>Java Derived</p> </td> <td width="32%"> <p>不启用assertion</p> </td> <td width="40%"> <p>Base Method<br /> Derived Method</p> </td> </tr> <tr> <td width="26%"> <p>Java -ea Derived</p> </td> <td width="32%"> <p>开启所有assertion</p> </td> <td width="40%"> <p>Java.lang.AssertionError:Assertion Failed:This is base</p> </td> </tr> <tr> <td width="26%"> <p>Java -da Derived</p> </td> <td width="32%"> <p>关闭所有assertion</p> </td> <td width="40%"> <p>Base Method<br /> Derived Method</p> </td> </tr> <tr> <td width="26%"> <p>Java -ea:Base Derived</p> </td> <td width="32%"> <p>仅打开Baseçš„assertion</p> </td> <td width="40%"> <p>Java.lang.AssertionError:Assertion Failed:This is base</p> </td> </tr> <tr> <td width="26%"> <p>Java -ea:Derived Derived</p> </td> <td width="32%"> <p>仅打开Derivedçš„assertion</p> </td> <td width="40%"> <p>Base Method<br /> Java.lang.AssertionError:Assertion Failed:This is derived</p> </td> </tr> </tbody> </table> <p>从这个例子我们可以看出,父类的assert语句ž®†åªæœ‰åœ¨çˆ¶ç±»çš„assert开启才起作用,如果仅仅开启子¾cȝš„assertåQŒçˆ¶¾cȝš„assert仍然不运行。例如,我们执行java -ea:Derived Derived的时候,Base¾cȝš„assert语句òq¶ä¸æ‰§è¡Œã€‚å› æ­¤ï¼Œæˆ‘ä»¬å¯ä»¥è®¤äØ“åQŒassert语句不具有ç‘ô承功能ã€?/p> <h3><a name="4">§1.1.4 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> assertion</a>çš„ä‹Éç”?/h3> <p>assertionçš„ä‹É用是一个复杂的问题åQŒå› ä¸ø™¿™ž®†æ¶‰åŠåˆ°½E‹åºçš„风æ û|¼Œassertion˜qç”¨çš„目标,½E‹åºçš„æ€§è´¨½{‰é—®é¢˜ã€‚通常来说åQŒassertion用于‹‚€æŸ¥ä¸€äº›å…³é”®çš„å€û|¼Œòq¶ä¸”˜q™äº›å€¼å¯¹æ•´ä¸ª½E‹åºåQŒæˆ–者局部功能的完成有很大的影响åQŒåƈ且这¿Ué”™è¯¯ä¸å®ÒŽ˜“恢复的。assertion表达式应该短ž®ã€æ˜“懂,如果需要评估复杂的表达式,应该使用函数计算。以下是一些ä‹É用assertion的情å†ëŠš„例子åQŒè¿™äº›æ–¹å¼å¯ä»¥è®©java½E‹åºçš„可靠性更高ã€?/p> <p align="left">1. <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> ‹‚€æŸ¥æŽ§åˆ¶æµåQ›åœ¨if-then-elseå’Œswith-case语句中,我们可以在不应该发生的控制支‹¹ä¸ŠåŠ ä¸Šassert false语句。如果这¿Uæƒ…况发生了åQŒassert能够‹‚€æŸ¥å‡ºæ¥ã€?br /> 例如åQšx取值只能ä‹É1,2,3åQŒæˆ‘们的½E‹åºå¯ä»¥å¦‚下表示</p> <p align="left">2. <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> switch (x)</p> <p>3. <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> { case 1: …;</p> <p>4. <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>case 2: …;</p> <p>5. <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>case 3: …</p> <p>6. <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>  <wbr> <wbr>default: assert false:"x value is invalid: "+x;</p> <p>7. <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> }</p> <p align="left">8. <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 在私有函数计½Ž—前åQŒæ£€æŸ¥è¾“入参数是否有效;对于一¿Uæœ‰äº›å‡½æ•ŽÍ¼Œè¦æ±‚输入满èƒö一些特定的条äšgåQŒé‚£ä¹ˆæˆ‘们可以在函数开始处使用assert˜q›è¡Œå‚æ•°‹‚€æŸ¥ã€‚对于公共函敎ͼŒæˆ‘们通常不ä‹É用assertion‹‚€æŸ¥ï¼Œå› äؓ一般来è¯ß_¼Œå…¬å…±å‡½æ•°å¿…é¡»å¯ÒŽ— æ•ˆçš„参数˜q›è¡Œ‹‚€æŸ¥å’Œå¤„理。而私有函数往往是直接ä‹É用的ã€?br /> 例如åQšæŸå‡½æ•°å¯èƒ½è¦æ±‚è¾“å…¥çš„å‚æ•°å¿…™åÖM¸ä¸ºnull。那么我们可以在函数的一开始加ä¸?assert parameter1!=null : "paramerter is null in test method";</p> <p align="left">9. <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 在函数计½Ž—后åQŒæ£€æŸ¥å‡½æ•°ç»“果是否有效;对于一些计½Ž—函敎ͼŒå‡½æ•°˜qè¡Œå®ŒæˆåŽï¼ŒæŸäº›å€¼éœ€è¦ä¿è¯ä¸€å®šçš„æ€§è´¨åQŒå› æ­¤æˆ‘们可以通过assert‹‚€æŸ¥è¯¥å€¹{€?br /> 例如åQŒæˆ‘们有一个计½Ž—绝对值的函数åQŒé‚£ä¹ˆæˆ‘们就可以在函数的¾l“果处,加上一个语句:</p> <p align="left">assert <wbr> value>=0:"Value should be bigger than 0:"+value;</p> <p align="left">通过˜q™ç§æ–¹å¼åQŒæˆ‘们可以对函数计算完的¾l“æžœ˜q›è¡Œ‹‚€æŸ¥ã€?/p> <p align="left">10. <wbr> <wbr> ‹‚€æŸ¥ç¨‹åºä¸å˜é‡åQ›æœ‰äº›ç¨‹åºä¸­åQŒå­˜åœ¨ä¸€äº›ä¸å˜é‡åQŒåœ¨½E‹åºçš„运行生命周期,˜q™äº›ä¸å˜é‡çš„值都是不变的。这些不变量可能是一个简单表辑ּåQŒä¹Ÿå¯èƒ½æ˜¯ä¸€ä¸ªå¤æ‚的表达式。对于一些关键的不变量,我们可以通过assert˜q›è¡Œ‹‚€æŸ¥ã€?br /> 例如åQŒåœ¨ä¸€ä¸ªèƒ¦ä¼šç³»¾lŸä¸­åQŒå…¬å¸çš„æ”¯å‡ºå’Œæ”¶å…¥å¿…™åÖM¿æŒä¸€å®šçš„òqŒ™¡¡å…³ç³»åQŒå› æ­¤æˆ‘们可以编写一个表辑ּ‹‚€æŸ¥è¿™¿Uåã^è¡¡å…³¾p»ï¼Œå¦‚下表示ã€?/p> <p align="left">11. <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>private boolean isBalance() {</p> <p align="left">12. <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>……</p> <p align="left">13. <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>}</p> <p align="left"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>  <wbr></p> <p align="left">在这个系¾lŸä¸­åQŒåœ¨ä¸€äº›å¯èƒ½åª„响这¿Uåã^è¡¡å…³¾pÈš„æ–ÒŽ³•的前后,我们都可以加上assert验证åQšassert isBalance():"balance is destoried";</p> <h3><a name="5">§1.1.5 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> ¾l“论</a></h3> <p>assertion为开发äh员提供了一¿Uçµ‹zÕdœ°è°ƒè¯•和测试机åˆÓž¼Œå®ƒçš„使用也非常简单、方ä¾Ñ€‚但是,如何规范、系¾lŸåœ°ä½¿ç”¨assertion(特别是在Java语言ä¸?仍然是一个亟待研½I¶çš„问题</p> </div> <img src ="http://www.aygfsteel.com/balajinima/aggbug/298533.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/balajinima/" target="_blank">李云æ³?/a> 2009-10-16 11:38 <a href="http://www.aygfsteel.com/balajinima/articles/298533.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>½W¬ä¸€éƒ¨åˆ†åQšå£°æ˜Žå’Œè®‰K—®æŽ§åˆ¶(¾lƒä¹ é¢?http://www.aygfsteel.com/balajinima/articles/298532.html李云æ³?/dc:creator>李云æ³?/author>Fri, 16 Oct 2009 03:37:00 GMThttp://www.aygfsteel.com/balajinima/articles/298532.htmlhttp://www.aygfsteel.com/balajinima/comments/298532.htmlhttp://www.aygfsteel.com/balajinima/articles/298532.html#Feedback0http://www.aygfsteel.com/balajinima/comments/commentRss/298532.htmlhttp://www.aygfsteel.com/balajinima/services/trackbacks/298532.html½W¬ä¸€éƒ¨åˆ†åQšå£°æ˜Žå’Œè®‰K—®æŽ§åˆ¶

  • 知道怎么样声明、创建和初始化各¿Uç±»åž‹çš„æ•°ç»„
  • 知道怎么样声明类、内部类、方法、成员变量、静态变量和æ–ÒŽ³•内部变量åQŒåƈ会ä‹É用合法的修饰½W¦ï¼ˆå¦‚public,final,static,abstract,½{‰ç­‰åQ‰ã€‚了解这些修饰符单独使用或组合ä‹É用的含义åQŒåƈ且知道它们对于包作用域的影响ã€?
  • 了解¾cÈš„æž„造器åQˆconstructoråQ?
  • ¾l™å®šä¸€ä¸ªæ–¹æ³•,能判断它的合法返回类åž?

§1.1.1     一

In the following pieces of code, A and D will compile without any error. True/False?

A: StringBuffer sb1 = "abcd";

B: Boolean b = new Boolean("abcd");

C: byte b = 255;

D: int x = 0x1234;

E: float fl = 1.2;

 

Choices:

A. True

B. False

――――――――――――――――――――――――――――――�/p>

½{”案 B. The code segments B and D will compile without any error. A is not a valid way to construct a StringBuffer, you need to creat a StringBuffer object using "new". B is a valid construction of a Boolean (any string other than "true" or "false" to the Boolean constructor will result in a Boolean with a value of "false"). C will fail to compile because the valid range for a byte is -128 to +127 (ie, 8 bits,signed). D is correct, 0x1234 is the hexadecimal representation in java. E fails to compile because the compiler interprets 1.2 as a double being assigned to a float (down-casting), which is not valid. You either need an explicit cast (as in "(float)1.2") or "1.2f", to indicate a float.

 

 

 

§1.1.2       äº?/a>

What will be the result of executing the following code?

Given that Test1 is a class.

1. Test1[] t1 = new Test1[10];

2. Test1[][] t2 = new Test1[5][];

3. if (t1[0] == null)

4. {

5.t2[0] = new Test1[10]

6.t2[1] = new Test1[10]

7.t2[2] = new Test1[10]

8.t2[3] = new Test1[10]

9.t2[4] = new Test1[10]

10. }

11. System.out.println(t1[0]);

12. System.out.println(t2[1][0]);

Choices:

a. The code will not compile because the array t2 is not initialized in an unconditional statement before use.

b. The code will compile but a runtime exception will be thrown at line 12.

c. The code will compile but a runtime exception will be thrown at line 11.

d. None of these.

――――――――――――――――――――�/p>

D is correct. Though we cannot use local variables without initializing them (compilation error), there is an exception to it. In case of arrays initialization is supposed to be complete when we specify the leftmost dimension of the array. The problem occurs at runtime if we try to access an element of the array which has not been initialized (specification of size). In the question above the array t2 is initialized before use, therefore there will be no problem at runtime also and the lines 11 and 12 will both print null.

 

§1.1.3       ä¸?/a>

Which declarations of identifiers are legal?

 A. $persons

  B. TwoUsers

  C. *point

  D. this

    E. _endline

  

  ½{”案 A,B,E

  

  解析 Java的标识符可以以一个Unicode字符åQŒä¸‹æ»‘线åQˆ_åQ‰ï¼Œ¾ŸŽå…ƒ½W¦ï¼ˆ$åQ‰å¼€å§‹ï¼ŒåŽç®‹å­?/p>

½W¦å¯ä»¥æ˜¯å‰é¢çš„符号和数字åQŒæ²¡æœ‰é•¿åº¦é™åˆÓž¼Œå¤§å°å†™æ•æ„Ÿï¼Œä¸èƒ½æ˜¯ä¿ç•™å­—ã€?/p>

 

§1.1.4       å›?/a>

Which of the following answer is correct to express the value 8 in octal number?

  A. 010

  B. 0x10

  C. 08

  D. 0x8

 

  ¾˜»è¯‘

  下面的哪些答案可以用以表½Cºå…«˜q›åˆ¶å€?ã€?/p>

  ½{”案 A

 

  解析  八进制å€ég»¥0å¼€å¤ß_¼Œä»?x开头的为十六进制å€û|¼Œå…«è¿›åˆ¶ä¸­ä¸èƒ½å‡ºçŽ°æ•°å­—8åQŒæœ€å¤§åªæœ?ã€?/p>

 

§1.1.5       äº?/a>

Which are not Java keywords?

  A. TRUE

  B. sizeof

  C. const

  D. super

  E. void

 

  ¾˜»è¯‘

  哪些不是Java关键字�/p>

 

  ½{”案 A,B

 

  解析

   AåQšä¸æ˜¯ï¼ŒJava中有true,但是˜q™ä¹Ÿä¸æ˜¯å…³é”®å­—而是字面量(literalåQ‰ã€?/p>

 BåQšä¸æ˜¯ï¼ŒJava中不需要这个操作符åQŒæ‰€æœ‰çš„¾cÕdž‹åQˆåŽŸå§‹ç±»åž‹ï¼‰çš„å¤§ž®éƒ½æ˜¯å›ºå®šçš„ã€?/p>

 C、D、E都是åQŒéœ€è¦è¯´æ˜Žçš„æ˜¯const是java中未被ä‹É用的关键字ã€?/p>

 

§1.1.6       å…?/a>

Which statements about Java code security are true?

  A. The bytecode verifier loads all classes needed for the execution of a program.

  B. Executing code is performed by the runtime interpreter.

  C. At runtime the bytecodes are loaded, checked and run in an interpreter.

  D. The class loader adds security by separating the namespaces for the

classes of the local file system from those imported from network sources.

 

―――――――――――――�/p>

½{”案 BCD

 

§1.1.7       ä¸?/h3>

  题目åQšä¸‹é¢æœ‰å…³java代码安全性的叙述哪些是对的ã€?/p>

  A. 字节码校验器加蝲查询执行需要的所有类�/p>

 

  B. ˜qè¡Œæ—¶è§£é‡Šå™¨æ‰§è¡Œä»£ç ã€?/p>

 

  C. 在运行时åQŒå­—节码被加载,验证然后在解释器里面˜qè¡Œã€?/p>

 

  D. ¾cÕdŠ è½½å™¨é€šè¿‡åˆ†ç¦»æœ¬æœºæ–‡äšg¾pȝ»Ÿçš„类和从¾|‘络导入的类增加安全性ã€?/p>

 

  SL275中描˜q°çš„Java½E‹åº˜qè¡Œçš„过½E‹æ˜¯˜q™æ ·çš„:¾cÕdŠ è½½å™¨åQˆclass loaderåQ‰åŠ è½½ç¨‹åºè¿è¡Œæ‰€éœ€è¦çš„æ‰€æœ‰ç±»åQŒå®ƒé€šè¿‡åŒºåˆ†æœ¬æœºæ–‡äšg¾pȝ»Ÿçš„类和网¾lœç³»¾lŸå¯¼å…¥çš„¾cÕd¢žåŠ å®‰å…¨æ€§ï¼Œ˜q™å¯ä»¥é™åˆ¶ä“Q何的ç‰ÒŽ´›ä¼Šæœ¨é©¬ç¨‹åºï¼Œå› äؓ本机¾cÀL€ÀL˜¯å…ˆè¢«åŠ è²åQŒä¸€æ—¦æ‰€æœ‰çš„¾c»è¢«åŠ è²å®Œï¼Œæ‰§è¡Œæ–‡äšg的内存划分就固定了,在这个时候特定的内存地址被分配给对应的符号引用,查找表(lookuo tableåQ‰ä¹Ÿè¢«å¾ç«‹ï¼Œç”׃ºŽå†…存划分发生在运行时åQŒè§£é‡Šå™¨åœ¨å—限制的代码区增加保护防止未授权的讉K—®åQ›ç„¶åŽå­—节码校验器(byte code verifieråQ‰è¿›è¡Œæ ¡éªŒï¼Œä¸»è¦æ‰§è¡Œä¸‹é¢çš„æ£€æŸ¥ï¼š¾cȝ¬¦åˆJVM规范的类文äšg格式åQŒæ²¡æœ‰è¿åè®¿é—®é™åˆÓž¼Œä»£ç æ²¡æœ‰é€ æˆå †æ ˆçš„上溢或者下溢,所有操作代码的参数¾cÕdž‹éƒ½æ˜¯æ­£ç¡®çš„,没有非法的数据类型è{换(例如ž®†æ•´åž‹æ•°è½¬æ¢æˆå¯¹è±¡ç±»åž‹ï¼‰å‘生åQ›æ ¡éªŒé€šè¿‡çš„字节码被解释器åQˆinterpreteråQ‰æ‰§è¡Œï¼Œè§£é‡Šå™¨åœ¨å¿…要旉™€šè¿‡˜qè¡Œæ—¶ç³»¾lŸæ‰§è¡Œå¯¹åº•层¼‹¬äšg的合适调用。后三个½{”案是SL275中的原话ã€?/p>

§1.1.8       å…?/h3>

Which fragments are correct in Java source file?

  A. package testpackage;

  public class Test{//do something...}

 

  B. import java.io.*;

  package testpackage;

  public class Test{// do something...}

 

  C. import java.io.*;

  class Person{// do something...}

  public class Test{// do something...}

 

  D. import java.io.*;

  import java.awt.*;

  public class Test{// do something...}

 

------------------------

½{”案 ACD

 

§1.1.9       ä¹?/h3>

Which of the following statements are legal?

  A. long l = 4990;

  B. int i = 4L;

  C. float f = 1.1;

  D. double d = 34.4;

  E. double t = 0.9F.

----------------------------

½{”案 ADE

 



]]>
Ö÷Õ¾Ö©Öë³ØÄ£°å£º ÀäË®½­ÊÐ| ´ïÀ­ÌØÆì| µ¾³ÇÏØ| Ë«·åÏØ| Ç­Î÷ÏØ| ¶ýÔ´ÏØ| ËÕÖÝÊÐ| ¾ÆÈªÊÐ| ÆÖ³ÇÏØ| ÒËÖÝÊÐ| ʲÚúÊÐ| ½­´¨ÏØ| μÄÏÊÐ| ÉÇβÊÐ| ÕÑͨÊÐ| ·ö¹µÏØ| »ÝÀ´ÏØ| »´ÑôÏØ| ³»ÖÝÊÐ| Û°³ÇÏØ| ÖÛɽÊÐ| Ìì¾þÏØ| ÀíÌÁÏØ| ¸§Ô¶ÏØ| º«³ÇÊÐ| Ûº£ÏØ| ÍôÇåÏØ| ¶¡ÇàÏØ| Éñ³ØÏØ| Á°Ô´ÊÐ| º«³ÇÊÐ| »ÆÆÖÇø| Áé´¨ÏØ| ÐÂÖñÊÐ| ÍòÄþÊÐ| Áú´¨ÏØ| ÎäÉ½ÏØ| ÑÓ±ß| ¶«Ïç| ³à·åÊÐ| лÎ|