﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-Scott@JAVA-随笔分类-Jave SE 6</title><link>http://www.blogjava.net/cisco/category/5494.html</link><description>Java, 一杯浓浓的咖啡伴你到深夜</description><language>zh-cn</language><lastBuildDate>Fri, 27 Jul 2007 20:52:36 GMT</lastBuildDate><pubDate>Fri, 27 Jul 2007 20:52:36 GMT</pubDate><ttl>60</ttl><item><title>A Glossary of Name Reuse</title><link>http://www.blogjava.net/cisco/archive/2007/07/25/132390.html</link><dc:creator>Scott@JAVA</dc:creator><author>Scott@JAVA</author><pubDate>Wed, 25 Jul 2007 13:26:00 GMT</pubDate><guid>http://www.blogjava.net/cisco/archive/2007/07/25/132390.html</guid><wfw:comment>http://www.blogjava.net/cisco/comments/132390.html</wfw:comment><comments>http://www.blogjava.net/cisco/archive/2007/07/25/132390.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/cisco/comments/commentRss/132390.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/cisco/services/trackbacks/132390.html</trackback:ping><description><![CDATA[From &lt;&lt;Java Puzzlers&gt;&gt;, Chapter 8. Classier Puzzlers<br><img height=240 alt="" src="http://ec1.images-amazon.com/images/I/51R5GN5BJML._AA240_.jpg" width=240 border=0><br><br>
<h3 class=docSection1Title>A Glossary of Name Reuse</h3>
<p class=docText>Most of the puzzles in this chapter were based on name reuse. This section summarizes the various forms of name reuse.</p>
<h4 class=docSection2Title>Overriding</h4>
<p class=docText>An instance method <span class=docEmphasis>overrides</span> all accessible instance methods with the same signature in superclasses [JLS 8.4.8.1], enabling <span class=docEmphasis>dynamic dispatch;</span> in other words, the VM chooses which overriding to invoke based on an instance's run-time type [JLS 15.12.4.4]. Overriding is fundamental to object-oriented programming and is the only form of name reuse that is not generally discouraged:</p>
<pre>class Base {
public void f() { }
}
class Derived extends Base {
<span class=docEmphStrong>public void f() { } // overrrides Base.f()</span>
}
</pre>
<br>
<h5 class=docSection3Title>Hiding</h5>
<p class=docText>A field, static method, or member type <span class=docEmphasis>hides</span> all accessible fields, static methods, or member types, respectively, with the same name (or, for methods, signature) in supertypes. Hiding a member prevents it from being inherited [JLS 8.3, 8.4.8.2, 8.5]:</p>
<pre>class Base {
public static void f() { }
}
class Derived extends Base {
<span class=docEmphStrong>public static void f() { } // hides Base.f()</span>
}
</pre>
<br>
<h5 class=docSection3Title>Overloading</h5>
<p class=docText>Methods in a class <span class=docEmphasis>overload</span> one another if they have the same name and different signatures. The overloaded method designated by an invocation is selected at compile time [JLS 8.4.9, 15.12.2]:</p>
<pre>class CircuitBreaker {
<span class=docEmphStrong>public void f(int i)    { } // int overloading</span>
<span class=docEmphStrong>public void f(String s) { } // String overloading</span>
}
</pre>
<br>
<h5 class=docSection3Title>Shadowing</h5>
<p class=docText>A variable, method, or type <span class=docEmphasis>shadows</span> all variables, methods, or types, respectively, with the same name in a textually enclosing scope. If an entity is shadowed, you cannot refer to it by its simple name; depending on the entity, you cannot refer to it at all [JLS 6.3.1]:</p>
<pre>class WhoKnows {
static String sentence = "I don't know.";
public static void main(String[] args) {
<span class=docEmphStrong>String sentence = "I know!";   // shadows static field</span>
System.out.println(sentence);  // prints local variable
}
}
</pre>
<br>
<p class=docText>Although shadowing is generally discouraged, one common idiom does involve shadowing. Constructors often reuse a field name from their class as a parameter name to pass the value of the named field. This idiom is not without risk, but most Java programmers have decided that the stylistic benefits outweigh the risks:</p>
<pre>class Belt {
private final int size;
<span class=docEmphStrong>public Belt(int size) { // Parameter shadows Belt.size</span>
this.size = size;
}
}
</pre>
<br>
<h5 class=docSection3Title>Obscuring</h5>
<p class=docText>A variable <span class=docEmphasis>obscures</span> a type with the same name if both are in scope: If the name is used where variables and types are permitted, it refers to the variable. Similarly, a variable or a type can obscure a package. Obscuring is the only kind of name reuse where the two names are in different namespaces: variables, packages, methods, or types. If a type or a package is obscured, you cannot refer to it by its simple name except in a context where the syntax allows only a name from its namespace. Adhering to the naming conventions largely eliminates obscuring [JLS 6.3.2, 6.5]:</p>
<pre>public class Obscure {
<span class=docEmphStrong>static String System; // Obscures type java.lang.System</span>
public static void main(String[] args) {
// Next line won't compile: System refers to static field
System.out.println("hello, obscure world!");
}
}
</pre>
<img src ="http://www.blogjava.net/cisco/aggbug/132390.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/cisco/" target="_blank">Scott@JAVA</a> 2007-07-25 21:26 <a href="http://www.blogjava.net/cisco/archive/2007/07/25/132390.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>初始化</title><link>http://www.blogjava.net/cisco/archive/2007/01/25/95827.html</link><dc:creator>Scott@JAVA</dc:creator><author>Scott@JAVA</author><pubDate>Wed, 24 Jan 2007 17:29:00 GMT</pubDate><guid>http://www.blogjava.net/cisco/archive/2007/01/25/95827.html</guid><wfw:comment>http://www.blogjava.net/cisco/comments/95827.html</wfw:comment><comments>http://www.blogjava.net/cisco/archive/2007/01/25/95827.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/cisco/comments/commentRss/95827.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/cisco/services/trackbacks/95827.html</trackback:ping><description><![CDATA[当Java程序运行时，需要某一个类，但该类还没有载入内存，则Java程序将该类装入内存，然后立即执行类中定义的类初始化块，执行次序就是类初始化定义的次序，然后执行对象初始化块，执行次序是定义的次序，最后执行类的构造函数，继续对对象进行初始化。<br /><br />例：<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img id="Codehighlighter1_8_184_Open_Image" onclick="this.style.display='none'; Codehighlighter1_8_184_Open_Text.style.display='none'; Codehighlighter1_8_184_Closed_Image.style.display='inline'; Codehighlighter1_8_184_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img id="Codehighlighter1_8_184_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_8_184_Closed_Text.style.display='none'; Codehighlighter1_8_184_Open_Image.style.display='inline'; Codehighlighter1_8_184_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" /><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000"> A </span><span id="Codehighlighter1_8_184_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.blogjava.net/images/dot.gif" /></span><span id="Codehighlighter1_8_184_Open_Text"><span style="COLOR: #000000">{<br /><img id="Codehighlighter1_25_69_Open_Image" onclick="this.style.display='none'; Codehighlighter1_25_69_Open_Text.style.display='none'; Codehighlighter1_25_69_Closed_Image.style.display='inline'; Codehighlighter1_25_69_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="Codehighlighter1_25_69_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_25_69_Closed_Text.style.display='none'; Codehighlighter1_25_69_Open_Image.style.display='inline'; Codehighlighter1_25_69_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />    </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> A() </span><span id="Codehighlighter1_25_69_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.blogjava.net/images/dot.gif" /></span><span id="Codehighlighter1_25_69_Open_Text"><span style="COLOR: #000000">{<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        System.out.println(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">A的构造函数</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />    }</span></span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" /><br /><img id="Codehighlighter1_76_122_Open_Image" onclick="this.style.display='none'; Codehighlighter1_76_122_Open_Text.style.display='none'; Codehighlighter1_76_122_Closed_Image.style.display='inline'; Codehighlighter1_76_122_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="Codehighlighter1_76_122_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_76_122_Closed_Text.style.display='none'; Codehighlighter1_76_122_Open_Image.style.display='inline'; Codehighlighter1_76_122_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />    </span><span id="Codehighlighter1_76_122_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.blogjava.net/images/dot.gif" /></span><span id="Codehighlighter1_76_122_Open_Text"><span style="COLOR: #000000">{<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        System.out.println(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">A的动态初始化块</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />    }</span></span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" /><br /><img id="Codehighlighter1_136_182_Open_Image" onclick="this.style.display='none'; Codehighlighter1_136_182_Open_Text.style.display='none'; Codehighlighter1_136_182_Closed_Image.style.display='inline'; Codehighlighter1_136_182_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="Codehighlighter1_136_182_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_136_182_Closed_Text.style.display='none'; Codehighlighter1_136_182_Open_Image.style.display='inline'; Codehighlighter1_136_182_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />    </span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000"> </span><span id="Codehighlighter1_136_182_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.blogjava.net/images/dot.gif" /></span><span id="Codehighlighter1_136_182_Open_Text"><span style="COLOR: #000000">{<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        System.out.println(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">A的静态初始化块</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />    }</span></span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span></span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /><br /><img id="Codehighlighter1_205_381_Open_Image" onclick="this.style.display='none'; Codehighlighter1_205_381_Open_Text.style.display='none'; Codehighlighter1_205_381_Closed_Image.style.display='inline'; Codehighlighter1_205_381_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img id="Codehighlighter1_205_381_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_205_381_Closed_Text.style.display='none'; Codehighlighter1_205_381_Open_Image.style.display='inline'; Codehighlighter1_205_381_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" /></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000"> B </span><span style="COLOR: #0000ff">extends</span><span style="COLOR: #000000"> A </span><span id="Codehighlighter1_205_381_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.blogjava.net/images/dot.gif" /></span><span id="Codehighlighter1_205_381_Open_Text"><span style="COLOR: #000000">{<br /><img id="Codehighlighter1_222_266_Open_Image" onclick="this.style.display='none'; Codehighlighter1_222_266_Open_Text.style.display='none'; Codehighlighter1_222_266_Closed_Image.style.display='inline'; Codehighlighter1_222_266_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="Codehighlighter1_222_266_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_222_266_Closed_Text.style.display='none'; Codehighlighter1_222_266_Open_Image.style.display='inline'; Codehighlighter1_222_266_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />    </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> B() </span><span id="Codehighlighter1_222_266_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.blogjava.net/images/dot.gif" /></span><span id="Codehighlighter1_222_266_Open_Text"><span style="COLOR: #000000">{<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        System.out.println(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">B的构造函数</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />    }</span></span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" /><br /><img id="Codehighlighter1_273_319_Open_Image" onclick="this.style.display='none'; Codehighlighter1_273_319_Open_Text.style.display='none'; Codehighlighter1_273_319_Closed_Image.style.display='inline'; Codehighlighter1_273_319_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="Codehighlighter1_273_319_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_273_319_Closed_Text.style.display='none'; Codehighlighter1_273_319_Open_Image.style.display='inline'; Codehighlighter1_273_319_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />    </span><span id="Codehighlighter1_273_319_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.blogjava.net/images/dot.gif" /></span><span id="Codehighlighter1_273_319_Open_Text"><span style="COLOR: #000000">{<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        System.out.println(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">B的动态初始化块</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />    }</span></span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" /><br /><img id="Codehighlighter1_333_379_Open_Image" onclick="this.style.display='none'; Codehighlighter1_333_379_Open_Text.style.display='none'; Codehighlighter1_333_379_Closed_Image.style.display='inline'; Codehighlighter1_333_379_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="Codehighlighter1_333_379_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_333_379_Closed_Text.style.display='none'; Codehighlighter1_333_379_Open_Image.style.display='inline'; Codehighlighter1_333_379_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />    </span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000"> </span><span id="Codehighlighter1_333_379_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.blogjava.net/images/dot.gif" /></span><span id="Codehighlighter1_333_379_Open_Text"><span style="COLOR: #000000">{<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        System.out.println(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">B的静态初始化块</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />    }</span></span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span></span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /><br /><img id="Codehighlighter1_402_579_Open_Image" onclick="this.style.display='none'; Codehighlighter1_402_579_Open_Text.style.display='none'; Codehighlighter1_402_579_Closed_Image.style.display='inline'; Codehighlighter1_402_579_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img id="Codehighlighter1_402_579_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_402_579_Closed_Text.style.display='none'; Codehighlighter1_402_579_Open_Image.style.display='inline'; Codehighlighter1_402_579_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" /></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000"> Test </span><span id="Codehighlighter1_402_579_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.blogjava.net/images/dot.gif" /></span><span id="Codehighlighter1_402_579_Open_Text"><span style="COLOR: #000000">{<br /><img id="Codehighlighter1_447_577_Open_Image" onclick="this.style.display='none'; Codehighlighter1_447_577_Open_Text.style.display='none'; Codehighlighter1_447_577_Closed_Image.style.display='inline'; Codehighlighter1_447_577_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="Codehighlighter1_447_577_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_447_577_Closed_Text.style.display='none'; Codehighlighter1_447_577_Open_Image.style.display='inline'; Codehighlighter1_447_577_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />    </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> main(String[] args) </span><span id="Codehighlighter1_447_577_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.blogjava.net/images/dot.gif" /></span><span id="Codehighlighter1_447_577_Open_Text"><span style="COLOR: #000000">{<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        System.out.println(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">第一次生成类B的对象时输出</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> B();<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        System.out.println(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">第二次生成类B的对象时输出</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> B();<br /><img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />    }</span></span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span></span></div><br />程序运行结果如下：<br /><br />第一次生成类B的对象时输出<br />A的静态初始化块<br />B的静态初始化块<br />A的动态初始化块<br />A的构造函数<br />B的动态初始化块<br />B的构造函数<br />第二次生成类B的对象时输出<br />A的动态初始化块<br />A的构造函数<br />B的动态初始化块<br />B的构造函数<img src ="http://www.blogjava.net/cisco/aggbug/95827.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/cisco/" target="_blank">Scott@JAVA</a> 2007-01-25 01:29 <a href="http://www.blogjava.net/cisco/archive/2007/01/25/95827.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>运行时多态</title><link>http://www.blogjava.net/cisco/archive/2007/01/23/95574.html</link><dc:creator>Scott@JAVA</dc:creator><author>Scott@JAVA</author><pubDate>Tue, 23 Jan 2007 09:20:00 GMT</pubDate><guid>http://www.blogjava.net/cisco/archive/2007/01/23/95574.html</guid><wfw:comment>http://www.blogjava.net/cisco/comments/95574.html</wfw:comment><comments>http://www.blogjava.net/cisco/archive/2007/01/23/95574.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/cisco/comments/commentRss/95574.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/cisco/services/trackbacks/95574.html</trackback:ping><description><![CDATA[
		<p> </p>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<img id="Codehighlighter1_8_116_Open_Image" onclick="this.style.display='none'; Codehighlighter1_8_116_Open_Text.style.display='none'; Codehighlighter1_8_116_Closed_Image.style.display='inline'; Codehighlighter1_8_116_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" />
				<img id="Codehighlighter1_8_116_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_8_116_Closed_Text.style.display='none'; Codehighlighter1_8_116_Open_Image.style.display='inline'; Codehighlighter1_8_116_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" />
				<span style="COLOR: #0000ff">class</span>
				<span style="COLOR: #000000"> A </span>
				<span id="Codehighlighter1_8_116_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
						<img src="http://www.blogjava.net/images/dot.gif" />
				</span>
				<span id="Codehighlighter1_8_116_Open_Text">
						<span style="COLOR: #000000">{<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">int</span>
						<span style="COLOR: #000000"> i </span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">1</span>
						<span style="COLOR: #000000">;<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" /><br /><img id="Codehighlighter1_39_65_Open_Image" onclick="this.style.display='none'; Codehighlighter1_39_65_Open_Text.style.display='none'; Codehighlighter1_39_65_Closed_Image.style.display='inline'; Codehighlighter1_39_65_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="Codehighlighter1_39_65_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_39_65_Closed_Text.style.display='none'; Codehighlighter1_39_65_Open_Image.style.display='inline'; Codehighlighter1_39_65_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">char</span>
						<span style="COLOR: #000000"> f() </span>
						<span id="Codehighlighter1_39_65_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
								<img src="http://www.blogjava.net/images/dot.gif" />
						</span>
						<span id="Codehighlighter1_39_65_Open_Text">
								<span style="COLOR: #000000">{<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        </span>
								<span style="COLOR: #0000ff">return</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">'</span>
								<span style="COLOR: #000000">A</span>
								<span style="COLOR: #000000">'</span>
								<span style="COLOR: #000000">;<br /><img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />    }</span>
						</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />
								<br />
								<img id="Codehighlighter1_88_114_Open_Image" onclick="this.style.display='none'; Codehighlighter1_88_114_Open_Text.style.display='none'; Codehighlighter1_88_114_Closed_Image.style.display='inline'; Codehighlighter1_88_114_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" />
								<img id="Codehighlighter1_88_114_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_88_114_Closed_Text.style.display='none'; Codehighlighter1_88_114_Open_Image.style.display='inline'; Codehighlighter1_88_114_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">static</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">char</span>
						<span style="COLOR: #000000"> g() </span>
						<span id="Codehighlighter1_88_114_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
								<img src="http://www.blogjava.net/images/dot.gif" />
						</span>
						<span id="Codehighlighter1_88_114_Open_Text">
								<span style="COLOR: #000000">{<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        </span>
								<span style="COLOR: #0000ff">return</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">'</span>
								<span style="COLOR: #000000">A</span>
								<span style="COLOR: #000000">'</span>
								<span style="COLOR: #000000">;<br /><img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />    }</span>
						</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span>
				</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
						<br />
						<img id="Codehighlighter1_137_327_Open_Image" onclick="this.style.display='none'; Codehighlighter1_137_327_Open_Text.style.display='none'; Codehighlighter1_137_327_Closed_Image.style.display='inline'; Codehighlighter1_137_327_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" />
						<img id="Codehighlighter1_137_327_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_137_327_Closed_Text.style.display='none'; Codehighlighter1_137_327_Open_Image.style.display='inline'; Codehighlighter1_137_327_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" />
				</span>
				<span style="COLOR: #0000ff">class</span>
				<span style="COLOR: #000000"> B </span>
				<span style="COLOR: #0000ff">extends</span>
				<span style="COLOR: #000000"> A </span>
				<span id="Codehighlighter1_137_327_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
						<img src="http://www.blogjava.net/images/dot.gif" />
				</span>
				<span id="Codehighlighter1_137_327_Open_Text">
						<span style="COLOR: #000000">{<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">int</span>
						<span style="COLOR: #000000"> i </span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">2</span>
						<span style="COLOR: #000000">;                   </span>
						<span style="COLOR: #008000">//</span>
						<span style="COLOR: #008000">变量重名</span>
						<span style="COLOR: #008000">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />
						</span>
						<span style="COLOR: #000000">
								<br />
								<img id="Codehighlighter1_193_244_Open_Image" onclick="this.style.display='none'; Codehighlighter1_193_244_Open_Text.style.display='none'; Codehighlighter1_193_244_Closed_Image.style.display='inline'; Codehighlighter1_193_244_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" />
								<img id="Codehighlighter1_193_244_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_193_244_Closed_Text.style.display='none'; Codehighlighter1_193_244_Open_Image.style.display='inline'; Codehighlighter1_193_244_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">char</span>
						<span style="COLOR: #000000"> f() </span>
						<span id="Codehighlighter1_193_244_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
								<img src="http://www.blogjava.net/images/dot.gif" />
						</span>
						<span id="Codehighlighter1_193_244_Open_Text">
								<span style="COLOR: #000000">{                   </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000">方法重写</span>
								<span style="COLOR: #008000">
										<br />
										<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />
								</span>
								<span style="COLOR: #000000">        </span>
								<span style="COLOR: #0000ff">return</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">'</span>
								<span style="COLOR: #000000">B</span>
								<span style="COLOR: #000000">'</span>
								<span style="COLOR: #000000">;<br /><img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />    }</span>
						</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />
								<br />
								<img id="Codehighlighter1_267_325_Open_Image" onclick="this.style.display='none'; Codehighlighter1_267_325_Open_Text.style.display='none'; Codehighlighter1_267_325_Closed_Image.style.display='inline'; Codehighlighter1_267_325_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" />
								<img id="Codehighlighter1_267_325_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_267_325_Closed_Text.style.display='none'; Codehighlighter1_267_325_Open_Image.style.display='inline'; Codehighlighter1_267_325_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">static</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">char</span>
						<span style="COLOR: #000000"> g() </span>
						<span id="Codehighlighter1_267_325_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
								<img src="http://www.blogjava.net/images/dot.gif" />
						</span>
						<span id="Codehighlighter1_267_325_Open_Text">
								<span style="COLOR: #000000">{           </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000">这种重写不具有多态性。多态性指实例方法</span>
								<span style="COLOR: #008000">
										<br />
										<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />
								</span>
								<span style="COLOR: #000000">        </span>
								<span style="COLOR: #0000ff">return</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">'</span>
								<span style="COLOR: #000000">B</span>
								<span style="COLOR: #000000">'</span>
								<span style="COLOR: #000000">;<br /><img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />    }</span>
						</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span>
				</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
						<br />
						<img id="Codehighlighter1_348_749_Open_Image" onclick="this.style.display='none'; Codehighlighter1_348_749_Open_Text.style.display='none'; Codehighlighter1_348_749_Closed_Image.style.display='inline'; Codehighlighter1_348_749_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" />
						<img id="Codehighlighter1_348_749_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_348_749_Closed_Text.style.display='none'; Codehighlighter1_348_749_Open_Image.style.display='inline'; Codehighlighter1_348_749_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" />
				</span>
				<span style="COLOR: #0000ff">public</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">class</span>
				<span style="COLOR: #000000"> Test </span>
				<span id="Codehighlighter1_348_749_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
						<img src="http://www.blogjava.net/images/dot.gif" />
				</span>
				<span id="Codehighlighter1_348_749_Open_Text">
						<span style="COLOR: #000000">{<br /><img id="Codehighlighter1_393_747_Open_Image" onclick="this.style.display='none'; Codehighlighter1_393_747_Open_Text.style.display='none'; Codehighlighter1_393_747_Closed_Image.style.display='inline'; Codehighlighter1_393_747_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="Codehighlighter1_393_747_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_393_747_Closed_Text.style.display='none'; Codehighlighter1_393_747_Open_Image.style.display='inline'; Codehighlighter1_393_747_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">public</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">static</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">void</span>
						<span style="COLOR: #000000"> main(String[] args) </span>
						<span id="Codehighlighter1_393_747_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
								<img src="http://www.blogjava.net/images/dot.gif" />
						</span>
						<span id="Codehighlighter1_393_747_Open_Text">
								<span style="COLOR: #000000">{<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        B b </span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">new</span>
								<span style="COLOR: #000000"> B();<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        System.out.println(b.i);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        System.out.println(b.f());<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        System.out.println(b.g());<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        System.out.println(B.g());<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        A a </span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000"> b;                  </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000">子类对象当作父类对象使用</span>
								<span style="COLOR: #008000">
										<br />
										<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />
								</span>
								<span style="COLOR: #000000">        System.out.println(a.i);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        System.out.println(a.f());<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        System.out.println(a.g());<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        System.out.println(A.g());<br /><img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />    }</span>
						</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span>
				</span>
		</div>
		<p>
				<br />
				<br />程序运行结果：<br />2<br />B<br />B<br />B<br />1<br />B<br />A<br />A<br /><br />程序分析：在该示例程序中，变量i产生数据成员变量隐藏；实例方法f()是方法重写，对实例方法的重写会产生运行时多态性。static方法g()也是方法重写，但这种方法重写不具有运行时多态的特征，多态特征是针对实例方法而言。因此当子类对象作为父类对象使用时，在输出结果中看到，只有f()发生了运行时多态。</p>
<img src ="http://www.blogjava.net/cisco/aggbug/95574.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/cisco/" target="_blank">Scott@JAVA</a> 2007-01-23 17:20 <a href="http://www.blogjava.net/cisco/archive/2007/01/23/95574.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>The essential Java language library</title><link>http://www.blogjava.net/cisco/archive/2007/01/22/95275.html</link><dc:creator>Scott@JAVA</dc:creator><author>Scott@JAVA</author><pubDate>Mon, 22 Jan 2007 04:27:00 GMT</pubDate><guid>http://www.blogjava.net/cisco/archive/2007/01/22/95275.html</guid><wfw:comment>http://www.blogjava.net/cisco/comments/95275.html</wfw:comment><comments>http://www.blogjava.net/cisco/archive/2007/01/22/95275.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/cisco/comments/commentRss/95275.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/cisco/services/trackbacks/95275.html</trackback:ping><description><![CDATA[
		<p>
				<a name="N10058">
						<span class="atitle">
								<font face="Arial" size="4">Books</font>
						</span>
				</a>
		</p>
		<p>Every programmer has certain books that he or she wears out by constantly referencing them as a professional. The following books should be on every Java language programmer's bookshelf. Books can be expensive, so this list is intentionally small, limited to the critical ones.</p>
		<p>
				<a name="N10062">
						<span class="smalltitle">
								<strong>
										<font face="Arial">Thinking in Java (Bruce Eckel)</font>
								</strong>
						</span>
				</a>
		</p>
		<p>
				<a href="http://www.amazon.com/exec/obidos/ASIN/0131002872">
						<i>
								<font color="#5c81a7">Thinking in Java, 3rd edition</font>
						</i>
				</a> (Bruce Eckel; Prentice Hall PTR, 2002)<br />Eckel's book is an extremely practical book for learning about how to use object oriented well in a Java language context. Lots of code samples illustrate the concepts as he introduces them. The text is absolutely practical, from a person who doesn't think Java technology is always the right answer. Eckel has lots of experience with lots of languages, and has solid object oriented thinking skills. This book puts those skills in a practical Java language context. He?s also working on a new book called <i>Thinking in Enterprise Java</i>.</p>
		<p>
				<a name="N10074">
						<span class="smalltitle">
								<strong>
										<font face="Arial">Effective Java (Joshua Bloch)</font>
								</strong>
						</span>
				</a>
		</p>
		<p>
				<a href="http://www.amazon.com/exec/obidos/ASIN/0201310058">
						<i>
								<font color="#5c81a7">Effective Java: Programming Language Guide</font>
						</i>
				</a> (Joshua Bloch; Addison-Wesley, 2001)<br />This the best book for understanding the principles from which good Java programs are designed. Most of this material is not to be found in other "learning to Java" books. For example, Bloch's chapter on overriding <code>equals()</code> is one of the best I've ever read. He also includes practical advice on using interfaces instead of abstract classes, and using exceptions intelligently. Bloch was Sun's Java platform libraries architect, so he knows the language from the inside out. In fact, he wrote lots of the useful libraries included with the language. This is a must-read.</p>
		<p>
				<a name="N10087">
						<span class="smalltitle">
								<strong>
										<font face="Arial">The Java Programming Language (Ken Arnold, James Gosling, David Holmes)</font>
								</strong>
						</span>
				</a>
		</p>
		<p>
				<a href="http://www.amazon.com/exec/obidos/ASIN/0201704331">
						<i>
								<font color="#996699">The Java Programming Language</font>
						</i>
				</a> (Ken Arnold, James Gosling, David Holmes; Addison-Wesley, 2000)<br />This is probably the best Java language primer book available. It?s not a formal specification, but a readable introduction to each language feature. It has the right balance of rigor and pedagogy, enabling someone who understands programming to wrap their head around the Java language (and its copious class libraries) quickly.</p>
		<p>
				<a name="N10096">
						<span class="smalltitle">
								<strong>
										<font face="Arial">Concurrent Programming in Java: Design Principles and Patterns (Doug Lea)</font>
								</strong>
						</span>
				</a>
		</p>
		<p>
				<a href="http://www.amazon.com/exec/obidos/ASIN/0201310090">
						<i>
								<font color="#5c81a7">Concurrent Programming in Java: Design Principles and Patterns, 2nd edition</font>
						</i>
				</a> (Doug Lea; Addison-Wesley, 1999)<br />Not every developer needs to know about concurrency in such detail, and not every engineer is up to the level of this book, but there's no better survey of concurrent programming than this. If you?re interested, this is the source. Lea is a professional programmer at SUNY, and his work and ideas related to concurrency have been included in the JDK 5.0 spec (from JSR166), so you can be sure what he has to say about using the Java language effectively is worth listening to. He's a good communicator as well.</p>
		<p>
				<a name="N100A5">
						<span class="smalltitle">
								<strong>
										<font face="Arial">Expert One-On-One J2EE Design and Development (Rod Johnson)</font>
								</strong>
						</span>
				</a>
		</p>
		<p>
				<a href="http://www.amazon.com/exec/obidos/ASIN/0764543857">
						<i>
								<font color="#5c81a7">Expert One-On-One J2EE Design and Development</font>
						</i>
				</a> (Rod Johnson)<br />For newcomers to J2EE, this is the only book that really tells it like it is. This book is the result of years of experience of what works and what doesn't, and unlike a lot of other authors, Johnson is willing to say what doesn't. J2EE is often used when it's unnecessary overkill. Johnson's book can help you avoid that.</p>
		<p>
				<a name="N100B3">
						<span class="smalltitle">
								<strong>
										<font face="Arial">Refactoring (Martin Fowler, Kent Beck, John Brant, William Opdyke, Don Roberts)</font>
								</strong>
						</span>
				</a>
		</p>
		<p>
				<a href="http://www.amazon.com/exec/obidos/ASIN/0201485672">
						<i>
								<font color="#5c81a7">Refactoring: Improving the Design of Existing Code</font>
						</i>
				</a> (Martin Fowler, Kent Beck, John Brant, William Opdyke, Don Roberts; Addison-Wesley, 1999)<br />Fowler has written some of the most popular programming books ever published, including <i>Analysis Patterns</i>. His book on <i>refactoring</i> is the seminal work on the subject. Refactoring code is a neglected programmer discipline, but an intuitive programmer idea. Refactoring is improving the design of existing code without changing its results. It's the best way to keep your code clean, which keeps it easy to change over time. When do you refactor? Whenever your code 'smells'. Fowler?s book is full of example Java language code. Many Java language integrated development environments (IDEs), including IBM's Eclipse, incorporate Fowler's refactorings, using his names for each one, so it pays to be familiar with things like <i>extract method</i>.</p>
		<p>
				<a name="N100CB">
						<span class="smalltitle">
								<strong>
										<font face="Arial">Design Patterns (Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides)</font>
								</strong>
						</span>
				</a>
		</p>
		<p>
				<a href="http://www.amazon.com/exec/obidos/ASIN/0201633612">
						<i>
								<font color="#5c81a7">Design Patterns: Elements of Reusable Object Oriented Software</font>
						</i>
				</a> (Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides; Addison-Wesley, 1997)<br />This is one of the more famous books among professional programmers, and has become known as "the Gang of Four (GOF) book," based on the collective nickname of the authors. Patterns are reusable ways of thinking about and solving common programming problems. Learning patterns is a discipline. Using them well (or knowing when <b>not</b> to use them) is a skill. Ignoring them is a mistake. All of the examples in the book are in C++, but the Java language was born from that world, and it?s relatively simple for Java language programmers to relate to how to implement the patterns in the Java language. Being familiar with patterns, and knowing how to use them well, makes programming easier. It also makes communicating with other programmers easier, because patterns are a shortcut way to describe lots of related programming concepts that work together in a common solution to a common problem. Some of the more common ones, like <i>Factory Method</i>, are ubiquitous, even in the Java language itself. On the subject of using patterns wisely, you also might want to read Joshua Kerievsky's <i>Refactoring to Patterns</i>, which says you should let your code tell you when to implement a pattern.</p>
		<p>
				<a name="N100E3">
						<span class="smalltitle">
								<strong>
										<font face="Arial">Patterns of Enterprise Application Architecture (Martin Fowler)</font>
								</strong>
						</span>
				</a>
		</p>
		<p>
				<a href="http://www.amazon.com/exec/obidos/ASIN/0321127420">
						<i>
								<font color="#5c81a7">Patterns of Enterprise Application Architecture</font>
						</i>
				</a> (Martin Fowler; Addison-Wesley, 2002)<br />Enterprise development certainly presents more challenges than small, one-off projects. That doesn?t mean that all enterprise development challenges are new. In fact, more often than not, it <i>has</i> been done before. In many cases, Fowler's worked on projects that did it. His book talks about some of the common solutions, and offers guidance on usage, compromises, and alternatives. Fowler includes some familiar patterns here, such as Model View Controller (MVC), but he also includes some that may be new to you, like Page Controller for handling requests for specific pages or actions on a Web site. As with most patterns, once you read many of them, you think, 'I knew that already'. Perhaps, but it helps to have a common presentation of the pattern to refer to. That kind of reference is a superb help on large projects with multiple components developed by different people.</p>
		<p>
				<a name="N100F5">
						<span class="smalltitle">
								<strong>
										<font face="Arial">UML Distilled (Martin Fowler)</font>
								</strong>
						</span>
				</a>
		</p>
		<p>
				<a href="http://www.amazon.com/exec/obidos/ASIN/0321193687">
						<i>
								<font color="#5c81a7">UML Distilled: A Brief Guide to the Standard Object Modeling Language</font>
						</i>
				</a> (Martin Fowler; Addison-Wesley 2003)<br />UML is an important common visual communication language for professional programmers, but it's overused and grossly abused. You don't need to know much to communicate with UML. Martin's distillation gives you the essentials. In fact, the inside front and back covers give you almost everything you'll ever use on a regular basis. All of the code behind the UML examples in the book is Java code.</p>
		<p>
				<a name="N10104">
						<span class="smalltitle">
								<strong>
										<font face="Arial">Test-Driven Development: By Example (Kent Beck)</font>
								</strong>
						</span>
				</a>
		</p>
		<p>
				<a href="http://www.amazon.com/exec/obidos/ASIN/0321146530">
						<i>
								<font color="#5c81a7">Test-Driven Development: By Example</font>
						</i>
				</a> (Kent Beck; Addison-Wesley 2002)<br />Test-first programming will revolutionize your programming, and can help you become one of the better programmers out there. Writing tests before you write code is an initially awkward, but powerful development skill. By writing tests first, you can keep your code simpler, and you can be sure it works from the start (practicing what he preaches, Beck co-wrote JUnit, the most prevalent testing framework for the Java language, test-first). Beck?s book is the definitive source, and the extended Money example is in the Java language. Beck walks through how to <b>think</b> test-first, which can be an initial barrier for many programmers.</p>
		<p>
				<a name="N10116">
						<span class="smalltitle">
								<strong>
										<font face="Arial">The Pragmatic Programmer: From Journeyman to Master (Andy Hunt and Dave Thomas)</font>
								</strong>
						</span>
				</a>
		</p>
		<p>
				<a href="http://www.amazon.com/exec/obidos/ASIN/020161622X">
						<i>
								<font color="#5c81a7">The Pragmatic Programmer: From Journeyman to Master</font>
						</i>
				</a> (Andrew Hunt and David Thomas; Addison-Wesley 1999)<br />Being an object oriented purist has its advantages. In today?s complex world, you often have to compromise in order to get things done as a Java language developer. The guiding principle is to be pragmatic. Hunt and Thomas talk about how to do that, without compromising what?s really important. This isn?t a Java language book, but it?s a critical mindset book for Java language developers. For example, I don't know of a single programmer who couldn't benefit from the admonition to "fix the problem, not the blame" and to sign your work like a proud craftsman.</p>
		<p>
				<a name="N10125">
						<span class="smalltitle">
								<strong>
										<font face="Arial">Peopleware: Productive Projects and Teams (Tom DeMarco and Timothy Lister)</font>
								</strong>
						</span>
				</a>
		</p>
		<p>
				<a href="http://www.amazon.com/exec/obidos/ASIN/0932633439/qid=1112295380/sr=2-2/ref=pd_ka_b_2_2/102-0023248-0497724">
						<i>
								<font color="#5c81a7">Peopleware: Productive Projects and Teams</font>
						</i>
				</a> (Tom DeMarco, Timothy Lister; Dorset House, 1999)<br />All of the other books on this list are at least somewhat technical. This one is not. In all of the technical jargon, and in the sea of acronyms, sometimes software developers and managers forget that <i>people</i> make software. DeMarco and Lister remind us of that fact, and why it should make a difference. This isn?t a book about a particular programming language, but every Java language programmer should read it. There are other good books about how killing programmers is counterproductive for managers, but this is one of the best.<br /><br /></p>
		<hr />
		<p>
				<a name="N10137">
						<span class="atitle">
								<font face="Arial" size="4">Web sites</font>
						</span>
				</a>
		</p>
		<p>There are more Web sites than you could visit in a lifetime, that is, if you wanted to digest any of their content. An exhaustive list of sites with content about some aspect of the Java language would be ridiculously large. The following sites are tried and true.</p>
		<p>
				<a name="N10141">
						<span class="smalltitle">
								<strong>
										<font face="Arial">Sun's Java Technology site</font>
								</strong>
						</span>
				</a>
		</p>
		<p>
				<a href="http://java.sun.com/">
						<font color="#5c81a7">Sun's Java language site</font>
				</a>
				<br />This is Sun?s main Java language site. You will find yourself visiting this site very frequently as a Java language developer. The following links are particularly important, especially to new Java language developers: 
</p>
		<ul>
				<li>
						<b>New to Java Center</b>
						<br />
						<a href="http://java.sun.com/learning/new2java/index.html">
								<font color="#5c81a7">New to Java Center</font>
						</a>
						<br />The New to Java Center is a good clearing house for step-by-step links to Java technology resources. If you're new to the language, this is a great place to start. 
</li>
				<li>
						<b>Tutorials and Code Camps</b>
						<br />
						<a href="http://java.sun.com/docs/books/tutorial/index.html">
								<font color="#5c81a7">Java Tutorial</font>
						</a>
						<br />The infamous Java Tutorial is here, along with other tutorials on various aspects of the Java language (such as Collections). </li>
		</ul>
		<p>
		</p>
		<p>
				<a name="N10169">
						<span class="smalltitle">
								<strong>
										<font face="Arial">IBM developerWorks</font>
								</strong>
						</span>
				</a>
		</p>
		<p>
				<a href="http://www.ibm.com/developerworks/">
						<font color="#5c81a7">IBM's developerWorks</font>
				</a>
				<br />This may seem shameless self-promotion, but developerWorks is a great resource for tutorials and articles about Java language tools and techniques. Content ranges from a beginner's guide to learning the language, to advanced concurrency techniques. You can search content by topic, and can browse by type.</p>
		<p>
				<a name="N10177">
						<span class="smalltitle">
								<strong>
										<font face="Arial">The Apache Software Foundation</font>
								</strong>
						</span>
				</a>
		</p>
		<p>
				<a href="http://www.apache.org/">
						<font color="#5c81a7">The Apache Software Foundation</font>
				</a>
				<br />The Apache site is home to many reusable libraries (in the Commons area) and tools to help Java developers. It?s all Open Source, so download what you want. Many extremely popular Java language libraries and tools (such as Struts, Ant, and Tomcat) began as Apache projects. The Jakarta area has most of the emerging Java language stuff.</p>
		<p>
				<a name="N10184">
						<span class="smalltitle">
								<strong>
										<font face="Arial">Eclipse.org</font>
								</strong>
						</span>
				</a>
		</p>
		<p>
				<a href="http://www.eclipse.org/">
						<font color="#5c81a7">Eclipse</font>
				</a>
				<br />There are several good Java language integrated development environments (IDEs). Eclipse (from IBM) is one of the newest, and is rapidly becoming the premier IDE for Java language development. It?s entirely Open Source, which means it?s free. This site contains all sorts of resources for learning about how to use Eclipse effectively. It also has information about the Standard Widget Toolkit (SWT), a lighter weight alternative to Swing.</p>
		<p>
				<a name="N10191">
						<span class="smalltitle">
								<strong>
										<font face="Arial">Eclipse Plugin Central and EclipsePlugins</font>
								</strong>
						</span>
				</a>
		</p>
		<p>
				<a href="http://www.eclipseplugincentral.com/">
						<font color="#5c81a7">Eclipse Plugin Central</font>
				</a> and <a href="http://eclipse-plugins.2y.net/"><font color="#5c81a7">EclipsePlugins</font></a><br />Eclipse is based on a plug-in architecture. In fact, the Java language development components of Eclipse are plug-ins. But there are literally hundreds of plug-ins for everything from Web development to playing games within the Eclipse environment. These two sites list most of them, by category, and are searchable. They are excellent resources. If you use Eclipse, and want to do something new in your development environment, odds are good that there?s a plug-in for it, and that you can find it here. Both sites let people review the plug-ins, so you can get some information about which ones are good and which ones aren?t worth your time.</p>
		<p>
				<a name="N101A2">
						<span class="smalltitle">
								<strong>
										<font face="Arial">JUnit.org</font>
								</strong>
						</span>
				</a>
		</p>
		<p>
				<a href="http://www.junit.org/">
						<font color="#5c81a7">JUnit.org</font>
				</a>
				<br />JUnit is the seminal unit testing framework for the Java language. The site contains the latest and greatest version, plus a huge number of other resources about testing (in the Java language and others) at various levels, for desktop apps, Web apps, J2EE apps, etc. If you want to find testing resources, this is the best place to start.</p>
		<p>
				<a name="N101AF">
						<span class="smalltitle">
								<strong>
										<font face="Arial">TheServerSide.com</font>
								</strong>
						</span>
				</a>
		</p>
		<p>
				<a href="http://www.theserverside.com/">
						<font color="#5c81a7">TheServerSide.com</font>
				</a>
				<br />If you are (or will be) involved with Java language development on the server side, this site is a vital resource. You can find articles here on JBoss, J2EE, LDAP, Struts, and a host of other topics, and it?s fully searchable. They don't simply describe features of the Java language, or supporting libraries. They take it one step further to describe novel uses of libraries (such as using Jakarta's Velocity as a rules engine, rather than a templating engine). They also provide running commentary on the state of the Java language (a current article is titled <i>Java is boring</i>, by Tim Bray). One of the better common features of the site are matrix comparisons of Java language tools and products (application servers, etc.).</p>
		<p>
				<a name="N101BF">
						<span class="smalltitle">
								<strong>
										<font face="Arial">Bruce Eckel's MindView, Inc.</font>
								</strong>
						</span>
				</a>
		</p>
		<p>
				<a href="http://www.bruceeckel.com/">
						<font color="#5c81a7">Bruce Eckel's MindView, Inc.</font>
				</a>
				<br />Eckel wrote several books about "thinking in" the Java language, Python, and C++. His <i>Thinking in Java</i> was particularly helpful to me as I was learning the language. It?s practical and to the point, with excellent insights about how to think in objects in a Java language context. You can download free electronic copies of all of his books at this site. He also has written many good articles, and he?s got links to all of them here (including articles on Jython, Java vs. .NET, etc.).</p>
		<p>
				<a name="N101CF">
						<span class="smalltitle">
								<strong>
										<font face="Arial">ONJava.com</font>
								</strong>
						</span>
				</a>
		</p>
		<p>
				<a href="http://www.onjava.com/">
						<font color="#996699">ONJava.com</font>
				</a>
				<br />O?Reilley has published excellent books about programming languages and tools for years. Their Java-language focused Web site is just as good. It has articles on various Java language tools (like JDOM and Hibernate), different areas for different parts of the Java Platform (like J2SE and J2EE). It?s fully searchable. They have excellent articles and tutorials. The site is arranged by topic. Some examples include Java and XML, Java Security, Wireless Java, and Java SysAdmin. The site also has a link to the O'Reilley Learning Lab, where you can take online courses (Java language-related and otherwise). They aren't free, but many count toward college credit, so you get a convenient way to learn skills, and some credentials as well.</p>
		<p>
				<a name="N101DC">
						<span class="smalltitle">
								<strong>
										<font face="Arial">java.net</font>
								</strong>
						</span>
				</a>
		</p>
		<p>
				<a href="http://community.java.net/">
						<font color="#996699">java.net Communities</font>
				</a>
				<br />There are multiple "communities" here, with subject-specific forums and articles. For example, the Java Desktop community has all sorts of stuff related to Java language development for the desktop. The Java Patterns community might be of particular interest as a portal for pattern resources from a Java language perspective. There is also a community for Java User Groups (JUGs), where you can find information about creating, joining and running a JUG.<br /></p>
<img src ="http://www.blogjava.net/cisco/aggbug/95275.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/cisco/" target="_blank">Scott@JAVA</a> 2007-01-22 12:27 <a href="http://www.blogjava.net/cisco/archive/2007/01/22/95275.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Setup JDK on Linux</title><link>http://www.blogjava.net/cisco/archive/2006/06/15/52883.html</link><dc:creator>Scott@JAVA</dc:creator><author>Scott@JAVA</author><pubDate>Wed, 14 Jun 2006 20:01:00 GMT</pubDate><guid>http://www.blogjava.net/cisco/archive/2006/06/15/52883.html</guid><wfw:comment>http://www.blogjava.net/cisco/comments/52883.html</wfw:comment><comments>http://www.blogjava.net/cisco/archive/2006/06/15/52883.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/cisco/comments/commentRss/52883.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/cisco/services/trackbacks/52883.html</trackback:ping><description><![CDATA[My Linux distribution is <a href="http://www.linux-ren.org/modules/newbb/viewtopic.php?topic_id=218&amp;forum=21&amp;PHPSESSID=081a99048a7d2dfb3b7b31196451e00d">Everest 0.1</a><br /><br />1. Download JDK 6 Beta from <a href="http://java.sun.com/javase/6/download.jsp">SDN</a><br /><img alt="jdk_download.png" src="http://www.blogjava.net/images/blogjava_net/cisco/blog%20pics/jdk_download.png" border="0" height="102" width="616" /><br /><br />2. Assume you put <font color="#008000">jdk-6-beta-linux-i586.bin</font> in <font color="#008000">/usr/local<br /><font color="#000000"><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">[root@localhost local]# .</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">jdk</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">6</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">beta</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">linux</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">i586.bin</span></div></font><font color="#000000">After you agree the license, a folder named "jdk1.6.0" will be unzipped out<br /><br />3. Set environment variable to let Java function<br />Create a file named "java.sh" in <font color="#008000">/etc/profile.d</font><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">[root@localhost profile.d]# vi java.sh</span></div>The content of the file is as below<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">JAVA_HOME</span><span style="color: rgb(0, 0, 0);">=/</span><span style="color: rgb(0, 0, 0);">usr</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">local</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">jdk1.</span><span style="color: rgb(0, 0, 0);">6.0</span><span style="color: rgb(0, 0, 0);"><br />PATH</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">$PATH:$JAVA_HOME</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">bin<br />export JAVA_HOME PATH</span></div>Trigger by the following command (or you have to reboot)<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">[root@localhost profile.d]# source java.sh</span></div><br />4. Check whether you make it or not<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">[root@localhost </span><span style="color: rgb(0, 0, 0);">~</span><span style="color: rgb(0, 0, 0);">]# java </span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">version<br />java version </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">1.6.0-beta</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br />Java(TM) </span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);"> Runtime Environment, Standard Edition (build </span><span style="color: rgb(0, 0, 0);">1.6</span><span style="color: rgb(0, 0, 0);">.</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">beta</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">b59g)<br />Java HotSpot(TM) Client VM (build </span><span style="color: rgb(0, 0, 0);">1.6</span><span style="color: rgb(0, 0, 0);">.</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">beta</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">b59g, mixed mode, sharing)</span></div><br /></font></font><img src ="http://www.blogjava.net/cisco/aggbug/52883.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/cisco/" target="_blank">Scott@JAVA</a> 2006-06-15 04:01 <a href="http://www.blogjava.net/cisco/archive/2006/06/15/52883.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>SWT, Swing or AWT: Which is right for you?</title><link>http://www.blogjava.net/cisco/archive/2006/03/01/32949.html</link><dc:creator>Scott@JAVA</dc:creator><author>Scott@JAVA</author><pubDate>Tue, 28 Feb 2006 17:29:00 GMT</pubDate><guid>http://www.blogjava.net/cisco/archive/2006/03/01/32949.html</guid><wfw:comment>http://www.blogjava.net/cisco/comments/32949.html</wfw:comment><comments>http://www.blogjava.net/cisco/archive/2006/03/01/32949.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/cisco/comments/commentRss/32949.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/cisco/services/trackbacks/32949.html</trackback:ping><description><![CDATA[<P><A name=N10059><SPAN class=atitle><FONT face=Arial size=4>A look at AWT</FONT></SPAN></A></P>
<P>Abstract Windows Toolkit (AWT) is the original Java GUI tool kit. AWT's main advantages are that it comes standard with every version of Java technology, including Java implementations in old Web browsers, and it is very stable. This means you do not need to install it, you can depend on it being available everywhere you find a Java runtime environment, and it will have the features you expect.<BR><BR><BR><A name=N100CD><SPAN class=atitle><FONT face=Arial size=4>A look at Swing</FONT></SPAN></A></P>
<P>Java Swing, also known as a part of the <I>Java Foundation Classes</I> (JFC), was an attempt to solve most of AWT's shortcomings. In Swing, Sun created a very well-engineered, flexible, powerful GUI tool kit. Unfortunately, this means Swing takes time to learn, and it is sometimes too complex for common situations.<BR><BR><BR></P>
<P><A name=N10156><SPAN class=atitle><FONT face=Arial size=4>A look at SWT</FONT></SPAN></A></P>
<P>SWT is a low-level GUI tool kit comparable in concept to AWT. JFace is a set of enhanced components and utility services to make building GUIs with SWT easier. The builders of SWT learned from the AWT and Swing implementations and tried to build a system that had the advantages of both without their disadvantages. In many ways, they succeeded.<BR><BR><BR><A name=N10526><SPAN class=atitle><FONT face=Arial size=4>Conclusion</FONT></SPAN></A><BR></P>
<P>In most cases, the decision is between Swing and SWT combined with JFace. In general, each of these tool kits is complete and powerful enough to build full-function GUIs, but Swing is generally superior to SWT alone (used without JFace). Swing has the advantage of being built into Java technology, is completely portable, and arguably has a better architecture. Swing also has the advantage for advanced graphical applications. SWT has the advantage of being implemented as a native application which increases the performance and native compatibility of SWT-based GUIs.</P>
<P>If you are developing only for one platform, SWT has an advantage in host compatibility, including integration with host features, such as use of ActiveX controls under Windows.<BR><BR><BR><FONT color=#0000ff>The original (full version) article:</FONT> <A href="http://www-128.ibm.com/developerworks/opensource/library/os-swingswt/">http://www-128.ibm.com/developerworks/opensource/library/os-swingswt/</A></P><img src ="http://www.blogjava.net/cisco/aggbug/32949.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/cisco/" target="_blank">Scott@JAVA</a> 2006-03-01 01:29 <a href="http://www.blogjava.net/cisco/archive/2006/03/01/32949.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《Java 1.5 Tiger 》Study Notes</title><link>http://www.blogjava.net/cisco/archive/2005/12/27/25523.html</link><dc:creator>Scott@JAVA</dc:creator><author>Scott@JAVA</author><pubDate>Mon, 26 Dec 2005 20:27:00 GMT</pubDate><guid>http://www.blogjava.net/cisco/archive/2005/12/27/25523.html</guid><wfw:comment>http://www.blogjava.net/cisco/comments/25523.html</wfw:comment><comments>http://www.blogjava.net/cisco/archive/2005/12/27/25523.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/cisco/comments/commentRss/25523.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/cisco/services/trackbacks/25523.html</trackback:ping><description><![CDATA[<IMG src="http://www.oreilly.com/catalog/covers/javaadn.s.gif"><FONT color=#800080><FONT color=#000000><STRONG><BR><BR>Click on the chapter title to see details</STRONG></FONT><A href="/cisco/articles/25387.html"><BR><BR>Chapter 1. What's New?</A><BR><BR><FONT color=#008000>1.1 Working with Arrays</FONT><BR><FONT color=#008000>1.2 Using Queues</FONT><FONT color=#000000> </FONT><BR><FONT color=#008000>1.3 Ordering Queues Using Comparators</FONT><BR><FONT color=#008000>1.4 Overriding Return Types<BR>1.5 Taking Advantage of Better Unicode<BR>1.6 Adding StringBuilder to the Mix<FONT color=#000000>&nbsp;</FONT><BR><BR><FONT color=#800080><A href="/cisco/articles/25522.html">Chapter 2. Generics</A></FONT><FONT color=#000000><A href="/cisco/articles/25522.html"> <BR></A></FONT><BR>2.1 Using Type-Safe Lists<BR>2.2 Using Type-Safe Maps<BR>2.3 Iterating Over Parameterized Types<BR>2.4 Accepting Parameterized Types as Arguments<BR>2.5 Returning Parameterized Types<BR>2.6 Using Parameterized Types as Type Parameters<BR>2.7 Checking for Lint <BR>2.8 Generics and Type Conversions<BR>2.9 Using Type Wildcards<BR>2.10 Writing Generic Types<BR>2.11 Restricting Type Parameters<BR><BR></FONT></FONT><FONT color=#800080><A href="/cisco/articles/25975.html">Chapter 3. Enumerated Types<BR></A><BR><FONT color=#008000>3.1 Creating an Enum</FONT><BR></FONT><FONT color=#008000>3.2 Declaring Enums Inline<BR>3.3 Iterating Over Enums<BR>3.4 Switching on Enums<BR>3.5 Maps of Enums<BR>3.6 Sets of Enums<BR>3.7 Adding Methods to an Enum<BR>3.8 Implemening Interfaces with Enums<BR>3.9 Value-Specific Class Bodies<BR>3.10 Manually Defining an Enum<BR>3.11 Extending an Enum<BR><BR><A href="/cisco/articles/26211.html">Chapter 4. Autoboxing and Unboxing<BR></A><BR>4.1 Converting Primitives to Wrapper Types<BR>4.2 Coverting Wrapper Types to Primitives<BR>4.3 Incrementing and Decrementing Wrapper Types<BR>4.4 Boolean Versus boolean<BR>4.5 Conditionals and Unboxing<BR>4.6 Control Statements and Unboxing<BR>4.7 Method Overload Resolution</FONT><img src ="http://www.blogjava.net/cisco/aggbug/25523.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/cisco/" target="_blank">Scott@JAVA</a> 2005-12-27 04:27 <a href="http://www.blogjava.net/cisco/archive/2005/12/27/25523.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《Effective Java》Study Notes</title><link>http://www.blogjava.net/cisco/archive/2005/12/17/24305.html</link><dc:creator>Scott@JAVA</dc:creator><author>Scott@JAVA</author><pubDate>Fri, 16 Dec 2005 16:03:00 GMT</pubDate><guid>http://www.blogjava.net/cisco/archive/2005/12/17/24305.html</guid><wfw:comment>http://www.blogjava.net/cisco/comments/24305.html</wfw:comment><comments>http://www.blogjava.net/cisco/archive/2005/12/17/24305.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/cisco/comments/commentRss/24305.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/cisco/services/trackbacks/24305.html</trackback:ping><description><![CDATA[<IMG src="http://java.sun.com/docs/books/images/effective.jpg"><BR><FONT color=#800080><FONT color=#000000></FONT><FONT color=#000000><BR>Programming Language Guide<BR>by Joshua Bloch <BR>Foreword by Guy Steele<BR><BR><STRONG>Clike on&nbsp;the chapter title to see the notes, respectivelly.</STRONG></FONT><BR><BR><STRONG>Chapter 1. Introduction</STRONG><BR><BR></FONT>No compelling words, neglect.<BR><FONT color=#000000><BR><STRONG><A href="/cisco/articles/24444.html">Chapter 2. Creating and Destorying Objects</A></STRONG>&nbsp; 
<P><STRONG><FONT color=#ff0000>Item 1: Consider providing static factory methods instread of constructs</FONT></STRONG><FONT color=#000000><BR><STRONG><FONT color=#ff0000>Item 2: Enforce the singleton property with a private constructor<BR>Item 3: Enforce noninstantiability with a private constructor<BR>Item 4: Avoid creating duplicate objects<BR>Item 5: Eliminate obsolete object reference<BR>Item 6: Avoid finalizers<BR><BR><A href="/cisco/articles/24445.html">Chapter 3. Methods Common to All Objects<BR></A></FONT></STRONG><FONT color=#800080><STRONG><BR><FONT color=#ff0000>Item 7: Obey the general contract when overriding</FONT></STRONG></FONT><FONT color=#ff0000> equals<BR><STRONG>Item 8: Always override</STRONG> hashCode <STRONG>when you override</STRONG> equals<BR><STRONG>Item 9: Always override</STRONG> toString</FONT><BR><FONT color=#ff0000><STRONG>Item 10: Override</STRONG> clone <STRONG>judiciously</STRONG></FONT><BR><FONT color=#ff0000><STRONG>Item 11: Consider implementing</STRONG> Comparable</FONT><BR><BR><A href="/cisco/articles/24446.html"><STRONG>Chapter 4. Classes and Interfaces</STRONG></A><FONT color=#000000> </FONT><A href="/cisco/articles/24446.html"><BR></A><BR><STRONG><FONT color=#ff0000>Item 12: Minimize the accessibility of classes and members</FONT></STRONG><BR><STRONG><FONT color=#ff0000>Item 13: Favor immutability<BR>Item 14: Favor composition over inheritance</FONT></STRONG><BR><STRONG><FONT color=#ff0000>Item 15: Design and document for inheritance or else prohibit it</FONT></STRONG><BR><STRONG><FONT color=#ff0000>Item 16: Prefer interfaces to abstract classes</FONT></STRONG><BR><STRONG><FONT color=#ff0000>Item 17: Use interfaces only to define types</FONT></STRONG><BR><STRONG><FONT color=#ff0000>Item 18: Favor static member classes over nonstatic</FONT></STRONG><BR><BR></FONT><FONT color=#800080><STRONG>Chapter 5. Substitutes for C Constructs<BR></STRONG></FONT><BR>None of my business, neglect.<BR><BR><A href="/cisco/articles/24447.html"><STRONG>Chapter 6. Methods</STRONG></A><BR><BR><STRONG><FONT color=#ff0000>Item 23: Check parameters for validity</FONT></STRONG><BR><STRONG><FONT color=#ff0000>Item 24: Make defensive copies when needed</FONT></STRONG><BR><STRONG><FONT color=#ff0000>Item 25: Design method signatures carefully</FONT></STRONG><BR><STRONG><FONT color=#ff0000>Item 26: Use overloading judiciously<BR>Item 27: Return zero-length arrays, not nulls</FONT></STRONG><BR><STRONG><FONT color=#ff0000>Item 28: Write doc comments for all exposed API elements<BR></FONT></STRONG><BR><STRONG><A href="/cisco/articles/24448.html">Chapter 7. General Programming<BR></A></STRONG><BR><STRONG><FONT color=#ff0000>Item 29: Minimize the scope of local variables</FONT></STRONG><BR><STRONG><FONT color=#ff0000>Item 30: Know and use the libraries</FONT></STRONG><BR><STRONG><FONT color=#ff0000>Item 31: Avoid float and double if exact answers are required</FONT></STRONG><BR><STRONG><FONT color=#ff0000>Item 32: Avoid strings where other types are more appropriate</FONT></STRONG><BR><STRONG><FONT color=#ff0000>Item 33: Beware the performance of string concatenation</FONT></STRONG><BR><STRONG><FONT color=#ff0000>Item 34: Refer to objects by their interfaces</FONT></STRONG><BR><STRONG><FONT color=#ff0000>Item 35: Prefer interfaces to reflection<BR>Item 36: Use native methods judiciously</FONT></STRONG><BR><STRONG><FONT color=#ff0000>Item 37: Optimize judiciously<BR>Item 38: Adhere to generally accepted naming conventions</FONT></STRONG></FONT></P><img src ="http://www.blogjava.net/cisco/aggbug/24305.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/cisco/" target="_blank">Scott@JAVA</a> 2005-12-17 00:03 <a href="http://www.blogjava.net/cisco/archive/2005/12/17/24305.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java 的内存泄漏</title><link>http://www.blogjava.net/cisco/archive/2005/12/05/22645.html</link><dc:creator>Scott@JAVA</dc:creator><author>Scott@JAVA</author><pubDate>Mon, 05 Dec 2005 15:32:00 GMT</pubDate><guid>http://www.blogjava.net/cisco/archive/2005/12/05/22645.html</guid><wfw:comment>http://www.blogjava.net/cisco/comments/22645.html</wfw:comment><comments>http://www.blogjava.net/cisco/archive/2005/12/05/22645.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/cisco/comments/commentRss/22645.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/cisco/services/trackbacks/22645.html</trackback:ping><description><![CDATA[
		<p>欧阳辰(<a href="mailto:yeekee@sina.com">yeekee@sina.com</a>)周欣(<a href="mailto:zhouxin@sei.pku.edu.cn">zhouxin@sei.pku.edu.cn</a>)</p>
		<p>
				<b>
						<span class="atitle2">一 问题的提出</span>
				</b>
		</p>
		<p>Java的一个重要优点就是通过垃圾收集器(Garbage Collection，GC)自动管理内存的回收，程序员不需要通过调用函数来释放内存。因此，很多程序员认为Java不存在内存泄漏问题，或者认为即使有内存泄漏也不是程序的责任，而是GC或JVM的问题。其实，这种想法是不正确的，因为Java也存在内存泄露，但它的表现与C++不同。</p>
		<p>随着越来越多的服务器程序采用Java技术，例如JSP，Servlet， EJB等，服务器程序往往长期运行。另外，在很多嵌入式系统中，内存的总量非常有限。内存泄露问题也就变得十分关键，即使每次运行少量泄漏，长期运行之后，系统也是面临崩溃的危险。</p>
		<p>
				<b>
						<span class="atitle2">二 Java是如何管理内存</span>
				</b>
		</p>
		<p>为了判断Java中是否有内存泄露，我们首先必须了解Java是如何管理内存的。Java的内存管理就是对象的分配和释放问题。在Java中，程序员需要通过关键字new为每个对象申请内存空间 (基本类型除外)，所有的对象都在堆 (Heap)中分配空间。另外，对象的释放是由GC决定和执行的。在Java中，内存的分配是由程序完成的，而内存的释放是有GC完成的，这种收支两条线的方法确实简化了程序员的工作。但同时，它也加重了JVM的工作。这也是Java程序运行速度较慢的原因之一。因为，GC为了能够正确释放对象，GC必须监控每一个对象的运行状态，包括对象的申请、引用、被引用、赋值等，GC都需要进行监控。</p>
		<p>监视对象状态是为了更加准确地、及时地释放对象，而释放对象的根本原则就是该对象不再被引用。</p>
		<p>为了更好理解GC的工作原理，我们可以将对象考虑为有向图的顶点，将引用关系考虑为图的有向边，有向边从引用者指向被引对象。另外，每个线程对象可以作为一个图的起始顶点，例如大多程序从main进程开始执行，那么该图就是以main进程顶点开始的一棵根树。在这个有向图中，根顶点可达的对象都是有效对象，GC将不回收这些对象。如果某个对象 (连通子图)与这个根顶点不可达(注意，该图为有向图)，那么我们认为这个(这些)对象不再被引用，可以被GC回收。</p>
		<p>以下，我们举一个例子说明如何用有向图表示内存管理。对于程序的每一个时刻，我们都有一个有向图表示JVM的内存分配情况。以下右图，就是左边程序运行到第6行的示意图。</p>
		<p>
		</p>
		<center>
				<div class="image">
						<a href="http://www.mywelt.net/?q=node/174">
								<img height="201" alt="31" src="http://www.mywelt.net/?q=img_assist/gen/174" width="582" />
						</a>
						<div class="caption">
						</div>
				</div>
		</center>
		<p>
		</p>
		<p>Java使用有向图的方式进行内存管理，可以消除引用循环的问题，例如有三个对象，相互引用，只要它们和根进程不可达的，那么GC也是可以回收它们的。这种方式的优点是管理内存的精度很高，但是效率较低。另外一种常用的内存管理技术是使用计数器，例如COM模型采用计数器方式管理构件，它与有向图相比，精度行低(很难处理循环引用的问题)，但执行效率很高。</p>
		<p>
				<b>
						<span class="atitle2">三 什么是Java中的内存泄露</span>
				</b>
		</p>
		<p>下面，我们就可以描述什么是内存泄漏。在Java中，内存泄漏就是存在一些被分配的对象，这些对象有下面两个特点，首先，这些对象是可达的，即在有向图中，存在通路可以与其相连；其次，这些对象是无用的，即程序以后不会再使用这些对象。如果对象满足这两个条件，这些对象就可以判定为Java中的内存泄漏，这些对象不会被GC所回收，然而它却占用内存。</p>
		<p>在C++中，内存泄漏的范围更大一些。有些对象被分配了内存空间，然后却不可达，由于C++中没有GC，这些内存将永远收不回来。在Java中，这些不可达的对象都由GC负责回收，因此程序员不需要考虑这部分的内存泄露。</p>
		<p>通过分析，我们得知，对于C++，程序员需要自己管理边和顶点，而对于Java程序员只需要管理边就可以了(不需要管理顶点的释放)。通过这种方式，Java提高了编程的效率。</p>
		<p>
		</p>
		<center>
				<div class="image">
						<a href="http://www.mywelt.net/?q=node/175">
								<img height="231" alt="32" src="http://www.mywelt.net/?q=img_assist/gen/175" width="507" />
						</a>
						<div class="caption">
						</div>
				</div>
		</center>
		<p>
		</p>
		<p>因此，通过以上分析，我们知道在Java中也有内存泄漏，但范围比C++要小一些。因为Java从语言上保证，任何对象都是可达的，所有的不可达对象都由GC管理。</p>
		<p>对于程序员来说，GC基本是透明的，不可见的。虽然，我们只有几个函数可以访问GC，例如运行GC的函数System.gc()，但是根据Java语言规范定义， 该函数不保证JVM的垃圾收集器一定会执行。因为，不同的JVM实现者可能使用不同的算法管理GC。通常，GC的线程的优先级别较低。JVM调用GC的策略也有很多种，有的是内存使用到达一定程度时，GC才开始工作，也有定时执行的，有的是平缓执行GC，有的是中断式执行GC。但通常来说，我们不需要关心这些。除非在一些特定的场合，GC的执行影响应用程序的性能，例如对于基于Web的实时系统，如网络游戏等，用户不希望GC突然中断应用程序执行而进行垃圾回收，那么我们需要调整GC的参数，让GC能够通过平缓的方式释放内存，例如将垃圾回收分解为一系列的小步骤执行，Sun提供的HotSpot JVM就支持这一特性。</p>
		<p>下面给出了一个简单的内存泄露的例子。在这个例子中，我们循环申请Object对象，并将所申请的对象放入一个Vector中，如果我们仅仅释放引用本身，那么Vector仍然引用该对象，所以这个对象对GC来说是不可回收的。因此，如果对象加入到Vector后，还必须从Vector中删除，最简单的方法就是将Vector对象设置为null。</p>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
				<span style="COLOR: #000000">Vector v</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #0000ff">new</span>
				<span style="COLOR: #000000"> Vector(</span>
				<span style="COLOR: #000000">10</span>
				<span style="COLOR: #000000">);<br /><img id="Codehighlighter1_50_96_Open_Image" onclick="this.style.display='none'; Codehighlighter1_50_96_Open_Text.style.display='none'; Codehighlighter1_50_96_Closed_Image.style.display='inline'; Codehighlighter1_50_96_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img id="Codehighlighter1_50_96_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_50_96_Closed_Text.style.display='none'; Codehighlighter1_50_96_Open_Image.style.display='inline'; Codehighlighter1_50_96_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" /></span>
				<span style="COLOR: #0000ff">for</span>
				<span style="COLOR: #000000"> (</span>
				<span style="COLOR: #0000ff">int</span>
				<span style="COLOR: #000000"> i</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">1</span>
				<span style="COLOR: #000000">;i</span>
				<span style="COLOR: #000000">&lt;</span>
				<span style="COLOR: #000000">100</span>
				<span style="COLOR: #000000">; i</span>
				<span style="COLOR: #000000">++</span>
				<span style="COLOR: #000000">) </span>
				<span id="Codehighlighter1_50_96_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
						<img src="http://www.blogjava.net/images/dot.gif" />
				</span>
				<span id="Codehighlighter1_50_96_Open_Text">
						<span style="COLOR: #000000">{<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />    Object o</span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #0000ff">new</span>
						<span style="COLOR: #000000"> Object();<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />    v.add(o);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />    o</span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #0000ff">null</span>
						<span style="COLOR: #000000">;<br /><img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span>
				</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000"> 此时，所有的Object对象都没有被释放，因为变量v引用这些对象。</span>
		</div>
		<p>
				<b>
						<span class="atitle2">四 如何检测内存泄漏</span>
				</b>
		</p>
		<p>最后一个重要的问题，就是如何检测Java的内存泄漏。目前，我们通常使用一些工具来检查Java程序的内存泄漏问题。市场上已有几种专业检查Java内存泄漏的工具，它们的基本工作原理大同小异，都是通过监测Java程序运行时，所有对象的申请、释放等动作，将内存管理的所有信息进行统计、分析、可视化。开发人员将根据这些信息判断程序是否有内存泄漏问题。这些工具包括Optimizeit Profiler，JProbe Profiler，JinSight , Rational 公司的Purify等。</p>
		<p>下面，我们将简单介绍Optimizeit的基本功能和工作原理。</p>
		<p>Optimizeit Profiler版本4.11支持Application，Applet，Servlet和Romote Application四类应用，并且可以支持大多数类型的JVM，包括SUN JDK系列，IBM的JDK系列，和Jbuilder的JVM等。并且，该软件是由Java编写，因此它支持多种操作系统。Optimizeit系列还包括Thread Debugger和Code Coverage两个工具，分别用于监测运行时的线程状态和代码覆盖面。</p>
		<p>当设置好所有的参数了，我们就可以在OptimizeIt环境下运行被测程序，在程序运行过程中，Optimizeit可以监视内存的使用曲线(如下图)，包括JVM申请的堆(heap)的大小，和实际使用的内存大小。另外，在运行过程中，我们可以随时暂停程序的运行，甚至强行调用GC，让GC进行内存回收。通过内存使用曲线，我们可以整体了解程序使用内存的情况。这种监测对于长期运行的应用程序非常有必要，也很容易发现内存泄露。</p>
		<p>
		</p>
		<center>
				<div class="image">
						<a href="http://www.mywelt.net/?q=node/176">
								<img height="354" alt="33" src="http://www.mywelt.net/?q=img_assist/gen/176" width="521" />
						</a>
						<div class="caption">
						</div>
				</div>
		</center>
		<p>
		</p>
		<p>在运行过程中，我们还可以从不同视角观查内存的使用情况，Optimizeit提供了四种方式：</p>
		<ul>
				<li>堆视角。 这是一个全面的视角，我们可以了解堆中的所有的对象信息(数量和种类)，并进行统计、排序，过滤。了解相关对象的变化情况。 
</li>
				<li>方法视角。通过方法视角，我们可以得知每一种类的对象，都分配在哪些方法中，以及它们的数量。 
</li>
				<li>对象视角。给定一个对象，通过对象视角，我们可以显示它的所有出引用和入引用对象，我们可以了解这个对象的所有引用关系。 
</li>
				<li>引用图。 给定一个根，通过引用图，我们可以显示从该顶点出发的所有出引用。 </li>
		</ul>
		<p>在运行过程中，我们可以随时观察内存的使用情况，通过这种方式，我们可以很快找到那些长期不被释放，并且不再使用的对象。我们通过检查这些对象的生存周期，确认其是否为内存泄露。在实践当中，寻找内存泄露是一件非常麻烦的事情，它需要程序员对整个程序的代码比较清楚，并且需要丰富的调试经验，但是这个过程对于很多关键的Java程序都是十分重要的。</p>
		<p>综上所述，Java也存在内存泄露问题，其原因主要是一些对象虽然不再被使用，但它们仍然被引用。为了解决这些问题，我们可以通过软件工具来检查内存泄露，检查的主要原理就是暴露出所有堆中的对象，让程序员寻找那些无用但仍被引用的对象。<br /><br /><font color="#008000">Further reading:</font><a href="http://java.sun.com/docs/books/performance/1st_edition/html/JPAppGC.fm.html">The Truth About Garbage Collection</a></p>
<img src ="http://www.blogjava.net/cisco/aggbug/22645.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/cisco/" target="_blank">Scott@JAVA</a> 2005-12-05 23:32 <a href="http://www.blogjava.net/cisco/archive/2005/12/05/22645.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>JDK5.0的11个主要新特征</title><link>http://www.blogjava.net/cisco/archive/2005/12/04/22452.html</link><dc:creator>Scott@JAVA</dc:creator><author>Scott@JAVA</author><pubDate>Sun, 04 Dec 2005 10:12:00 GMT</pubDate><guid>http://www.blogjava.net/cisco/archive/2005/12/04/22452.html</guid><wfw:comment>http://www.blogjava.net/cisco/comments/22452.html</wfw:comment><comments>http://www.blogjava.net/cisco/archive/2005/12/04/22452.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/cisco/comments/commentRss/22452.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/cisco/services/trackbacks/22452.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 摘至 落雪的梦想1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 泛型(Generic)1.1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 说明增强了java的类型安全，可以在编译期间对容器内的对象进行类型检查，在运行期不必进行类型的转换。而在j2s...&nbsp;&nbsp;<a href='http://www.blogjava.net/cisco/archive/2005/12/04/22452.html'>阅读全文</a><img src ="http://www.blogjava.net/cisco/aggbug/22452.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/cisco/" target="_blank">Scott@JAVA</a> 2005-12-04 18:12 <a href="http://www.blogjava.net/cisco/archive/2005/12/04/22452.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>heap与stack的研究</title><link>http://www.blogjava.net/cisco/archive/2005/12/04/22451.html</link><dc:creator>Scott@JAVA</dc:creator><author>Scott@JAVA</author><pubDate>Sun, 04 Dec 2005 09:55:00 GMT</pubDate><guid>http://www.blogjava.net/cisco/archive/2005/12/04/22451.html</guid><wfw:comment>http://www.blogjava.net/cisco/comments/22451.html</wfw:comment><comments>http://www.blogjava.net/cisco/archive/2005/12/04/22451.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/cisco/comments/commentRss/22451.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/cisco/services/trackbacks/22451.html</trackback:ping><description><![CDATA[<P>摘至 <A accessKey=1 href="http://www.matrix.org.cn/blog/cleverpig/">聪明猪的blog</A><BR><BR></P>
<P>Thinking in java第四章的内容是关于内存分配和初始化的,对这一章的学习带出了我以往学习中的一个模糊点:究竟什么是堆存储(Heap)?什么是栈存储(Stack)?有什么区别呢?翻了不少资料,补了这一课,觉得非常受用.</P>
<P><BR>2.1 内存分配策略<BR>按照编译原理的观点,程序运行时的内存分配有三种策略,分别是静态的,栈式的,和堆式的.<BR>静态存储分配是指在编译时就能确定每个数据目标在运行时刻的存储空间需求,因而在编译时就可以给他们分配固定的内存空间.这种分配策略要求程序代码中不允许有可变数据结构(比如可变数组)的存在,也不允许有嵌套或者递归的结构出现,因为它们都会导致编译程序无法计算准确的存储空间需求.<BR>栈式存储分配也可称为动态存储分配,是由一个类似于堆栈的运行栈来实现的.和静态存储分配相反,在栈式存储方案中,程序对数据区的需求在编译时是完全未知的,只有到运行的时候才能够知道,但是规定在运行中进入一个程序模块时,必须知道该程序模块所需的数据区大小才能够为其分配内存.和我们在数据结构所熟知的栈一样,栈式存储分配按照先进后出的原则进行分配。<BR>静态存储分配要求在编译时能知道所有变量的存储要求,栈式存储分配要求在过程的入口处必须知道所有的存储要求,而堆式存储分配则专门负责在编译时或运行时模块入口处都无法确定存储要求的数据结构的内存分配,比如可变长度串和对象实例.堆由大片的可利用块或空闲块组成,堆中的内存可以按照任意顺序分配和释放.</P>
<P>2.2 堆和栈的比较<BR>上面的定义从编译原理的教材中总结而来,除静态存储分配之外,都显得很呆板和难以理解,下面撇开静态存储分配,集中比较堆和栈:<BR>从堆和栈的功能和作用来通俗的比较,堆主要用来存放对象的，栈主要是用来执行程序的.而这种不同又主要是由于堆和栈的特点决定的:<BR>在编程中，例如C/C++中，所有的方法调用都是通过栈来进行的,所有的局部变量,形式参数都是从栈中分配内存空间的。实际上也不是什么分配,只是从栈顶向上用就行,就好像工厂中的传送带(conveyor belt)一样,Stack Pointer会自动指引你到放东西的位置,你所要做的只是把东西放下来就行.退出函数的时候，修改栈指针就可以把栈中的内容销毁.这样的模式速度最快,当然要用来运行程序了.需要注意的是,在分配的时候,比如为一个即将要调用的程序模块分配数据区时,应事先知道这个数据区的大小,也就说是虽然分配是在程序运行时进行的,但是分配的大小多少是确定的,不变的,而这个"大小多少"是在编译时确定的,不是在运行时.<BR>堆是应用程序在运行的时候请求操作系统分配给自己内存，由于从操作系统管理的内存分配,所以在分配和销毁时都要占用时间，因此用堆的效率非常低.但是堆的优点在于,编译器不必知道要从堆里分配多少存储空间，也不必知道存储的数据要在堆里停留多长的时间,因此,用堆保存数据时会得到更大的灵活性。事实上,面向对象的多态性,堆内存分配是必不可少的,因为多态变量所需的存储空间只有在运行时创建了对象之后才能确定.在C++中，要求创建一个对象时，只需用new命令编制相关的代码即可。执行这些代码时，会在堆里自动进行数据的保存.当然，为达到这种灵活性，必然会付出一定的代价:在堆里分配存储空间时会花掉更长的时间！这也正是导致我们刚才所说的效率低的原因,看来列宁同志说的好,人的优点往往也是人的缺点,人的缺点往往也是人的优点(晕~).</P>
<P><BR>2.3 JVM中的堆和栈 <BR>JVM是基于堆栈的虚拟机.JVM为每个新创建的线程都分配一个堆栈.也就是说,对于一个Java程序来说，它的运行就是通过对堆栈的操作来完成的。堆栈以帧为单位保存线程的状态。JVM对堆栈只进行两种操作:以帧为单位的压栈和出栈操作。<BR>我们知道,某个线程正在执行的方法称为此线程的当前方法.我们可能不知道,当前方法使用的帧称为当前帧。当线程激活一个Java方法,JVM就会在线程的Java堆栈里新压入一个帧。这个帧自然成为了当前帧.在此方法执行期间,这个帧将用来保存参数,局部变量,中间计算过程和其他数据.这个帧在这里和编译原理中的活动纪录的概念是差不多的.<BR>从Java的这种分配机制来看,堆栈又可以这样理解:堆栈(Stack)是操作系统在建立某个进程时或者线程(在支持多线程的操作系统中是线程)为这个线程建立的存储区域，该区域具有先进后出的特性。<BR>每一个Java应用都唯一对应一个JVM实例，每一个实例唯一对应一个堆。应用程序在运行中所创建的所有类实例或数组都放在这个堆中,并由应用所有的线程共享.跟C/C++不同，Java中分配堆内存是自动初始化的。Java中所有对象的存储空间都是在堆中分配的，但是这个对象的引用却是在堆栈中分配,也就是说在建立一个对象时从两个地方都分配内存，在堆中分配的内存实际建立这个对象，而在堆栈中分配的内存只是一个指向这个堆对象的指针(引用)而已。</P>
<P><BR>2.4 GC的思考<BR>Java为什么慢?JVM的存在当然是一个原因,但有人说,在Java中,除了简单类型(int,char等)的数据结构,其它都是在堆中分配内存(所以说Java的一切都是对象)，这也是程序慢的原因之一。<BR>我的想法是(应该说代表TIJ的观点),如果没有Garbage Collector(GC),上面的说法就是成立的.堆不象栈是连续的空间,没有办法指望堆本身的内存分配能够象堆栈一样拥有传送带般的速度,因为,谁会为你整理庞大的堆空间,让你几乎没有延迟的从堆中获取新的空间呢?<BR>这个时候,GC站出来解决问题.我们都知道GC用来清除内存垃圾,为堆腾出空间供程序使用,但GC同时也担负了另外一个重要的任务,就是要让Java中堆的内存分配和其他语言中堆栈的内存分配一样快,因为速度的问题几乎是众口一词的对Java的诟病.要达到这样的目的,就必须使堆的分配也能够做到象传送带一样,不用自己操心去找空闲空间.这样,GC除了负责清除Garbage外,还要负责整理堆中的对象,把它们转移到一个远离Garbage的纯净空间中无间隔的排列起来,就象堆栈中一样紧凑,这样Heap Pointer就可以方便的指向传送带的起始位置,或者说一个未使用的空间,为下一个需要分配内存的对象"指引方向".因此可以这样说,垃圾收集影响了对象的创建速度,听起来很怪,对不对?<BR>那GC怎样在堆中找到所有存活的对象呢?前面说了,在建立一个对象时，在堆中分配实际建立这个对象的内存,而在堆栈中分配一个指向这个堆对象的指针(引用),那么只要在堆栈(也有可能在静态存储区)找到这个引用,就可以跟踪到所有存活的对象.找到之后,GC将它们从一个堆的块中移到另外一个堆的块中,并将它们一个挨一个的排列起来,就象我们上面说的那样,模拟出了一个栈的结构,但又不是先进后出的分配,而是可以任意分配的,在速度可以保证的情况下,Isn't it great?<BR>但是，列宁同志说了,人的优点往往也是人的缺点,人的缺点往往也是人的优点(再晕~~).GC()的运行要占用一个线程,这本身就是一个降低程序运行性能的缺陷,更何况这个线程还要在堆中把内存翻来覆去的折腾.不仅如此,如上面所说,堆中存活的对象被搬移了位置,那么所有对这些对象的引用都要重新赋值.这些开销都会导致性能的降低.<BR>此消彼长,GC()的优点带来的效益是否盖过了它的缺点导致的损失,我也没有太多的体会,Bruce Eckel 是Java的支持者，王婆卖瓜，话不能全信.个人总的感觉是,Java还是很慢,它的发展还需要时间.<BR></P>
<P>上面的体会是我看了TIJ.3rdEdition.Revision4.0中第四章之后得出的,内容和前面的有些不同.我没有看过侯捷的中文版本,但我觉得,在关键问题上,原版的TIJ的确更值得一读.所以和中文版配合起来学习是比较不错的选择.<BR>我只能算一个Java的初学者,没想到起了这么个题目,却受到这么多人的关注,欣喜之余,也决心尽力写好下面的每一篇.不过这一篇完了,我就该准备赴美签证了,如果成功,那就要等到8月27号CS的研究生院开学之后,才有时间会开始研究下一章了,希望可以多从原版中获取一点经验.</P><img src ="http://www.blogjava.net/cisco/aggbug/22451.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/cisco/" target="_blank">Scott@JAVA</a> 2005-12-04 17:55 <a href="http://www.blogjava.net/cisco/archive/2005/12/04/22451.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>