??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲熟妇成人精品一区,亚洲乱理伦片在线观看中字,亚洲人成777在线播放http://www.tkk7.com/balajinima/zh-cnMon, 12 May 2025 18:32:45 GMTMon, 12 May 2025 18:32:45 GMT60Java 设计模式与徏模专?Java 建模http://www.tkk7.com/balajinima/articles/299405.html李云?/dc:creator>李云?/author>Thu, 22 Oct 2009 09:31:00 GMThttp://www.tkk7.com/balajinima/articles/299405.htmlhttp://www.tkk7.com/balajinima/comments/299405.htmlhttp://www.tkk7.com/balajinima/articles/299405.html#Feedback0http://www.tkk7.com/balajinima/comments/commentRss/299405.htmlhttp://www.tkk7.com/balajinima/services/trackbacks/299405.html
l一建模语言QUMLQ基
Java 建模QUML 工作?/a>
Java 建模Q子整体软g开?/a>
Java 建模语言QJava Modeling LanguageQJMLQ?/a>



本专题ؓ Java 软g工程师们提供了面?Java 的设计模式和建模斚w相关的文章和教程。帮助读者理解、学习作Z业Y件工E师必需掌握的设计模式与建模技术?/p>
l一建模语言QUMLQ基

UML基础Q统一建模语言?/a>
回顾 20 世纪晚期 -- 准确地说?1997 q_OMG l织QObject Management Group 对象理l织Q发布了l一建模语言QUnified Modeling LanguageQUMLQ。UML 的目标之一是为开发团队提供标准通用的设计语a来开发和构徏计算机应用。UML 提出了一套IT专业人员期待多年的统一的标准徏模符受通过使用 UMLQ这些h员能够阅d交流pȝ架构和设计规?-像建筑工h多年来所使用的徏{设计图一栗?/p>

l一建模语言(UML) 版本 2.0
所谓的“模型驱动”开发(MDDQ方式,已经昄Z它们从根本性上提高软g质量和开发生产力斚w的潜力。与传统的方法相比,q种方式是基于较高层ơ上的抽象和更好的自动化利用的。由于徏模语a对MDD的成功具有关键性的作用Q所以最q完成了对基于工业标准的l一建模语言QUMLQ的主要修订。随着一些重要的新的建模能力d到其中――比如更_地获得Y件架构的能力――这ơ修订的主要Ҏ得语a定义更加_Q从而达C更高层次的自动化。这文章解释了q一Ҏ是如何实现的,q且描述?UML 2.0 的其他亮炏V?/p>

UML 基础Q类?/a>
?UML 2 中,作ؓ新结构图cd的最重要实例Q类囑֏以在整个软g开发生命周期中Q被分析师,业务建模人员Q开发者和试者用的。本文提供了全面的介l?/p>

UML 基础Q序列图
本文作ؓ UML 基础的、关于统一建模语言的基囄一pd文章的一部分Q提供对序列囄详细介绍。它也介l了最q的 UML 2.0 规范的几个新W号元g?

UML 基础Q组件图
q篇文章介绍lg图,一个在新的l一建模语言 2.0 中规定的l构图?

UML 基础Q组件图
q篇文章介绍lg图,一个在新的l一建模语言 2.0 中规定的l构图?




Java 建模QUML 工作?/a>

Java 建模QUML 工作,W?1 部分Q序列图?
Granville Miller 在其C栏的W一部分中介l了“l一建模语言”(UML) 的一个构Ӟ序列图。在整个设计q程中都会用到序列图Q此囄于演C系l执行时参与者与对象之间的内部交互。让我们跟着 Granville 一起创建其中一个图Q我们将使用一个贷Ƒ֤理应用程序作为示例?/p>

Java 建模Q?UML 工作:W?2 部分Q序列图中的条g逻辑
Granville l箋讨论“l一建模语言”和序列图的绘制。他仔细研究了序列图l制q程中条仉辑的角Ԍq讨ZZ么要在图中包含或排除条g和@环。Granville q描qC序列囄两种形?-- 常规和实?-- q说明了它们在开发周期中各自的应用?/p>

Java 建模Q?UML 工作,W?3 部分Q在用例建模上的用户接口逻辑
在这一部分?Java 建模中,Granville 引领您进入介于徏模和Ҏ之间的区域,同时看一下通过用例建模所攉的需求。他特别着重讨Z用户接口、系l接口和用例描述之间的关pR尽现在正试图在用例中包括用户接口逻辑Q但q通常被认为是不好的Ş式。接着Q?Grancille 用序列图和系l接口告诉您具体原因。请点击文章剙或底部的讨论Q参与讨坛,与本文作者和其他读者分享您Ҏ文的x?

Java 建模QUML 工作,W?4 部分
本文深入讨论用例囄基本lg之一Q参与者(actorQ。参与者不仅在 UML 建模中不可或~,而且在创?Java 应用E序Ӟ它也能v到很重要的作用,甚至可以?J2EE 应用E序设计中的模式提出。对于开发诸?Web 服务Q在其系l设计中Q外部交互扮演了很重要的角色Q这L复杂pȝQ参与者变得尤光要。Granville 用序列图和类图来阐述参与者在用例囄l制?Java 应用E序开发中的角艌Ӏ?




Java 建模Q子整体软g开?/a>

Java 建模Q子整体软g开发,W?1 部分Q宣a
Granville Miller 暂时攑ּ需求收集主题,着手讨论另一个引人入胜的主题Q子整体软g~程?让我们找找这个方法如何补充和扩展灉|开发运动原则,以及它在L开发界中的出现如何可能改变软g开发者的教育和Y件开发实c请?讨论论坛与作者和其他读者分享您关于本文的想法?

Java 建模Q子整体软g开发,W?2 部分Q需求收集:工作的恰当过E?/a>
Granville Miller l箋他关于子整体软g开发的讨论Qƈ在概念上寚w求收集作了概括?让我们看看四个最常见的需求收集过E??功能Ҏ、用h景、用例和传统的Y仉求规??怎样适应灉|的Y件开发过E更qK的环境?请在 讨论论坛与作者和其他读者分享您关于本文的想法?q程太少Q非凡的做^凡的事; q程太多Q即使是非凡的h也不能做非凡的事?




Java 建模语言QJava Modeling LanguageQJMLQ?/a>

JML 入门
Java 建模语言QJava Modeling LanguageQJMLQ是一U用来进行详l设计的表示法(notationQ,它倡导一U思考方法和cȝ新思\。在q篇入门文章中,Java ~程N Joe Verzulli 介绍?JML 及其一些最重要的说明构造?



]]>
Java 设计模式与徏模专?设计模式http://www.tkk7.com/balajinima/articles/299404.html李云?/dc:creator>李云?/author>Thu, 22 Oct 2009 09:27:00 GMThttp://www.tkk7.com/balajinima/articles/299404.htmlhttp://www.tkk7.com/balajinima/comments/299404.htmlhttp://www.tkk7.com/balajinima/articles/299404.html#Feedback0http://www.tkk7.com/balajinima/comments/commentRss/299404.htmlhttp://www.tkk7.com/balajinima/services/trackbacks/299404.html

Factory Method 模式
Singleton 模式
Observer 模式
State 模式
控制反{QIoCQ模?/a>
Single Call 模式
其它模式
反模?/a>
?Java cd看设计模?/a>
使用设计模式改善E序l构



本专题ؓ Java 软g工程师们提供了面?Java 的设计模式和建模斚w相关的文章和教程。帮助读者理解、学习作Z业Y件工E师必需掌握的设计模式与建模技术?/p>
Factory Method 模式

Factory Method 模式?Javamail 中的应用
设计模式在Y件工E中占有重要CQ?JavaMail ?Java q_的一个扩展,为管理电子邮件提供了l一的应用编E接口。本文讨?Factory Method 设计模式在Javamail 中的应用?

?Java 中应用设计模?- Factory Method
在设计模式中,Factory Method也是比较单的一?但应用非常广泛,EJB,RMI,COM,CORBA,Swing中都可以看到此模式的影子Q它是最重要的模式之一。在很多地方我们都会看到xxxFactoryq样命名的类,那么,什么是Factory Method,Z么要用这个模?如何用Java语言来实现该模式,q就是本文想要带l大家的内容?

Factory 模式的优?/a>
最常见的构造一?Object 的方法是通过构造函? ? String strTemp = new String('Hello World'); 其实q有一U一U更灉|的创建Object 的方?--利用Factory 模式. q是著名?四h?(Gangs Of Four)的经怹?设计模式'[(Design Pattern) 所记蝲?3模式之一. 它有着构造函C可比拟的一些优?




Singleton 模式

?Java 中应用设计模?-- Singleton
本文介绍了设计模式中 Singleton 的基本概?对其功能和用途进行了单的分析,列出了通常实现 Singleton 的几U方?q给Z详细的Java 代码?




Observer 模式

Observer 模式?J2EE 中的实现
设计模式是经验的文档化。它是对被用来在特定场景下解决一般设计问题的cd怺通信的对象的描述。更通俗的来_它是一个问?解决Ҏ寏V一旦我们掌握了设计模式Q就{于拥有了一支强有力的专安伍。它甚至能够佉K向对象的新手利用前h的经验找责明的cd对象Q从而获得优雅的解决Ҏ?




State 模式

State 模式在客L软g中的应用
在对一?J2EE 目的重构、增加新功能的过E中Q对客户?GUI E序Q我们用了 State 模式。结果显C,该模式的使用Q不但减了客户?GUI E序的程序规模(LOCQ,而且Q该部分的开发及单元试旉大大减少Q同Ӟ在集成测试中发现的缺h量比使用该模式前q_减少?倍。本文就该项目中使用 State 模式的方式进行介l?




控制反{QIoCQ模?/a>

在方法签名中使用控制反{QIoCQ?/a>
控制反{QIoCQ模式通常用于lg。本文描qC如何Ҏ法签名用该模式Q以减少lg间的耦合q改善性能。IBM Global Business Services N Andr Fachat 用两个例子展CZq种Ҏ的灵zL?




Single Call 模式

Single Call 模式
在本文中Q作者向大家讲述?Single Call 模式的原理,同时也介l了 Single Call 模式的实现问题?




其它模式

Java 设计模式 201Q超四人组
您可以在本教E中发现Q到底还有多设计模式您不知道。首先,我们会探讨一些资源,学习设计模式的新手通常会忽略它们。这些资源ؓ各种计算领域Q如商业应用E序、Web 应用E序Q甚?Web 设计Q提供了极ؓ有用的模式?

界面l装器模?/a>
本文提出了一U界面设计中的架构模式-界面l装器模式,它致力于分解界面Q将界面和组装行耦,界面逻辑处理与领域逻辑处理解耦,q样我们在开?GUI 胖客L界面应用时可以从众多的界面控制管理中解脱出来Q而专注于我们的后C务逻辑的开发。通过该模式,我们可以动态地l装我们的界面,我们甚至q可以在我们的界面中L地插?transaction 事务?session 会话理?

数据校验器架构模式组
本文阐述软g架构与设计模式,它ؓ架构师和开发h员提供了一l关于数据校验的架构模式Q隔L验器Q可l装校验器,动态策略校验器Q动态注册校验器{)Q数据校验是Mcd的开发中都不可或~的环节Q如果没有统一的架构,可能校验代码会遍布整个应用,如何数据校验与应用逻辑解耦,如何适应各种_度的数据和各种复杂E度业务规则Q正是本文要探讨的?




反模?/a>

反模式可以如何提高编E?/a>
设计模式对Y件开发来说很重要Q这一点从它在技术N易新M所占的数量可见一斑。不q,鉴于其在开发过E中的实用性,设计模式只解决了问题的一半。反模式 ?描述“对生绝对负面结果的问题的一U常用解x?#8221; ?旨在通过?Java E序员展C如何避免常见的 Java 陷阱来解决问题的另一半?




?Java cd看设计模?/a>

?Java cd看设计模式,W一部分
在这一部分的内容中Q介l的是一个相对简单但功能强大的模式:Observer 模式。希望通过q部分地叙述Q大家看了之后,能够对设计模式有一个比较全面地Q感性的认识?

?Java cd看设计模式,W二部分
q部分及以后的内容,会步入正题Q从 Java cd的分析入手,来阐叙设计模式是如何应用C个完的设计中的。实际上QJava cd非常的庞杂,q儿不可能把所有能够找到的设计模式的例子一一列DQ只是找了一些容易发现的例子。实际上也没有必要,因ؓ只要对一个设计模式有_的理解,对于它的具体应用而言Q倒是一件不是很困难的事情?

?Java cd看设计模式,W三部分
主要介绍几个l构型的模式?Bridge 模式?Decorator 模式。对?Bridge 模式可能需要更多的理解Q因为它在很大程度上_例示了设计模式的基本的设计思\和原则?

?Java cd看设计模式,W四部分
在上一部分中,介绍了两个结构型的模式:Bridge和Decorator。这一部分的内容,会接着上面的讲解,l箋我们的设计模式之旅?

?Java cd看设计模式,W五部分
有了前面诸多设计模式的基Q这儿可以提Z个比较特D的模式 MVC。MVCq不属于 GOF ?23 个设计模式之列,但是它在 GOF 的书中作Z个重要的例子被提出来Qƈl予了很高的评h。一般的来讲Q我们认为GOF?3个模式是一些中U的模式Q在它下面还可以抽象Z些更Z般的低层的模式,在其上也可以通过l合来得C些高U的模式。MVC可以看作是一些模式进行组合之后的l果Q实际上QMVC的出现要早于设计模式的提出,q而只是对它在设计模式的基上进行在分析Q。如果没有前面的基础Q理解MVC或许会有一些困难?




使用设计模式改善E序l构

使用设计模式改善E序l构Q一Q?/a>
设计模式是对特定问题l过无数ơ经验ȝ后提出的能够解决它的优雅的方案。但是,如果惌真正使设计模式发挥最大作用,仅仅知道设计模式是什么,以及它是如何实现的是很不够的Q因为那样就不能使你对于设计模式有真正的理解Q也׃能够在自q设计中正、恰当的使用设计模式。本文试图从另一个角度(设计模式的意图、动机)来看待设计模式,通过q种新的思\Q设计模式会变得非常贴近你的设计q程Qƈ且能够指对{简化你的设计,最l将会导Z个优U的解x案?

使用设计模式改善E序l构Q二Q?/a>
在本pd的第一文章中Q描qC如何通过设计模式来指导我们的E序重构q程Qƈ且着重介l了设计模式意图、动机的重要性。在本文中我们将l箋上篇文章q行讨论Q这ơ主要着重于设计模式的适用性,对于设计模式适用性的掌握有助于从另一个不同的斚w来判断一个设计模式是否真正适用于我们的实际问题Q从而做出明智的选择?

使用设计模式改善E序l构Q三Q?/a>
设计模式在某U程度上实能够改善我们的程序结构,使设计具有更好的Ҏ。也正是׃q个原因Q会D我们可能q度的用它。程序结构具有过度的、不必要的灵zL和E序l构没有灉|性一样都是有害的

设计模式 Java 建模

Factory Method 模式
Singleton 模式
Observer 模式
State 模式
控制反{QIoCQ模?/a>
Single Call 模式
其它模式
反模?/a>
?Java cd看设计模?/a>
使用设计模式改善E序l构



本专题ؓ Java 软g工程师们提供了面?Java 的设计模式和建模斚w相关的文章和教程。帮助读者理解、学习作Z业Y件工E师必需掌握的设计模式与建模技术?/p>
Factory Method 模式

Factory Method 模式?Javamail 中的应用
设计模式在Y件工E中占有重要CQ?JavaMail ?Java q_的一个扩展,为管理电子邮件提供了l一的应用编E接口。本文讨?Factory Method 设计模式在Javamail 中的应用?

?Java 中应用设计模?- Factory Method
在设计模式中,Factory Method也是比较单的一?但应用非常广泛,EJB,RMI,COM,CORBA,Swing中都可以看到此模式的影子Q它是最重要的模式之一。在很多地方我们都会看到xxxFactoryq样命名的类,那么,什么是Factory Method,Z么要用这个模?如何用Java语言来实现该模式,q就是本文想要带l大家的内容?

Factory 模式的优?/a>
最常见的构造一?Object 的方法是通过构造函? ? String strTemp = new String('Hello World'); 其实q有一U一U更灉|的创建Object 的方?--利用Factory 模式. q是著名?四h?(Gangs Of Four)的经怹?设计模式'[(Design Pattern) 所记蝲?3模式之一. 它有着构造函C可比拟的一些优?




Singleton 模式

?Java 中应用设计模?-- Singleton
本文介绍了设计模式中 Singleton 的基本概?对其功能和用途进行了单的分析,列出了通常实现 Singleton 的几U方?q给Z详细的Java 代码?




Observer 模式

Observer 模式?J2EE 中的实现
设计模式是经验的文档化。它是对被用来在特定场景下解决一般设计问题的cd怺通信的对象的描述。更通俗的来_它是一个问?解决Ҏ寏V一旦我们掌握了设计模式Q就{于拥有了一支强有力的专安伍。它甚至能够佉K向对象的新手利用前h的经验找责明的cd对象Q从而获得优雅的解决Ҏ?




State 模式

State 模式在客L软g中的应用
在对一?J2EE 目的重构、增加新功能的过E中Q对客户?GUI E序Q我们用了 State 模式。结果显C,该模式的使用Q不但减了客户?GUI E序的程序规模(LOCQ,而且Q该部分的开发及单元试旉大大减少Q同Ӟ在集成测试中发现的缺h量比使用该模式前q_减少?倍。本文就该项目中使用 State 模式的方式进行介l?




控制反{QIoCQ模?/a>

在方法签名中使用控制反{QIoCQ?/a>
控制反{QIoCQ模式通常用于lg。本文描qC如何Ҏ法签名用该模式Q以减少lg间的耦合q改善性能。IBM Global Business Services N Andr Fachat 用两个例子展CZq种Ҏ的灵zL?




Single Call 模式

Single Call 模式
在本文中Q作者向大家讲述?Single Call 模式的原理,同时也介l了 Single Call 模式的实现问题?




其它模式

Java 设计模式 201Q超四人组
您可以在本教E中发现Q到底还有多设计模式您不知道。首先,我们会探讨一些资源,学习设计模式的新手通常会忽略它们。这些资源ؓ各种计算领域Q如商业应用E序、Web 应用E序Q甚?Web 设计Q提供了极ؓ有用的模式?

界面l装器模?/a>
本文提出了一U界面设计中的架构模式-界面l装器模式,它致力于分解界面Q将界面和组装行耦,界面逻辑处理与领域逻辑处理解耦,q样我们在开?GUI 胖客L界面应用时可以从众多的界面控制管理中解脱出来Q而专注于我们的后C务逻辑的开发。通过该模式,我们可以动态地l装我们的界面,我们甚至q可以在我们的界面中L地插?transaction 事务?session 会话理?

数据校验器架构模式组
本文阐述软g架构与设计模式,它ؓ架构师和开发h员提供了一l关于数据校验的架构模式Q隔L验器Q可l装校验器,动态策略校验器Q动态注册校验器{)Q数据校验是Mcd的开发中都不可或~的环节Q如果没有统一的架构,可能校验代码会遍布整个应用,如何数据校验与应用逻辑解耦,如何适应各种_度的数据和各种复杂E度业务规则Q正是本文要探讨的?




反模?/a>

反模式可以如何提高编E?/a>
设计模式对Y件开发来说很重要Q这一点从它在技术N易新M所占的数量可见一斑。不q,鉴于其在开发过E中的实用性,设计模式只解决了问题的一半。反模式 ?描述“对生绝对负面结果的问题的一U常用解x?#8221; ?旨在通过?Java E序员展C如何避免常见的 Java 陷阱来解决问题的另一半?




?Java cd看设计模?/a>

?Java cd看设计模式,W一部分
在这一部分的内容中Q介l的是一个相对简单但功能强大的模式:Observer 模式。希望通过q部分地叙述Q大家看了之后,能够对设计模式有一个比较全面地Q感性的认识?

?Java cd看设计模式,W二部分
q部分及以后的内容,会步入正题Q从 Java cd的分析入手,来阐叙设计模式是如何应用C个完的设计中的。实际上QJava cd非常的庞杂,q儿不可能把所有能够找到的设计模式的例子一一列DQ只是找了一些容易发现的例子。实际上也没有必要,因ؓ只要对一个设计模式有_的理解,对于它的具体应用而言Q倒是一件不是很困难的事情?

?Java cd看设计模式,W三部分
主要介绍几个l构型的模式?Bridge 模式?Decorator 模式。对?Bridge 模式可能需要更多的理解Q因为它在很大程度上_例示了设计模式的基本的设计思\和原则?

?Java cd看设计模式,W四部分
在上一部分中,介绍了两个结构型的模式:Bridge和Decorator。这一部分的内容,会接着上面的讲解,l箋我们的设计模式之旅?

?Java cd看设计模式,W五部分
有了前面诸多设计模式的基Q这儿可以提Z个比较特D的模式 MVC。MVCq不属于 GOF ?23 个设计模式之列,但是它在 GOF 的书中作Z个重要的例子被提出来Qƈl予了很高的评h。一般的来讲Q我们认为GOF?3个模式是一些中U的模式Q在它下面还可以抽象Z些更Z般的低层的模式,在其上也可以通过l合来得C些高U的模式。MVC可以看作是一些模式进行组合之后的l果Q实际上QMVC的出现要早于设计模式的提出,q而只是对它在设计模式的基上进行在分析Q。如果没有前面的基础Q理解MVC或许会有一些困难?




使用设计模式改善E序l构

使用设计模式改善E序l构Q一Q?/a>
设计模式是对特定问题l过无数ơ经验ȝ后提出的能够解决它的优雅的方案。但是,如果惌真正使设计模式发挥最大作用,仅仅知道设计模式是什么,以及它是如何实现的是很不够的Q因为那样就不能使你对于设计模式有真正的理解Q也׃能够在自q设计中正、恰当的使用设计模式。本文试图从另一个角度(设计模式的意图、动机)来看待设计模式,通过q种新的思\Q设计模式会变得非常贴近你的设计q程Qƈ且能够指对{简化你的设计,最l将会导Z个优U的解x案?

使用设计模式改善E序l构Q二Q?/a>
在本pd的第一文章中Q描qC如何通过设计模式来指导我们的E序重构q程Qƈ且着重介l了设计模式意图、动机的重要性。在本文中我们将l箋上篇文章q行讨论Q这ơ主要着重于设计模式的适用性,对于设计模式适用性的掌握有助于从另一个不同的斚w来判断一个设计模式是否真正适用于我们的实际问题Q从而做出明智的选择?

使用设计模式改善E序l构Q三Q?/a>
设计模式在某U程度上实能够改善我们的程序结构,使设计具有更好的Ҏ。也正是׃q个原因Q会D我们可能q度的用它。程序结构具有过度的、不必要的灵zL和E序l构没有灉|性一样都是有害的



]]>
Java认证之精屁ȝhttp://www.tkk7.com/balajinima/articles/298543.html李云?/dc:creator>李云?/author>Fri, 16 Oct 2009 03:50:00 GMThttp://www.tkk7.com/balajinima/articles/298543.htmlhttp://www.tkk7.com/balajinima/comments/298543.htmlhttp://www.tkk7.com/balajinima/articles/298543.html#Feedback0http://www.tkk7.com/balajinima/comments/commentRss/298543.htmlhttp://www.tkk7.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>>32q是-1, -1>>>32Z么还?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如果不加componentC,那中q着
2.3如果某个方位有个component,如果你还往上面加component,后来的会覆盖原来?br /> 3. applet,panel默认布局是flowlayout
4. frame,dialog默认布局是borderlayout
5. 各种awecȝ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.事gcL
(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
cd对象(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ȝҎ抛出的的异常只能是父cL法抛出异常的子异常类,或者无!
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. hashCodeq回一个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.实现cd现的Ҏ必须昑ּ的声明成public,什么都不写都不??!!
5.接口不能声明成final,要不它怎么被实现呢(q好像又是废话啊
6.一个类实现两个接口,如果两个接口有相同的Ҏ,实现cd实现q个Ҏ,没问题的.

内嵌cInner Class)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1.内嵌cd以访问outercȝM变量,包括U有?
2.静态innerc?只能讉KoutercȝMstatic变量
2.1内嵌cd以是final,abstract?br /> 3.我靠,Ҏ内的内嵌cM能ؓstatic: void test(){ static class A{}} XXXXX!!!!
4.我靠,Ҏ内的内嵌cM不能带Q何modifier,void test(){ public class A{}} XXXXX!!!!
5.我靠,Ҏ内的内嵌cd能访问方法内的final变量,但是,可以讉KoutercȝM变量.
6.匿名cM能有构造器,但声明时候带参数,相当于构造器的参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.内部cd以有synchronizedҎ,那么锁是q个内部c?跟外部类没一点关p?内外分别?在锁的问题上.
10.外部cM能通过this被访?thisq时候应该指的是内部c?享用外部cȝ成员q接用,不用加Q何限定词
11.如何用this呢请?
class Outer{ int i;
class Inner{
class InnerInner{
void Test(){
Outer.this.i=1;
}
}
}
}
看见了吧,cd.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程睡觉1U?然后,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)注意Qceil,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()qtab都退?"\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,sbcd不一?不能比较
集合:
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?但必Lstatic?br /> 2.一个文件只能有一个publicc?而且q得跟文件名一?包括大小?br /> 3.变量只能是字?$,_开?接下来的W二个可以是,数字
4.ch\u0061r='a'; char \u0063='b'; char c='\u0063';都是合法?br /> 5.1e-5d,合法.e-5d不合?必须有前面的pL
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已经自动转成个intcd?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.最外面一层的cM能声明成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。不可到辄语句声明为错Qwhile(false){} ;for(;false{};if(false){}都无法编?br /> 2。for(W一部分;的第一部分可以用来声明或者赋|但不能两者都
3。byte b; switch  { case 200: // 200 not in range of byteQ因?00过b的范_编译错?br /> 4。带标签的continue回达到标{位置Q从新进入紧接在标签后面的@?br /> 5。带标签的break会中断当前@环,q{Ud标签标示的的循环的末?/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.0l果是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.Filecȝ一些重要方?isFile();isDirectory();String[] list();exists();getAbsolutePath();getParent();
2.通过delete();mkdir();rename(File newname);可以操纵文g,但是却改变不了文件的内容
2.1 FilecL法改变当前目?除非重新创徏一个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



]]>
JAVA概念ȝhttp://www.tkk7.com/balajinima/articles/298542.html李云?/dc:creator>李云?/author>Fri, 16 Oct 2009 03:49:00 GMThttp://www.tkk7.com/balajinima/articles/298542.htmlhttp://www.tkk7.com/balajinima/comments/298542.htmlhttp://www.tkk7.com/balajinima/articles/298542.html#Feedback0http://www.tkk7.com/balajinima/comments/commentRss/298542.htmlhttp://www.tkk7.com/balajinima/services/trackbacks/298542.html?strong>传g传引

严格来说QJava中所有方法参数都是传倹{因为即使是传递的参数是对象的引数Ӟ引数本n也是传值的。所有基本类型都是传值的?/p>

传|被传入的参数被视为内部的局域变量,其变化不影响外部变量的原始倹{?/p>

传引Q?/strong>对于引数本n来讲也是传值的Q但是在Ҏ内部若对引数所指向的对象属性有改变Q则会直接实时地影响所指向的对象属性?/p>

理解传引q一概念的最x式就是画出指向图。eg. Aà(Object)O 对于A本n而言它是传值的Q也是说当A作ؓ参数传递的时候,假若在方法内部把其它的引数赋l了AQ但是当Ҏq回ӞA仍旧是指向原来的对象Q而不会改变。其ơ,对于传引来说Q假若在Ҏ内部对A所指向的对象属性有改变Q那么对象属性的改变会是直接实时的?/p>

再次QJava中所有的参数传递都是传值的?/p>

传DU题型的考试很多Q基本类型传值问题考的较多的是Ҏ一变量Q故意用某一Ҏ试图改变它,然后Ҏq回时再打印它。按传值的说法Q实际上该变量ƈ没有改变?/p>

?strong>构造函?/strong>

aQ构造器没有Mq回cdQ哪怕是void也不行?strong>假如指定了返回|那么Java会视Z个普通的Ҏ?/strong>

bQ如果没有显C地调用父类的构造器QJavaL自动地调用父cȝ默认构造器。(也就是第一行会默认为super( )Q?/p>

cQ只要类中显式地定义一个或多个构造器Q那么Java不会再ؓ你定义一个默认的构造器

dQ构造函数可以被重蝲Qƈ且在其体内可以用this()和super()来调用其它的构造器。但是this()和super()只能攑֜构造函C的第一行进行调用?/p>

eQ构造器的修饰符只可以是接入控制Wpublic、private、protected?default)。其它修饰符一律不可?/p>

fQ构造方法不可被l承?/p>

?strong>重蝲与覆?/strong>

  重蝲的英文ؓOverloadQ而覆盖的英文为Override。重载是出现在同一cM多个同名函数Q而覆盖是出现在类l承体系中同名函数。(注意Q覆盖有时被UCؓ重写Q?/p>

  重蝲是依据同名方?strong>参数的个?/strong>?strong>参数的类?/strong>?strong>参数的顺?/strong>来确定的。方法的修饰W、返回倹{抛出的异常均不能作为区分重载方法的依据。(l承体系中也有重载现象)

  覆盖是在l承体系中子c覆盖超cM定义的方法。子cM覆盖Ҏ?strong>q回cd?strong>参数cd必须_匚w?strong>接入控制W?/strong>只能更加公有化;抛出的异?/strong>是超cL出的子集或不抛?/p>

     QstaticҎ不能覆盖Q?strong>privateҎ也不能覆盖。Java视它们ؓ被隐?strong>Q?/strong>

      · 参数cd一_q回cd不同Q编译错误,提示“试图用不兼容的返回类型覆?#8221;?/p>

      · 只要参数cd不一_q回cd同与不同Q编译都能通过Q此不ؓ覆盖而是重蝲?/p>

?strong>多?/strong>

  多态是出现于类的承层ơ中Q通过向上转型?strong>Ҏ重写的机制来实现的?/p>

  面向对象E序设计的目标是Q?strong>希望所撰写的程序代码都只对基类q行操作。这么一来,当加入新的承类Ӟ大部分程序代码都不会受媄响而改写(也即是说代码h扩充性)。所以当调用新加入的l承cLQ都会首先向上{型ؓ基类。这是多态的向上转型?/p>

  当你希望通过调用基类的方法而能让承类产生正确的行为时Q这昄需要在l承c进行重写该Ҏ。而究竟是该调用哪个承类Q这是由Java的动态绑定决定的?/p>

  多态最重要的一点在?strong>run-time binding。多态是面向对象E序设计的目标?/p>

  关于多态中覆盖注意如下Q?/p>

属?/strong>既可以承,也是可以“覆盖”的。但是对属性而言没有动态绑定这一Ҏ,所以覆盖的属性被认ؓ是子cȝ特别属性。从某种意义上来Ԍ属性决定了c(性质Q。另一斚wQ申明的cd决定了对象的属性。这是因为,M对象或变量等号右面是用来赋值给W合{号左面所xcd的,所以左面的cd是先xӞ赋值必要W合xcd。对于向上{型而言Q因为等号右面的对象is axcdQ所以是成立的。一定要CQ?strong>属性永q都是跟着xcd?/strong>。但是,?strong>Ҏ而言是在q行时动态绑定的Q它取决于对象自w的实际cdQ实际上Ҏ法而言Q也是先查向上{型后的基c该ҎQ若无该Ҏ的定义,则编译错Q然后再动态绑定到l承cȝ覆盖ҎQ?/p>

另外Q?strong>staticҎ不能覆盖Q?strong>privateҎ也不能覆盖?/p>

q需特别注意Q方法覆盖时Q若覆盖得不对(例如参数一_仅依靠返回类型不同)Q则~译会出错,而不是被Java认ؓҎ重蝲Q除非参数类型也不一Pq样java会认Z是overrideQ实际上它是overloadQ?/p>

?strong>cd始化

  cȝ初始化大致上有这么几个方面?/p>

aQ静态变量的初始?bQ一般实例变量的初始?cQ构造函数的初始?/p>

  初始化的隄在于l承体系中。当有承体pLQ初始化始终要遵循的原则是Q无论如何必先初始化基类

   0Q当载入当前cLQ若当前cLl承体系Q则依次无条件蝲入基c?/p>

   0’Q先从最的基类开始顺序初始化光态static变量Q接着初始化当前类的静态static变量Q也是_static变量的初始化是伴随着c被装蝲时而初始化的,它的初始化在M构造函数执行前Q?/p>

   1Q先从最端基类Q构造基cd象?/p>

     Q假如构造函C有this或super调用Q则先执行此调用Q?/p>

     1.1Q首先按出现ơ序初始化其实例变量

       1.2Q再执行其构造函C

   2Q依ơ递归上述步骤

  <此外Q实例变量可以不昑ּ初始化(pȝ会赋默认|Q但是局部变量必L式初始化>

?strong>异常

throws是异常的xQ它|于Ҏ的定义处Qthrow是异常的掷出Q它|于Ҏ体内?/strong>

异常可分为可异常和非检异常,调用x为可异常的Ҏ时必L获异常?/strong>

aQ方法申明了可检异常,则调用该Ҏ的块一定要捕获异常Q否则编译出?/p>

   bQthrow后面不能跟Q何语句,否则~译提示语句不可到达

     cQ多个catch语句Q要求更具体的异常在前,类异常在后Q否则编译出?/p>

     dQfinally语句会在return语句之前执行Q即在蟩出方法之前一定会执行finally语句

     eQ假如遇到的是System.exit(0)Q则无论如何Q程序马上退出,finally语句不会执行

     fQ方法申明了异常Q但是在Ҏ体内可以不显C地用throw抛出异常

     gQ没有申明可异常的Ҏ调用Ӟ不可用catch捕获Q否则编译出?/p>

其它注意Q?/p>

a子类中覆盖的Ҏ只能抛出父类Ҏ抛出异常的子集,也可以不抛出M异常Q这本n是子集Q?/p>

   b 但是对于非检异常RuntimeException则不会受C面的U束Q它们可以被随时抛出。也不受范围限制?/p>

    c 当承的子类没有x异常Ӟ假如它的一个实例被xc(向上转型Q,q时再调用子cL有申明异常的ҎQ而用了catchQ程序也会编译通过。(实际q行中调用的q是子类中的ҎQ?/p>

?strong>equals()?=

  对于上述关于equals()ȝ如下:

   aQ类型不兼容的两个对象可以用equals()Q但是只要比较的对象cd不同Q哪怕值相同)Q永q返回false

   bQ对于没有覆盖equals()的类Q即使对象类型相同,g相同Q但q回的仍旧是falseQ因为它用的是object的默认equalsҎQ与==相同Q?/p>

   c然而对于覆盖equals()的类Q只要值相同,便返回true。这些类是String,Wrappers,Date,Calendar,BitSet{?/p>

  对于==ȝ如下Q?/p>

   aQ类型不兼容的两个对象不可以?=Q若用则~译错误

   bQ同U类型的对象Q假如不是指引同一个对象,则返回ؓfalseQ只有指向同一个内存块的对象才q回trueQ?/p>

   cQ对于String情况有些不同Q因为String对象有字面量和构造函C分。字面量对象是放在缓冲池中的Q这意味着Q如果两个字面量值相同,则第二个对象会指向第一个已l存在的对象Q而不会新产生Q所?=q回的是true。用构造器产生的对象同一般对象。对于字面量来说Q多个类׃n的是同一个缓冲池。这意味着在另外一个类中创Z个先前类中已有的字面量字W串Q则仍旧是同一对象?/p>

      另外Q注意,toUpperCase()、toLowerCase()Ҏ而言Q如果大写形式与原始String没什么差别,Ҏq回原始对象Q?=q回true?/p>

   dQ对于基本类型而言Q系l会自动先归一_ֺQ然后再比较|若值相同则q回true?/p>

?strong>String

  StringcL重要的一点在?#8220;不变性(immutableQ?#8221;。它的意思是一旦StringcM生了׃会再改变了,若试图改变它都会产生新的对象?/p>

  String对象有字面量和构造函C分。字面量对象是放在缓冲池中的Q这意味着Q如果两个字面量值相同,则第二个对象会指向第一个已l存在的对象Q而不会新产生Q所?=q回的是true。用构造器产生的对象同一般对象。对于字面量来说Q多个类׃n的是同一个缓冲池。这意味着即在另外一个类中创Z个先前类中已有的字面量字W串Q则仍旧是同一对象?/p>

  考试中需要注意的是:s=new String(“abc”);s+=”def”;System.out.println(s);

                      s=new String(“abc”);s.concat(“def”);System.out.prinln(s);

  前一E序打印?#8220;abcdef”Q后一E序打印?#8220;abc”。区别是W一个程序又重新赋值给了s。而第二个E序中s.concat(“def”)只是产生了一个新的对象但没有赋给谁,但原来的s不变?/p>

  另外Q对于StringBuffer而言是可变的Q对它的M改变都是实时的?/p>

?strong>包装c?/strong>

  包装cLZ方便对基本数据类型操U而出现的。有了包装类可以用很多的方法来操纵基本数据cdQ没有包装类想直接对基本数据cd操作是不方便的,除非自己~写ҎQ?/p>

  要熟悉包装类应该着U理解下面几点:

  aQ包装类的构造器。一般说来,包装cȝ构造器参数有两U:基本数据?/strong>?strong>字符?/strong>

     注意Q?/strong>Boolean包装cL造器当传入字W串Ӟ除了不分大小写的trueq回true外,其它字符串一律返回false

  bQ常见的操作Ҏ。例如:转换为本基本cd或其它基本类型的Ҏ

     eg. byteValue(),intValue()…;parseByte(String s),parseInt(String s)…

       cQvalueOf(各基本类型或字符?的用。ValueOf(str)是包装类的静态方法,作用{同于构造器。它会解析传入的参数Q然后构造一个相同类型的包装c,q返回该包装cR?/p>

       例子Q原始类?#224;字符?nbsp; (new Integer(101)).toString()QString.valueOf(“101”)

             字符?#224;Q包装类Q?#224;原始cd Integer.parseInt(“string”)Q?/p>

(new Integer(“101”)).doubleValue()QInteger.valueOf(“101”).intValue()

?strong>Mathc?/strong>

  MathcM都是静态方法。其中最易错的是三个ҎQceil(),floor(),round()

另外q需注意Q有许多Ҏ随基本数据类型不同有多个重蝲版本。eg.abs(),max()

aQceil()Ҏ。该Ҏq回cd为doubleQ往单一的正坐标方向取最q的整数

bQfloor()Ҏ。该Ҏq回cdcdoubleQ取最靠近其负向的整数?/p>

cQround()Ҏ。它有两个重载版本:double和floatQ返回分别ؓlong和int

   long round(double a)Qint round(float)

   round()ҎQMath.floor(a+0.5),q意味着正数5入,负数6?/strong>

   eg.System.out.println(Math.ceil(Double.MIN_VALUE))     //1.0

System.out.println(Math.floor(-0.1))                //-1.0

System.out.println(Math.round(-9.5))                //-9

System.out.println(Math.round(-9.6))                //-10

System.out.println(Math.round(Double.MIN_VALUE))    //0

?strong>collectionc?/strong>

  collectioncL供了持有对象的便利性,q对持有对象的操作便利性。正如其名,攉意ؓ各U类型的对象收在一P且数目不限(有点像收集袋Q。收集会放入其中的所有对象均视ؓObjectQ向上{型)Q所以在取出元素对象Ӟ必须昑ּQ即强制转型Q指出其cd?/p>

  对象攉从整体上分ؓCollection接口和Map接口。这U分cȝ标准是:某个元素位置上放|元素对象的个数。显ӞMap接口攄的是一寏V?/p>

  Collection接口又可扩展Z个基本接口:List接口和Set接口?/p>

  依上所qͼ对象攉可以划分为四个基本的cdQ?strong>Collection、List、Set、Map

  · Collection 它是一个基cȝ接口Q对元素没有M的限Ӟ可以重复q且无序?/p>

  · List  从其名就知是有序的列表。它描述的是按顺序将对象攑օ其中。显Ӟ后放入的元素有可能与先前攑օ的对象是相同的。所以,List是允许对象重复出现的有序列表?/p>

  · Set   其实是数学上所说的集合Q它不允许有重复的元素。其中可以有I集Q即null对象Q。Set中的元素不要求有序?/p>

  · Map   x,借助于key和value来描q对象的搜烦。key域中要求唯一性(其实是一个SetQ,value域可以允许有重复的元素(其实是一个CollectionQ。另外注意:常见的HashMap是无序的Q而TreeMap是有序的?/p>

?strong>标识W?/strong>

  aQ所有标识符的首字符必须是字母(大小写)、下划线_、美元符?Q或W号K)

bQ接下来的可以是由数字(0-9Q及首字W相同类型的字符Q字母、_?Q,其它MҎ字符均不?/p>

cQ标识符不能使用Java关键字和保留字(50个)。但是注意像java,Integer,sizeof,friendly{都不是Java的关键字

dQ标识符大小写是敏感的,但没有长度的限制?/p>

?strong>Switch(i)

  aQswitch(i)中的参数最高精度是intQ或者short,byte,charQ,但不可是longQfloat,double

  bQdefault语句可以攄于Q何地方(default意ؓ都不匚wcase中的|

  cQ当语句中未加break语句Ӟ则程序会从匹配的地方开始执行(包括匚wdefault语句Q,接下来所有的语句都会被执行(而不匹配否Q,直到遇到break语句或switchN?/p>

?strong>垃圾攉

  aQ只要一个对象失M所?/strong>的referenceQ就可以考虑攉到垃圾收集堆了?/p>

  BQ当失去对一个对象的所有引用时QJVM只是考虑垃圾攉Q但q不意味着qL回这个对象的内存Q甚x本不收回。JVM仅会在需要更多的内存以l执行程序时Q才会进行垃圾收集?/p>

CQ多数情况下Q你永远不会知道垃圾攉什么时候会发生。Java垃圾收集进E作Z个低优先U线E在q行。在Java中垃圾收集是不能被强q?strong>立即执行的。调用System.gc()或Runtime.gc()静态方法不能保证垃圾收集器的立x行,因ؓQ也许存在着更高优先U的U程?/p>

DQ如果你想h工调用垃圾收集,q想在收集对象时执行一些你惌的Q务,你就可以覆盖一个叫finalize()的方法。java会ؓ每一个对象只调用一ơfinalize()Ҏ?strong>finalize()Ҏ必须被申明ؓprotected的,不返回Q何|viodQ,而且要申明抛Z个Throwable对象Qƈ一定要调用类的finalize()ҎQsuper.finalize()Q?/strong>

   eg.protected void finalize() throws Throwable(){

         super.finalize();

         …………;}

?strong>is a & has a

  is a  描述的是一个超cd一个子cȝ关系Q也xl承的关pR?/p>

  has a 描述的是一个对象的部分是另一个对象,也即?strong>l合的关p(或称用)?/p>

?strong>内类与匿名类

  内类是被包含的类中类Q有三个斚w需要注意:一般内cR方法内cR静态内cR?/p>

  · 一般内cd可以被当做外cȝ一?#8220;实例变量”来看待。因此,四个接入控制Wpublic、protected、default、private。只是注意:要在外类的non-static函数外生该内类对象Q必MOuterClassName.InnerClassName的Ş式指定该内类对象?strong>cdx?strong>一般内cdd兌臛_外类的某个对?/strong>?/p>

                一般内cM可拥有static成员?/p>

· Ҏ内类它属?strong>范围型内c,也就是说你无法在Ҏ外来调用内类Q从q一Ҏ讲它可视为方法的局部变量。但是,虽然有它的范畴性,毕竟内类q是c,它不会像局部变量那样随着Ҏ的返回就消失了,它仍旧被java视ؓcR?/p>

              AQ方法内cd以直接访问外cȝM成员

              BQ方法内cd能访问该Ҏ中final型局部变量和final型的Ҏ参数

              C. Ҏ内类不可有Q?strong>接入控制W?/strong>修饰Q这一点与局部变量相同)

  · 静态内cd在生其对象时不需要存在一个外cd象。它可被视ؓstatic函数?/p>

             static内类可以|于接口中?/p>

  · ??cd实际上是l承自newcȝ一个无名类。New传回的reference会被自动向上转型。匿名类不能拥有构造器Q但是可以通过其基c默认或带参数的构造器来申明?/p>

匿名cL加Q何修饰符Q遵循超cȝ修饰W)Q也不可实现接口、抛出异常?/p>

?strong>断言

  断言是Java 1.4中新d的功能,是Java中的一U新的错误检查机Ӟ它提供了一U在代码中进行正性检查的机制Q但是这功能可以根据需要关闭。断a包括Q?strong>assert关键字,AssertionErrorc,以及在java.lang.ClassLoader中增加了几个新的有关assertҎ?/p>

assert最重要的特Ҏassert语句可以?strong>q行?/strong>L的开启或关闭Q默认情况下是关闭的?/p>

断言语句有两U合法的形式QaQ?strong>assert expression_r1; bQ?strong>assert expression_r1 : expression_r2;

expression_r1是一条被判断的布表辑ּQ必M证在E序执行q程中它的g定是真;expression_r2是可选的Q用于在expression_r1为假Ӟ传递给抛出的异常AssertionError的构造器Q因此expression_r2的类型必L合法的AssertionError构造器的参数类型。AssertionError除了一个不带参数的~省构造器外,q有7个带单个参数的构造器Q分别ؓQobjectQeg.StringQ? boolean  char  int  long  float  double。第一UŞ式如果抛出异常,则调用AssertionError的缺省构造器Q对于第二种形式Q则Ҏexpression_r2值的cdQ分别调?U单参数构造器中的一U?/p>

AQ?strong>assertE序的编?/strong>Qjavac -source 1.4 TestAssert.javaQ提Cjava?.4版本~译Q?/p>

BQ?strong>assertE序的运?/strong>Qjava –ea TestAssert 或?java –ea:TestAssert TestAssert

   其它的运行参敎ͼjava -ea:pkg0... TestAssertQjava –esaQjava –dsaQ系l类断言Q,另外Q还可以同时l合用。当一个命令行使用多项 -ea -da 参数Ӟ遵@两个基本的原则:后面的参数设定会覆盖前面参数的设定,特定具体的参数设定会覆盖一般的参数讑֮?/p>

CQ?strong>AssertinErrorc?/strong>是Error的直接子c,因此代表E序出现了严重的错误Q这U异帔R常是不需要程序员使用catch语句捕捉的?/p>

DQ?strong>使用assert的准?/strong>Qassert语句的作用是保证E序内部的一致性,而不是用户与E序之间的一致性,所以不应用在保证命令行参数的正性。可以用来保证传递给privateҎ参数的正性。因为私有方法只是在cȝ内部被调用,因而是E序员可以控制的Q我们可以预期它的状态是正确和一致的。公有方法则不适用。此外,assert语句可用于检查Q何方法结束时状态的正确性,及在Ҏ的开始检查相关的初始状态等{?/p>

assert语句q不构成E序正常q行逻辑的一部分Q时刻记住在q行时它们可能不会被执行?/p>

?strong>U程

  U程是将E序中容易消耗大量cpu且易陷入d@环的片断代码独立出来作ؓ一个线E来q行Q也即线E是一个代码块Q?/p>

  U程一l启动startQ就会进入ready状态(实际上就是runnable状态,只是{待分配cpuQ。这也说明线Eƈ不会马上runningQ其q行h不确定性。线E启动后Q只要不跛_run()ҎQ则一直都有机会runningQ它ql自动在各线E间分配cpu旉来running。要牢记的是Q?strong>U程q行与中断具有不定性,你永q也不知道线E何时会q行Q何时会中断?/strong>

  U程从对象的角度来看Q它自n也可以是一个对象。它可视为其它对象中的一个代码块?/p>

  在线E的概念中要特别注意几个概念Q?strong>单线E?/strong>?strong>多线E?/strong>?strong>多线E的q行?strong>多线E间的同步(资源讉KQ?/strong>?strong>多线E间的通信?/p>

  · 单线E?/strong> 对于单线E而言Q编写其E序是比较简单的Q也比较Ҏ理解Q因为它q不涉及到synchronized和communication问题。创建单U程的方法有两种Q其一、扩展Threadc,即class A extends Thread{public void run(){};……}Q其二、实现Runnable接口Qclass B implements Runnable{Thread t=new Thread(this);public void run(){};……}Q?/p>

  · 多线E?/strong> 相对于单U程而言Q多U程会复杂很多,原因是它们会涉及到多线E间的资源访?/strong>?strong>多线E间通信问题。这涉及到下面所说的三个斚wQ?strong>多线E的q行?strong>多线E间的同步(资源讉KQ?/strong>?strong>多线E间的通信

  · 多线E的q行 对于多个可运行runnable的线E来_q行与中断具有不定性,永远也无法知道线E何时会q行Q何时会中断。但是多U程q行也遵循几个原则:如果多个U程h同样的优先Q则pȝ会在它们之间切换cpu旉q行Q?strong>JVMZ优先U来军_U程的运行,但是qƈ不意味着一个低优先U的U程一直不q行?/p>

  · 多线E的同步 被线E可讉K的每个对象都有一个仅被一个线E访问控制的锁,锁控制着对对象的同步码的存取。这个可被多个线E访问的对象是所说的资源׃n问题?/p>

AQ在E序中,可以通过定义一个synchronized代码块或多个synchronizedҎ来得调用的U程获得该对象的控制锁。通过获得对象的锁Q该U程׃L其它U程对该对象所定义?strong>同步?/strong>?strong>同步Ҏq行操作Q特别注意的是,此时q不能保证其它线E对该对象的非同步变量和非同步方法进行操作)?/p>

BQ线E只有在同步块或同步Ҏq回后才释放锁?/p>

CQsynchronizedq不能保证程序的q行q箋性,而只是保证同一性。也是说即使在synchronized块或Ҏ中,U程的运行仍旧会有中断的可能性。尽如此,但它却能保别的U程不会再访问该对象的其它同步块和方法,因ؓ对象锁ƈ未释放。这一事实说明?#8220;多线E的q行”?#8220;多线E间的同?#8221;是两个独立的概念?/p>

DQ多U程的同步块或方法可以放在Q何可被线E访问的对象中(包括U程本nQ它实际上也可被其它的线E访问)?/p>

  · 多线E间的通信 多线E间的同步消除了一个线E在改变另一个线E的E_对象状态时发生的ƈ发错误,但是qE间通信而言Q同步不起Q何作用,也就是说“多线E间的通信”又是一个独立的概念。多U程间的通信通常是靠wait(),notify()两个Ҏ来实现的Q有兌两个Ҏ的ȝ如下Q?/p>

    1.wait(),notify()属于objectҎQƈ不是U程的方?/p>

2.object.wait()  意ؓQ调用我Q指该objectQ的当前U程你得{等Q也是?..Q调用我的当前线E)...{待

object.notify()意ؓQ该唤醒其它先前调用q我的且在等待的U程

      从上q意义可知,wait(),notify()的对象是指线E所要用到的׃n对象Q当然共享对象也可以是线E对象)Q但是它的方法动作却是针对调用它的线E。(通常情况下,对象的方法是作用于自q属性,而很作用于其它对象。若要作用于其它对象Q则用调用object.method()Q?/p>

3.wait(),notify()必须成对出现Q出现的方式可有3UŞ式?/p>

      A.{wait();.... notify();?/p>

      B.{wait();?.. {object.notify();?/p>

      C.{notify();?.. {object.wait();?/p>

4.wait(),notify()必须出现在synchronizedҎ或块中,否则会出现异?/strong>。原因是因ؓwait()会释攑֯象锁Q而锁必然是出现在同步Ҏ或块中。另外,wait()同sleep()一P也必L捉异常InterruptedException?/p>

5.wait(),notify()的执行一般与外在的条件有养I只有条g改变了才触发唤醒{待的线E。这U条件的改变通常是以旗标(Tag)的方式出玎ͼ也即当某一Ҏ执行完后Q应当立x变旗标倹{假若需要让U程交替执行Q则q需要加入互斥条件的判断。eg.同步Ҏ1中{if(a)}Q则同步Ҏ2中{if(!a)}

6.当执行完notify()ӞE序q不会立卛_q行调用wait()的线E,而直到释放notify()的对象锁。当释放完锁后,E序重新分配cpuQ要注意的是Q此时系lƈ不一定就让wait的线E去q行Q而有可能是刚才调用notify()的线E接着l箋q行。这一Ҏ说明了线E运行与中断的不定性?/p>

7.一般说来,notify()是唤醒等待池中等待时间最长的U程Q而notifyAll()是唤醒等待池中所有等待线E,然后U程ȝ争对象锁。这里说的是一般情况,有时情况q如此Q这是由pȝ中线E运行与中断的不定性决定的?/p>

8.wait()Qnotify()通常情况下需要sleep()的配合,否则屏幕中的q行昑ּ?#8220;飞?#8221;?/p>

9.多线E间的通信会出现死锁现象,即wait的线E有可能永远也得不到对象锁?/p>

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

?strong>其他注意问题

?nbsp;    数组

aQ数l在使用之前Q必要保证l其分配内存Q系l会用默认值初始化Q,不可只定义。否则编译通过q行也会出现I指针错误。分配内存只需通过new可以了?/p>

bQ二l数l的W二l可以是变长的,而且可以在定义时不指定具体倹{这意味着java中的二维数组不必像矩阵那栯求每一l长度都相同?/p>

?nbsp;    变量赋?/p>

aQ实例变量只可在定义时显式赋|不可先定义,再赋|q样的话~译出错Q?/p>

bQ方法变量既可以在定义时昑ּ赋|又可以先定义以后再赋倹{?/p>

cQstatic变量可以在类的Q何地方赋|若在Ҏ中赋|实际上是重赋gQ?/p>

dQfinal变量可以在Q何地方赋|但是一旦赋|׃允许再次重赋倹{?/p>

eQstatic final变量只能在定义处赋|卻I帔RQ?/p>

?nbsp;    UM

aQ?gt;>  首位的拷贝右UM。等同于有符L除法?/p>

bQ?gt;>> 零填充右UM?/p>

cQ?lt;<  左移位。等同于有符号乘法,但是必须遵@整数的溢出原则?/p>

dQ?gt;>32 >>>32MUd位都是UL32的余?/strong>。eg.U?2位即不移?/p>

?nbsp;    byte、char和int

׃char的取D围和int的正取D围相同,所以,整型字面量可以直接赋lchar。但是要是明将一个整型(intQ变量直接赋lchar变量则编译错误?/p>

另外Qint i=5;char c=’a’;c+=i;~译是通过的?/p>

bytecd在强制{型的情况下,当范围超出时会@环溢出?/p>

?nbsp;    求模%

求余只管左边的符P双不管?/p>

eg. int a=-5;int b=-2;System.out.println(a%b) //-1

   int a=-5;int b=2;System.out.println(a%b) //-1

   int a=5;int b=-2;System.out.println(a%b) //1

 



]]>
W九部分Q集合类框架http://www.tkk7.com/balajinima/articles/298540.html李云?/dc:creator>李云?/author>Fri, 16 Oct 2009 03:44:00 GMThttp://www.tkk7.com/balajinima/articles/298540.htmlhttp://www.tkk7.com/balajinima/comments/298540.htmlhttp://www.tkk7.com/balajinima/articles/298540.html#Feedback0http://www.tkk7.com/balajinima/comments/commentRss/298540.htmlhttp://www.tkk7.com/balajinima/services/trackbacks/298540.htmlW九部分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.tkk7.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.tkk7.com/balajinima/articles/298538.html</guid><wfw:comment>http://www.tkk7.com/balajinima/comments/298538.html</wfw:comment><comments>http://www.tkk7.com/balajinima/articles/298538.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/balajinima/comments/commentRss/298538.html</wfw:commentRss><trackback:ping>http://www.tkk7.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同的原因停止QceaseQƈq入qA状态?/p> <p> <wbr></p> <p>  一个新创徏的线Eƈ不是自动的开始运行的Q必调用它的start()Ҏ使之线E放?/p> <p>可运行态(runnable</p> <p>stateQ,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>AnswerQAC。同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>AnswerQDE</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 objectQ(MutipleQ?/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.tkk7.com/balajinima/aggbug/298538.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/balajinima/" target="_blank">李云?/a> 2009-10-16 11:43 <a href="http://www.tkk7.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.tkk7.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.tkk7.com/balajinima/articles/298539.html</guid><wfw:comment>http://www.tkk7.com/balajinima/comments/298539.html</wfw:comment><comments>http://www.tkk7.com/balajinima/articles/298539.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/balajinima/comments/commentRss/298539.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/balajinima/services/trackbacks/298539.html</trackback:ping><description><![CDATA[<p align="left">W八部分Q在java.lang包中的基c?/p> <ul type="disc"> <li>能够应用MathcM的abs,ceil,floor,max,min,random,round,sin,cos,tan,sqrtҎ? <li>正确理解StringcM可改变的意义? <li>当执行一D늨序,中间包含有wrappercȝ一个实例,知道它运行的前提条gq行l果。能用下面wrapperc(例如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.tkk7.com/balajinima/aggbug/298539.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/balajinima/" target="_blank">李云?/a> 2009-10-16 11:43 <a href="http://www.tkk7.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.tkk7.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.tkk7.com/balajinima/articles/298537.html</guid><wfw:comment>http://www.tkk7.com/balajinima/comments/298537.html</wfw:comment><comments>http://www.tkk7.com/balajinima/articles/298537.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/balajinima/comments/commentRss/298537.html</wfw:commentRss><trackback:ping>http://www.tkk7.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>能正用覆盖和重蝲的方法,能正调用父cL重蝲的构造方?constructor)Q知道调用这些方法后的结果? <li>能正实例化cL内部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实际上是一个Personc,q涉及到c?/p> <p>型的自动转换Q将一个子cȝ实例赋值给一个父cȝ变量是不用进行强制类型{换,反之则需</p> <p>要进行强制类型{换,而且被赋值的变量实际上应该是一个子cȝ对象Q,如果对t调用了子</p> <p>cM新增的方法则造成~译旉误编译将不能通过Q而在q行Ӟq行时系l将Ҏt实际?/p> <p>向的cd调用对应的方法,对于本例来说Qt.print(10)调用t实际指向的Teachercȝ对应</p> <p>Ҏ。在java中,可以用一个子cȝ实例实例化父cȝ一个变量,而变量在~译时是一个父c?/p> <p>实例Q在q行时可能是一个子cd例?/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>  此题涉及Ҏ重蝲QoverloadQ,Ҏ重写QoverrideQ以及类z时方法重写的规则</p> <p>。方法重载的规则是:一、参数列表必M同,个数的不同完全可以,如果个数相同则参数类</p> <p>型的不同不能引v歧意Q例如int</p> <p>和long,float和double׃能作为唯一的类型不同;二、返回值可以不同,但是不能是重?/p> <p>时唯一的不同点Q这点和c++中不同,c++中返回类型必M_。方法重写发生在cȝ承时Q?/p> <p>子类可以重写一个父cM已有的方法,必须在返回类型和参数列表一h才能说是重写Q否?/p> <p>是重蝲Qjava中方法重写的一个重要而且Ҏ被忽略的规则是重写的Ҏ的访问权限不能比</p> <p>被重写的Ҏ的访问权限低Q重写的另一个规则是重写的方法不能比被重写的Ҏ抛弃(thro</p> <p>ws)更多U类的异常,其抛弃的异常只能,或者是其子c,不能以抛弃异常的个数来判断种</p> <p>c,而应该是异常cdơ结果上的种cR此题中{案a的错误就是重写的讉K权限比被重写?/p> <p>Ҏ的低Q而b,c都属于重载,d的错误在于比被重写的Ҏ抛弃了更多种cȝ异常?/p> </div> <img src ="http://www.tkk7.com/balajinima/aggbug/298537.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/balajinima/" target="_blank">李云?/a> 2009-10-16 11:42 <a href="http://www.tkk7.com/balajinima/articles/298537.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>W五部分Q操作与赋?/title><link>http://www.tkk7.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.tkk7.com/balajinima/articles/298536.html</guid><wfw:comment>http://www.tkk7.com/balajinima/comments/298536.html</wfw:comment><comments>http://www.tkk7.com/balajinima/articles/298536.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/balajinima/comments/commentRss/298536.html</wfw:commentRss><trackback:ping>http://www.tkk7.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>知道StringQBoolean和类使用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>  下面的哪些叙qCؓ真?/p> <p>  A. equals()Ҏ判定引用值是否指向同一对象?/p> <p> <wbr></p> <p>  B. == 操作W判定两个分立的对象的内容和cd是否一致?/p> <p> <wbr></p> <p>  C. equals()Ҏ只有在两个对象的内容一致时q回true?/p> <p> <wbr></p> <p>  D. cFile重写Ҏequals()在两个分立的对象的内容和cd一致时q回true?/p> <p> <wbr></p> <p>  {案 A,D</p> <p> </p> <p>  解析 严格来说q个问题的答案是不确定的Q因为equals()Ҏ是可以被重蝲的,但是</p> <p>按照java语言的本意来_如果没有重写QoverrideQ新cȝequals()Q则该方法和</p> <p>==</p> <p>操作W一样在两个变量指向同一对象时返回真Q但是java推荐的是使用equals()Ҏ来判?/p> <p>两个对象的内Ҏ否一P像Stringcȝ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左UL最低位始终补零Q最高位被舍弃。移位操作符另一?/p> <p>非常值得注意的特Ҏ其右操作数是取模q算的,意思是说对于一个int型数据而言Q对它移</p> <p>?2位的l果是保持不变而非变成Ӟ卻Ia>>32的结果是a而不?Q同理,对long型数是对</p> <p>x作数?4的模Qa>>64==aQ还有一炚w要注意的是移位操作符”>>>”只对int型和long?/p> <p>有效Q对byte或者short的操作将D自动cd转换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对象时才有可能返回trueQ其它对象都q回假。需要指出的?/p> <p>׃s和tq使用new创徏的,他们指向内存池中的同一个字W串帔RQ因此其地址实际上是</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都是cPerson的子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但是后面的cd转换是非法的Q因为t是一个Teacher对象Q它不能被强?/p> <p>转换Z个Student对象Q即使这两个cL共同的父cR如果是t转换Z个Person对象则可</p> <p>以,而且不需要强制{换。这个错误在~译时就可以发现Q因此编译不能通过?/p> <p> <wbr></p> </div> <img src ="http://www.tkk7.com/balajinima/aggbug/298536.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/balajinima/" target="_blank">李云?/a> 2009-10-16 11:41 <a href="http://www.tkk7.com/balajinima/articles/298536.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>W四部分Q语a基础http://www.tkk7.com/balajinima/articles/298535.html李云?/dc:creator>李云?/author>Fri, 16 Oct 2009 03:40:00 GMThttp://www.tkk7.com/balajinima/articles/298535.htmlhttp://www.tkk7.com/balajinima/comments/298535.htmlhttp://www.tkk7.com/balajinima/articles/298535.html#Feedback0http://www.tkk7.com/balajinima/comments/commentRss/298535.htmlhttp://www.tkk7.com/balajinima/services/trackbacks/298535.htmlW四部分Q语a基础

  • 能正声明包、import、类Q包括内部类Q、接口、方法(包括用于开始一个类的执行的mainҎQ、变量,q正用修饰符?
  • 能够正确使用实现了接口的c,包括java.lang.Runnable接口或题目中指定的接口?
  • 知道命o行参数是如何传递给cȝmainҎ的?
  • 知道哪些是JAVA的合法keyword。注意:考试中不会出现要你解释keyword和各U常数这U深奥的问题?
  • 明白如果没有昑ּ地赋|各种变量或者数l的默认值是什么?
  • 掌握所有原始数据类型的取D_如何声明各种数据cd?

§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个Vectorcd象?/p>

  D. W三行语句创Z个Vectorcd象?/p>

  E. W三行语句ؓ一个Vectorcd象分配内存空间?/p>

 

  {案 A,D,E

 

  解析

  SL-275中指出:要ؓ一个新对象分配I间必须执行new

Xxx()调用Qnew调用执行以下的操作:

 

  1Qؓ新对象分配空间ƈ其成员初始化ؓ0或者null?/p>

 

   2Q执行类体中的初始化。(例如在类中有一个成员声明int a=10;在第一步后a=0

Q执行到W二步后a=10Q?/p>

 

  3Q执行构造函数?/p>

 

  4Q变量被分配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>

 

  cM有几U变量,分别是:局部变量(英文可以为:local\automatic\temporary\stac

k

variableQ是定义在方法里的变量;实例变量Q英文ؓQinstance

variableQ是在方法外而在cd明内定义的变量,有时也叫成员变量Q类变量Q英文ؓQcl

ass

variableQ是用关键字static声明的实例变量,他们的生存期分别是:局部变量在定义该变

量的Ҏ被调用时被创建,而在该方法退出后被撤销Q实例变量在使用new

Xxxx()创徏该类的实例时被创建,而其生存期和该类的实例对象的生存期相同;cd量在?/p>

c被加蝲时被创徏Q不一定要用new

Xxxx()创徏Q所有该cȝ实例对象׃n该类变量Q其生存期是cȝ生存期。Q何变量在使用

前都必须初始?但是需要指出的是局部变量必L式初始化Q而实例变量不必,原始cd?/p>

实例变量在该cȝ构造方法被调用时ؓ它分配的~省的|整型?Q布型是falseQ而Q?/p>

型是0.0fQ引用类型(cȝ型)的实例变量的~省值是nullQ没有进行实际的初始化,对它?/p>

使用引起NullPointExceptionQ,cd量的规则和实例变量一P不同的是cd量的初始?/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.tkk7.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.tkk7.com/balajinima/articles/298534.html</guid><wfw:comment>http://www.tkk7.com/balajinima/comments/298534.html</wfw:comment><comments>http://www.tkk7.com/balajinima/articles/298534.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/balajinima/comments/commentRss/298534.html</wfw:commentRss><trackback:ping>http://www.tkk7.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公司的JavaE序员(Java ProgrammerQ认证考试中,垃圾攉器是必考的内容Q一般最多可以占d值的6%左右。但是由于SUN公司的Java Programming Language SL-275 评的标准教材中Q对有关垃圾攉器的内容只做了非常简单的介绍Q而另外的一些关于Java技术的书籍Q比如《Java 2 核心技术》(Core Java 2Q、《Java~程思想》(Thinking in JavaQ、《精通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> 在CQC++或其他程序设计语a中,无论是对象还是动态配|的资源或内存,都必ȝE序员自行声明生和回收Q否则其中的资源消耗,造成资源的浪费甚x机。但手工回收内存往往是一复杂而艰巨的工作。因预先定占用的内存空间是否应该被回收是非常困隄Q如果一D늨序不能回收内存空_而且在程序运行时pȝ中又没有了可以分配的内存I间Ӟq段E序只能崩溃。通常Q我们把分配出去后,却无法回收的内存I间UCؓ"内存渗漏体(Memory LeaksQ??/p> <p>以上q种E序设计的潜在危险性在Javaq样以严谨、安全著U的语言中是不允许的。但是Java语言既不能限制程序员~写E序的自由性,又不能把声明对象的部分去除(否则׃是面向对象的E序语言了)Q那么最好的解决办法是从JavaE序语言本n的特性入手。于是,Java技术提供了一个系l的线E(ThreadQ,卛_圾收集器U程QGarbage Collection ThreadQ,来跟t每一块分配出ȝ内存I间Q当Java 虚拟机(Java Virtual MachineQ处于空闲@环时Q垃圾收集器U程会自动检查每一快分配出ȝ内存I间Q然后自动回收每一快可以回收的无用的内存块?/p> <p> <wbr></p> <p> <wbr> <wbr> <wbr> 垃圾攉器线E是一U低优先U的U程Q在一个JavaE序的生命周期中Q它只有在内存空闲的时候才有机会运行。它有效地防止了内存渗漏体的出现Qƈ极大可能地节省了宝贵的内存资源。但是,通过Java虚拟机来执行垃圾攉器的Ҏ可以是多U多L?/p> <p>下面介绍垃圾攉器的特点和它的执行机Ӟ</p> <p> <wbr> <wbr> <wbr> 垃圾攉器系l有自己的一套方案来判断哪个内存块是应该被回收的Q哪个是不符合要求暂不回收的。垃圾收集器在一个JavaE序中的执行是自动的Q不能强制执行,即ɽE序员能明确地判断出有一块内存已l无用了Q是应该回收的,E序员也不能强制垃圾攉器回收该内存块。程序员唯一能做的就是通过调用System. gc Ҏ?"执行垃圾攉器,但其是否可以执行Q什么时候执行却都是不可知的。这也是垃圾攉器的最主要的缺炏V当然相对于它给E序员带来的巨大方便性而言Q这个缺Ҏ瑕不掩瑜的?/p> <p>垃圾攉器的主要特点有:</p> <p>1Q垃圾收集器的工作目标是回收已经无用的对象的内存I间Q从而避免内存渗漏体的生,节省内存资源Q避免程序代码的崩溃?/p> <p>2Q垃圾收集器判断一个对象的内存I间是否无用的标准是Q如果该对象不能再被E序中Q何一?zd的部?所引用Q此时我们就_该对象的内存I间已经无用。所?zd的部?Q是指程序中某部分参与程序的调用Q正在执行过E中Q尚未执行完毕?/p> <p>3Q垃圾收集器U程虽然是作Z优先U的U程q行Q但在系l可用内存量q低的时候,它可能会H发地执行来挽救内存资源。当然其执行与否也是不可预知的?/p> <p>4Q垃圾收集器不可以被强制执行Q但E序员可以通过调用System. gcҎ来徏议执行垃圾收集器?/p> <p>5Q不能保证一个无用的对象一定会被垃圾收集器攉Q也不能保证垃圾攉器在一DJava语言代码中一定会执行。因此在E序执行q程中被分配出去的内存空间可能会一直保留到该程序执行完毕,除非该空间被重新分配或被其他Ҏ回收。由此可见,完全d地根l内存渗漏体的生也是不可能的。但是请不要忘记QJava的垃圾收集器毕竟使程序员从手工回收内存空间的J重工作中解׃出来。设想一个程序员要用C或C++来编写一D?0万行语句的代码,那么他一定会充分体会到Java的垃圾收集器的优点!</p> <p>6Q同h有办法预知在一l均W合垃圾攉器收集标准的对象中,哪一个会被首先收集?/p> <p>7Q@环引用对象不会媄响其被垃圾收集器攉?/p> <p>8Q可以通过对象的引用变量Qreference variablesQ即句柄handlesQ初始化为null|来暗C垃圾收集器来收集该对象。但此时Q如果该对象q接有事件监听器Q典型的 AWTlgQ,那它q是不可以被攉。所以在设一个引用变量ؓnullg前,应注意该引用变量指向的对象是否被监听Q若有,要首先除ȝ听器Q然后才可以赋空倹{?/p> <p>9Q每一个对象都有一个finalize( )ҎQ这个方法是从Objectcȝ承来的?/p> <p>10Qfinalize( )Ҏ用来回收内存以外的系l资源,像是文件处理器和网l连接器。该Ҏ的调用顺序和用来调用该方法的对象的创建顺序是无关的。换句话_书写E序时该Ҏ的顺序和Ҏ的实际调用顺序是不相q的。请注意q只是finalize( )Ҏ的特炏V?/p> <p>11Q每个对象只能调用finalize( )Ҏ一ơ。如果在finalize( )Ҏ执行时生异常(exceptionQ,则该对象仍可以被垃圾攉器收集?/p> <p>12Q垃圾收集器跟踪每一个对象,攉那些不可到达的对象(卌对象没有被程序的M"zȝ部分"所调用Q,回收其占有的内存I间。但在进行垃圾收集的时候,垃圾攉器会调用finalize( )ҎQ通过让其他对象知道它的存在,而不可到达的对象再?复苏"为可到达的对象。既然每个对象只能调用一ơfinalize( )ҎQ所以每个对象也只可?复苏"一ơ?/p> <p>13Qfinalize( )Ҏ可以明确地被调用Q但它却不能q行垃圾攉?/p> <p>14Qfinalize( )Ҏ可以被重载(overloadQ,但只有具备初始的finalize( )Ҏ特点的方法才可以被垃圾收集器调用?/p> <p>15Q子cȝfinalize( )Ҏ可以明确地调用父cȝfinalize( )ҎQ作子类对象的最后一ơ适当的操作。但Java~译器却不认是一ơ覆盖操作(overridingQ,所以也不会对其调用q行查?/p> <p>16Q当finalize( )Ҏ未被调用时QSystem. runFinalization( )Ҏ可以用来调用finalize( )ҎQƈ实现相同的效果,Ҏ用对象进行垃圾收集?/p> <p>17Q当一个方法执行完毕,其中的局部变量就会超Z用范_此时可以被当作垃圾收集,但以后每当该Ҏ再次被调用时Q其中的局部变量便会被重新创徏?/p> <p>18QJava语言使用了一U?标记交换区的垃圾攉法"。该法会遍历程序中每一个对象的句柄Qؓ被引用的对象做标讎ͼ然后回收未做标记的对象。所谓遍历可以简单地理解?查每一??/p> <p>19QJava语言允许E序员ؓMҎdfinalize( )ҎQ该Ҏ会在垃圾攉器交换回收对象之前被调用。但不要q分依赖该方法对pȝ资源q行回收和再利用Q因Ҏ调用后的执行l果是不可预知的?/p> <p>通过以上对垃圾收集器特点的了解,你应该可以明垃圾收集器的作用,和垃圾收集器判断一块内存空间是否无用的标准。简单地_当你Z个对象赋gؓnullq且重新定向了该对象的引用者,此时该对象就W合垃圾攉器的攉标准?/p> <p>判断一个对象是否符合垃圾收集器的收集标准,q是SUN公司E序员认证考试中垃圾收集器部分的重要考点Q可以说Q这是唯一的考点Q。所以,考生在一D늻定的代码中,应该能够判断出哪个对象符合垃圾收集器攉的标准,哪个不符合。下面结合几U认证考试中可能出现的题型来具体讲解:</p> <p>Object obj = new Object ( ) ;</p> <p>我们知道Qobj为Object的一个句柄。当出现new关键字时Q就l新建的对象分配内存I间Q而obj的值就是新分配的内存空间的首地址Q即该对象的?L别注意,对象的值和对象的内Ҏ不同含义的两个概念:对象的值就是指其内存块的首地址Q即对象的句柄;而对象的内容则是其具体的内存?。此时如果有 obj = nullQ则obj指向的内存块此时无用了Q因Z面再没有调用该变量了?/p> <p>请再看以下三U认证考试时可能出现的题型Q?/p> <p>E序D?Q?/p> <p>1Qfobj = new Object ( ) ;</p> <p>2Qfobj. Method ( ) ;</p> <p>3Qfobj = new Object ( ) ;</p> <p>4Qfobj. 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>1QObject sobj = new Object ( ) ;</p> <p>2QObject sobj = null ;</p> <p>3QObject sobj = new Object ( ) ;</p> <p>4Qsobj = new Object ( ) ;</p> <p>问:q段代码中,W几行的内存I间W合垃圾攉器的攉标准Q?/p> <p>{:W?行和W?行。因为第2行ؓsobj赋gؓnullQ所以在此第1行的sobjW合垃圾攉器的攉标准。而第4行相当于为sobj赋gؓnullQ所以在此第3行的sobj也符合垃圾收集器的收集标准?/p> <p>如果有一个对象的句柄aQ且你把a作ؓ某个构造器的参敎ͼ?new Constructor ( a )的时候,即你给a赋gؓnullQa也不W合垃圾攉器的攉标准。直到由上面构造器构造的新对象被赋空值时Qa才可以被垃圾攉器收集?/p> <p>E序D?Q?/p> <p>1QObject aobj = new Object ( ) ;</p> <p>2QObject bobj = new Object ( ) ;</p> <p>3QObject cobj = new Object ( ) ;</p> <p>4Qaobj = bobj;</p> <p>5Qaobj = cobj;</p> <p>6Qcobj = null;</p> <p>7Qaobj = null;</p> <p>问:q段代码中,W几行的内存I间W合垃圾攉器的攉标准Q?/p> <p>{:W?行。注意这c题型是认证考试中可能遇到的最N型了?/p> <p>?-3分别创徏了Objectcȝ三个对象QaobjQbobjQcobj</p> <p>?Q此时对象aobj的句柄指向bobjQ所以该行的执行不能使aobjW合垃圾攉器的攉标准?/p> <p>?Q此时对象aobj的句柄指向cobjQ所以该行的执行不能使aobjW合垃圾攉器的攉标准?/p> <p>?Q此时仍没有M一个对象符合垃圾收集器的收集标准?/p> <p>?Q对象cobjW合了垃圾收集器的收集标准,因ؓcobj的句柄指向单一的地址I间。在W?行的时候,cobj已经被赋gؓnullQ但由cobj同时q指向了aobjQ第5行)Q所以此时cobjq不W合垃圾攉器的攉标准。而在W?行,aobj所指向的地址I间也被赋予了空值nullQ这p明了Q由cobj所指向的地址I间已经被完全地赋予了空倹{所以此时cobj最l符合了垃圾攉器的攉标准。但对于aobj和bobjQ仍然无法判断其是否W合攉标准?/p> <p>MQ在Java语言中,判断一块内存空间是否符合垃圾收集器攉标准的标准只有两个:</p> <p>1Q给对象赋予了空值nullQ以下再没有调用q?/p> <p>2Q给对象赋予了新|既重新分配了内存I间?/p> <p>最后再ơ提醒一下,一块内存空间符合了垃圾攉器的攉标准Qƈ不意味着q块内存I间׃定会被垃圾收集器攉?/p> <p> <wbr></p> <img src ="http://www.tkk7.com/balajinima/aggbug/298534.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/balajinima/" target="_blank">李云?/a> 2009-10-16 11:39 <a href="http://www.tkk7.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.tkk7.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.tkk7.com/balajinima/articles/298533.html</guid><wfw:comment>http://www.tkk7.com/balajinima/comments/298533.html</wfw:comment><comments>http://www.tkk7.com/balajinima/articles/298533.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/balajinima/comments/commentRss/298533.html</wfw:commentRss><trackback:ping>http://www.tkk7.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,continueQ能计算在@环中或@环后循环计数器的倹{? <li>能够正确使用异常和异常处理语句(try,catch,finallyQ。能正确声明抛出异常的方法,q知道怎样覆盖它? <li>了解代码D中的异常对E序跌{的媄响。注意:异常可能是一个运行时异常Qruntime <wbr>exceptionQ?一个已l定义的异常Qchecked <wbr>exceptionQ,也可能是一个error? <li>能正用断aQƈ了解关于断言机制的正说法?</li> </ul> <div> <ul type="disc"> <li>能够正确使用if,switch语句Qƈ且能正确使用合法的参数类型? <li>能够正确使用所有带标签或不带标{循环语句Q能正确使用break,continueQ能计算在@环中或@环后循环计数器的倹{? <li>能够正确使用异常和异常处理语句(try,catch,finallyQ。能正确声明抛出异常的方法,q知道怎样覆盖它? <li>了解代码D中的异常对E序跌{的媄响。注意:异常可能是一个运行时异常Qruntime <wbr>exceptionQ?一个已l定义的异常Qchecked <wbr>exceptionQ,也可能是一个error? <li>能正用断aQƈ了解关于断言机制的正说法?</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开发中Qassertion是一U经典的调试、测试方式,本文深入解析assertion功能的用以及其设计理念Qƈl出相关的例子?/p> <p>assertion(断言)在Y件开发中是一U常用的调试方式Q很多开发语a中都支持q种机制Q如CQC++和Eiffel{,但是支持的Ş式不相同,有的是通过语言本n、有的是通过库函数等。另外,从理Z来说Q通过assertion方式可以证明E序的正性,但是q是一相当复杂的工作Q目前还没有太多的实跉|义?/p> <p>在实CQassertion是在程序中的一条语句,它对一个boolean表达式进行检查,一个正程序必M证这个boolean表达式的gؓtrueQ如果该gؓfalseQ说明程序已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ؓ了支持assertionQJava增加了一个关键字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这些语句将不vM作用。如果打开了assertion功能Q那么expression_r1的值将被计,如果它的gؓfalseQ该语句强抛Z个AssertionError对象。如果assertion语句包括expression_r2参数Q程序将计算出expression_r2的结果,然后这个结果作为AssertionError的构造函数的参数Q来创徏AssertionError对象Qƈ抛出该对象;如果expression_r1gؓtrueQexpression_r2不被计?/p> <p>一U特D情冉|Q如果在计算表达式时Q表辑ּ本n抛出ExceptionQ那么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和Classc,因此Q这U程序必dJDK1.4(或者更高版?的JRE下运行,而不能在老版本的JRE下运行?/p> <p>1. <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> ׃我们可以选择开启assertion功能Q或者不开启,另外我们q可以开启一部分cL包的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机制QJavaE序通常使用if-then-else或者switch-case语句q行assertion查,而且查的数据cd也不完全相同。assertion机制让JavaE序员用l一的方式处理assertion问题Q而不是按自己的方式处理。另外,如果用户使用自己的方式进行检查,那么q些代码在发布以后仍然将起作用,q可能会影响E序的性能。而从语言a层次支持assertion功能Q这把assertionҎ能带来的负面媄响降到最?/p> <p>Java是通过增强一个关键字assert实现支持assertionQ而不是用一个库函数支持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员将处理两种cd的目标码Qdebug版本和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行时才发生的特炏VAssertionError通常为非常关键的错误Q这些错误往往是不Ҏ恢复的,而且assertion机制也不鼓励E序员对q种错误q行恢复。因此,Zassertion的含义,Java专家组选择了让AssertError为Error的子cR?/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研Iassert是如何定位的。如果开启一个子cȝassertionQ那么它的父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> { // Lassertionp|</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> { // Lassertionp|</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ȝassertQ父cȝassert仍然不运行。例如,我们执行java -ea:Derived Derived的时候,Basecȝassert语句q不执行。因此,我们可以认ؓQassert语句不具有承功能?/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序的风|assertionq用的目标,E序的性质{问题。通常来说Qassertion用于查一些关键的|q且q些值对整个E序Q或者局部功能的完成有很大的影响Qƈ且这U错误不Ҏ恢复的。assertion表达式应该短、易懂,如果需要评估复杂的表达式,应该使用函数计算。以下是一些用assertion的情늚例子Q这些方式可以让javaE序的可靠性更高?/p> <p align="left">1. <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 查控制流Q在if-then-else和swith-case语句中,我们可以在不应该发生的控制支上加上assert false语句。如果这U情况发生了Qassert能够查出来?br /> 例如Qx取值只能1,2,3Q我们的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有些函敎ͼ要求输入满一些特定的条gQ那么我们可以在函数开始处使用assertq行参数查。对于公共函敎ͼ我们通常不用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也可能是一个复杂的表达式。对于一些关键的不变量,我们可以通过assertq行查?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验证Qassert 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灵zd调试和测试机Ӟ它的使用也非常简单、方ѝ但是,如何规范、系l地使用assertion(特别是在Java语言?仍然是一个亟待研I的问题</p> </div> <img src ="http://www.tkk7.com/balajinima/aggbug/298533.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/balajinima/" target="_blank">李云?/a> 2009-10-16 11:38 <a href="http://www.tkk7.com/balajinima/articles/298533.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>W一部分Q声明和讉K控制(l习?http://www.tkk7.com/balajinima/articles/298532.html李云?/dc:creator>李云?/author>Fri, 16 Oct 2009 03:37:00 GMThttp://www.tkk7.com/balajinima/articles/298532.htmlhttp://www.tkk7.com/balajinima/comments/298532.htmlhttp://www.tkk7.com/balajinima/articles/298532.html#Feedback0http://www.tkk7.com/balajinima/comments/commentRss/298532.htmlhttp://www.tkk7.com/balajinima/services/trackbacks/298532.htmlW一部分Q声明和讉K控制

  • 知道怎么样声明、创建和初始化各U类型的数组
  • 知道怎么样声明类、内部类、方法、成员变量、静态变量和Ҏ内部变量Qƈ会用合法的修饰W(如public,final,static,abstract,{等Q。了解这些修饰符单独使用或组合用的含义Qƈ且知道它们对于包作用域的影响?
  • 了解cȝ构造器QconstructorQ?
  • 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

 

  解析  八进制g0开_?x开头的为十六进制|八进制中不能出现数字8Q最大只??/p>

 

§1.1.5       ?/a>

Which are not Java keywords?

  A. TRUE

  B. sizeof

  C. const

  D. super

  E. void

 

  译

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

 

  {案 A,B

 

  解析

   AQ不是,Java中有true,但是q也不是关键字而是字面量(literalQ?/p>

 BQ不是,Java中不需要这个操作符Q所有的cdQ原始类型)的大都是固定的?/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. cd载器通过分离本机文gpȝ的类和从|络导入的类增加安全性?/p>

 

  SL275中描q的JavaE序q行的过E是q样的:cd载器Qclass loaderQ加载程序运行所需要的所有类Q它通过区分本机文gpȝ的类和网l系l导入的cd加安全性,q可以限制Q何的Ҏ伊木马程序,因ؓ本机cLL先被加蝲Q一旦所有的c被加蝲完,执行文g的内存划分就固定了,在这个时候特定的内存地址被分配给对应的符号引用,查找表(lookuo tableQ也被徏立,׃内存划分发生在运行时Q解释器在受限制的代码区增加保护防止未授权的讉KQ然后字节码校验器(byte code verifierQ进行校验,主要执行下面的检查:cȝ合JVM规范的类文g格式Q没有违反访问限Ӟ代码没有造成堆栈的上溢或者下溢,所有操作代码的参数cd都是正确的,没有非法的数据类型{换(例如整型数转换成对象类型)发生Q校验通过的字节码被解释器QinterpreterQ执行,解释器在必要旉过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

 



]]>
解析Java对象的equals()和hashCode()的?/title><link>http://www.tkk7.com/balajinima/articles/298351.html</link><dc:creator>李云?/dc:creator><author>李云?/author><pubDate>Thu, 15 Oct 2009 03:21:00 GMT</pubDate><guid>http://www.tkk7.com/balajinima/articles/298351.html</guid><wfw:comment>http://www.tkk7.com/balajinima/comments/298351.html</wfw:comment><comments>http://www.tkk7.com/balajinima/articles/298351.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/balajinima/comments/commentRss/298351.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/balajinima/services/trackbacks/298351.html</trackback:ping><description><![CDATA[<p><strong>前言</strong> </p> <p>在Java语言中,equals()和hashCode()两个函数的用是紧密配合的,你要是自p计其中一个,p设计另外一个。在多数情况 下,q两个函数是不用考虑的,直接使用它们的默认设计就可以了。但是在一些情况下Q这两个函数最好是自己设计Q才能确保整个程序的正常q行。最常见的是?一个对象被加入集合对象Qcollection objectQ时Q这两个函数必须自己设计。更l化的定义是Q如果你惛_一个对象A攑օ另一个Collection Object B里,或者用这个对象A为查找一个元对象在Collection Object    B里位|的keyQƈ支持是否容纳Q删除Collection Object    B里的元对象这L操作Q那么,equals()和hashCode()函数必须开发者自己定义。其他情 况下Q这两个函数是不需要定义的?/p> <p><strong>equals():</strong> <br /> 它是用于q行两个对象的比较的Q是对象内容的比较,当然也能用于q行对象参阅值的比较。什么是对象参阅值的比较Q就是两个参阅变量的值得比较Q我?都知道参阅变量的值其实就是一个数字,q个数字可以看成是鉴别不同对象的代号。两个对象参阅值的比较Q就是两个数字的比较Q两个代L比较。这U比较是?认的对象比较方式Q在Objectq个对象中,q种方式已l设计好了。所以你也不用自己来重写Q浪费不必要的时间?/p> <p>对象内容的比较才是设计equals()的真正目的,Java语言对equals()的要求如下,q些要求是必遵循的。否则,你就不该费旉Q?<br /> 对称性:如果x.equals(y)q回?#8220;true”Q那么y.equals(x)也应该返回是“true”?<br /> 反射性:x.equals(x)必须q回?#8220;true”?<br /> cL性:如果x.equals(y)q回?#8220;true”Q而且y.equals(z)q回?#8220;true”Q那么z.equals(x)也应该返回是“true”?<br /> q有一致性:如果x.equals(y)q回?#8220;true”Q只要x和y内容一直不变,不管你重复x.equals(y)多少ơ,q回都是“true”?<br /> M情况下,x.equals(null)Q永q返回是“false”Qx.equals(和x不同cd的对?永远q回?#8220;false”?<br /> <strong>hashCode():</strong><br /> q?个函数返回的是一个用来进行hash操作的整型代P请不要把q个代号和前面所说的参阅变量所代表的代号弄混了。后者不仅仅是个代号q具有在内存中才查找?象的位置的功能。hashCode()所q回的值是用来分类对象在一些特定的Collection对象中的位置。这些对象是HashMap, Hashtable, HashSetQ等{。这个函数和上面的equals()函数必须自己设计Q用来协助HashMap, Hashtable, HashSetQ等{对自己所攉的大量对象进行搜d定位?/p> <p>q些Collection对象I竟如何工作的,惌每个元对象hashCode是一个箱子的 ~码Q按照编码,每个元对象就是根据hashCode()提供的代号归入相应的子里。所有的子加v来就是一个HashSetQHashMapQ或 Hashtable对象Q我们需要寻找一个元对象Ӟ先看它的代码Q就是hashCode()q回的整型|q样我们扑ֈ它所在的子Q然后在子里,?个元对象都拿出来一个个和我们要扄对象q行ҎQ如果两个对象的内容相等Q我们的搜寻也就l束。这U操作需要两个重要的信息Q一是对象的 hashCode()Q还有一个是对象内容Ҏ的结果?/p> <p>hashCode()的返回值和equals()的关pd下:</p> <p>如果x.equals(y)q回“true”Q那么x和y的hashCode()必须相等?<br /> 如果x.equals(y)q回“false”Q那么x和y的hashCode()有可能相{,也有可能不等?</p> <p>Z么这两个规则是这LQ原因其实很单,拿HashSet来说吧,HashSet可以拥有一个或更多的箱子,在同一个箱子中可以有一?或更多的独特元对象(HashSet所容纳的必L独特的元对象Q。这个例子说明一个元对象可以和其他不同的元对象拥有相同的hashCode。但是一?元对象只能和拥有同样内容的元对象相等。所以这两个规则必须成立?在实际的某个集合对象如HashSet set.contains(object o);Ӟ是先通过 hashcode()扑ֈ“子” Q在Ҏ equals判断对象内容 是否相等Q从而判断集合对象是否包含某个元对象Q?/p> <p><strong>设计q两个函数所要注意到的:<br /> </strong>如果你设计的对象cdq不使用于Collection对象Q那么没有必要自己再设计q两个函数的处理方式。这是正的面向对象设计ҎQQ何用户一时用不到的功能,先不要设计Q以免给日后功能扩展带来ȝ?/p> <p>如果你在设计时想别出心裁Q不遵守以上的两套规则,那么劝你q是不要做这h入非非的事。我q没有遇到过哪一个开发者和我说设计q两个函数要q背前面说的两个规则Q我到q些q反规则的情冉|Q都是作计错误处理?/p> <p>当一个对象类型作为Collection对象的元对象Ӟq个对象应该拥有自己处理equals()Q和/或处理hashCode()的设计,而且要遵守前面所?的两U原则。equals()先要查null和是否是同一cd。查同一cd是ؓ了避免出现ClassCastExceptionq样的异常给丢出来。查 null是ؓ了避免出现NullPointerExceptionq样的异常给丢出来?/p> <p>如果你的对象里面容纳的数据过多,那么q两个函?equals()和hashCode()会变得效率低。如果对象中拥有无法serialized的数据,equals()有可能在操作中出现错误。想?一个对象xQ它的一个整型数据是transient型(不能被serialize成二q制数据)。然而equals()和hashCode()都有依靠 q个整型数据Q那么,q个对象在serialization之前和之后,是否一P{案是不一栗因为serialization之前的整型数据是有效?数据Q在serialization之后Q这个整型数据的值ƈ没有存储下来Q再重新׃q制数据{换成对象后,两者(对象在serialization 之前和之后)的状态已l不同了。这也是要注意的?/p> <p><strong>知道以上q些能够帮助你:<br /> </strong>1. q行更好的设计和开发?br /> 2. q行更好的测试案例开发?br /> 3. 在面试过E中让面试者对你的学识渊博感到满意?</p> <p><span style="color: red">在Hibernate中,POJOc要重写hashcode()Ҏ和equals()Ҏ。ؓ什么呢Q?/span></p> <p>1Q重ҎequalsQ重写hashCode只是技术要求(Z提高效率Q?</p> <p>2Qؓ什么要重写equals呢,因ؓ在java的集合框架中Q是通过equals来判断两个对象是否相{的 </p> <p>3Q在hibernate中,l常使用set集合来保存相兛_象,而set集合是不允许重复的,但是下面的程序,判断一下运行结果:</p> <p>    Set user = new HashSet(); <br />     user.add(new Book("_Nstruts")); <br />     user.add(new Book("_Nstruts")); <br />     System.out.println(user.size()); </p> <p>上面E序的运行结果取决于BookcL否重写了equalsҎ?/p> <p>如果没有重写Q默认equals是比较地址Q那么这两个book对象不一P输出2Q意味着hibernate会认是两个对象,再接下来的持久化q程中可能会出错?/p> <p>如果重写了equalsQ比如按照主键(书名Q比较,那么q两个对象是一LQ输? ?/p> <p><br />  </p> <img src ="http://www.tkk7.com/balajinima/aggbug/298351.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/balajinima/" target="_blank">李云?/a> 2009-10-15 11:21 <a href="http://www.tkk7.com/balajinima/articles/298351.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>java异常分类http://www.tkk7.com/balajinima/articles/298342.html李云?/dc:creator>李云?/author>Thu, 15 Oct 2009 03:03:00 GMThttp://www.tkk7.com/balajinima/articles/298342.htmlhttp://www.tkk7.com/balajinima/comments/298342.htmlhttp://www.tkk7.com/balajinima/articles/298342.html#Feedback1http://www.tkk7.com/balajinima/comments/commentRss/298342.htmlhttp://www.tkk7.com/balajinima/services/trackbacks/298342.htmlJava中异常的分类

所有异常,都承自java.lang.ThrowablecR?/span>

Throwable有两个直接子c,ErrorcdExceptioncR?/span>

Exception

Exception则可使从M标准Java库的cL法,自己的方法以及运行时M异常中抛出来的基cd?/span>

异常可分为执行异常(RuntimeExceptionQ和查异常(Checked ExceptionsQ两U?/span>

RuntimeException

RuntimeException在默认情况下会得到自动处理。所以通常用不着捕获RuntimeExceptionQ但在自q装里,也许仍然要选择抛出一部分RuntimeException?/span>

RuntimeException是那些可能在 Java 虚拟机正常运行期间抛出的异常的超cR可能在执行Ҏ期间抛出但未被捕LRuntimeException的Q何子c都无需?/span>throws子句中进行声明。(java apiQ?/span>

它是uncheckedExcepiton?/span>

Java.lang.ArithmeticException

Java.lang.ArrayStoreExcetpion

Java.lang.ClassCastException

Java.lang.EnumConstantNotPresentException

Java.lang.IllegalArgumentException

       Java.lang.IllegalThreadStateException

       Java.lang.NumberFormatException

Java.lang.IllegalMonitorStateException

Java.lang.IllegalStateException

Java.lang.IndexOutOfBoundsException

       Java.lang.ArrayIndexOutOfBoundsException

       Java.lang.StringIndexOutOfBoundsException

Java.lang.NegativeArraySizeException’

Java.lang.NullPointerException

Java.lang.SecurityException

Java.lang.TypeNotPresentException

Java.lang.UnsupprotedOperationException

CheckedException

除了runtimeException以外的异常,都属?/span>checkedExceptionQ它们都?/span>java.lang库内部定义?/span>Java~译器要求程序必Lh声明抛出q种异常?/span>

一个方法必通过throws语句在方法的声明部分说明它可能抛Zq未捕获的所?/span>checkedException?/span>

Java.lang.ClassNotFoundException

Java.lang.CloneNotSupportedException

Java.lang.IllegalAccessException

Java.lang.InterruptedException

Java.lang.NoSuchFieldException

Java.lang.NoSuchMetodException

Error

当程序发生不可控q种错误Ӟ通常的做法是通知用户q中止程序的执行?/span>

与异怸同的?/span>Error及其子类的对象不应被抛出?/span>

Error ?/span> Throwable 的子c,代表~译旉和系l错误,用于指示合理的应用程?/span>不应该试图捕?/span>的严重问题。大多数q样的错误都是异常条件。虽?/span> ThreadDeath 错误是一?/span>正规的条Ӟ但它也是 Error 的子c,因ؓ大多数应用程序都不应该试图捕获它?/span>

在执行该Ҏ期间Q?/span>无需在其 throws 子句中声明可能抛Z是未能捕?/span>?/span> Error 的Q何子c,因ؓq些错误可能是再也不会发生的异常条g?/span>

它是uncheckedExcepiton?/span>



]]>
վ֩ģ壺 պƷƵѹۿ| ŷպƵ| լ߹ۿ޲ | ͵޾Ʒ͵һ| ޹Ƭ߹ۿ| ޳AƬ߹ۿ| ձ㽶Ƶ| ޹Ʒۺһ| ҹƷ޿| ɫһۺ| ձһ| ղһ| ޾Ʒ벻߲| ɫۺϾþþƷ޹| ޾Ʒ| ˳߹ۿվ| һ˿hdƵ| ҵijdzӪѹۿ | ҹëƬѹۿƵ| ʵƵѹۿ| ʵŹ| ҹ߹ۿ| ޵Ӱһ| ޹Ʒ˾þõӰ| ۺվ| ޾Ʒ91ר| һëƬѹۿ| ѹۿ91Ƶ| ѾƷԲ߹ۿͼƬ | 99þþþþѿ| ձĻ| Ƶۿ99| ˰ձƵ| պϵ | ƷŮþþ| vvվ| Ƶ߹ۿ| ɫվ| H⶯߹ۿ鶹| 99þ99ȾƷѹۿ| վѹۿ|