??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲色图欧美,亚洲精品wwww,日韩理论片avhttp://www.aygfsteel.com/wangjc0801/category/37128.html从头看Javazh-cnFri, 23 Jan 2009 03:19:46 GMTFri, 23 Jan 2009 03:19:46 GMT60Java集合http://www.aygfsteel.com/wangjc0801/archive/2009/01/21/252278.htmlwangjcwangjcWed, 21 Jan 2009 15:31:00 GMThttp://www.aygfsteel.com/wangjc0801/archive/2009/01/21/252278.htmlhttp://www.aygfsteel.com/wangjc0801/comments/252278.htmlhttp://www.aygfsteel.com/wangjc0801/archive/2009/01/21/252278.html#Feedback0http://www.aygfsteel.com/wangjc0801/comments/commentRss/252278.htmlhttp://www.aygfsteel.com/wangjc0801/services/trackbacks/252278.html1. java集合cȝ于存放数量不{的多个对象

2. Java集合可分Zc? Set, List, Map.其中Set代表无序?不可重复的集?List代表有序?可重复的集合;Map代表h映射关系的集?

3. java1.5之前,java集合?x)丢失集合中对象的数据类?把所有对象当成Objectcd处理.

4. java集合主要׃个接口派生而出:Collection和Map

=================================================================

 

1. Collection接口

    Iterator接口隐藏了各UConllection实现cȝ底层l节,向应用程序提供了遍历Conllection集合的统一~程接口.Iterator仅用于遍历集?且必M附于Collection对象.

    Iterator接口采用快速失败机?一旦在q代的时候检到该集合已l被修改,通常是程序中的另外的U程,E序立即引发异常.

    代码清单:CollectionTest.java

package Test.test;
 
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
 
public class CollectionTest {
 
    public static void main(String[] args) {
        
        Collection books = new HashSet();
        
        books.add("Java基础");
        books.add("J2EE基础");
        books.add("J2EE高");
        
        System.out.println(books);//Collection的实现类重写了toString()Ҏ(gu)
        Iterator it = books.iterator();
        while (it.hasNext()) {
            String book = (String)it.next();
            System.out.println("book:"+book);
            if ("J2EE基础".equals(book)) {
                //it.remove();//removeҎ(gu)删除上一ơnextҎ(gu)q回的元?/span>
                
                books.remove(book);
                // 在Iterator q代books?另一个线E去修改books元素,则会(x)引发java.util.ConcurrentModificationException
                // 异常,而同L(fng)如果要删除的是最后一个对?׃?x)发生异?
            }
        }
        
        System.out.println(books);
 
    }
 
}



wangjc 2009-01-21 23:31 发表评论
]]>
Javac?/title><link>http://www.aygfsteel.com/wangjc0801/archive/2009/01/14/251359.html</link><dc:creator>wangjc</dc:creator><author>wangjc</author><pubDate>Wed, 14 Jan 2009 15:01:00 GMT</pubDate><guid>http://www.aygfsteel.com/wangjc0801/archive/2009/01/14/251359.html</guid><wfw:comment>http://www.aygfsteel.com/wangjc0801/comments/251359.html</wfw:comment><comments>http://www.aygfsteel.com/wangjc0801/archive/2009/01/14/251359.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/wangjc0801/comments/commentRss/251359.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/wangjc0801/services/trackbacks/251359.html</trackback:ping><description><![CDATA[<p><font color="#0000ff" size="2"></font></p> <p><font color="#0000ff" size="2">1. cd对象</font> <p><font color="#0000ff" size="2">   </font> <p><font color="#0000ff" size="2">     cd以认为是自定义的数据cd,.cȝ于描q某一cd象的共同特征,对象是类的具体存?</font> <p><font color="#0000ff" size="2"></font>  <p><font color="#0000ff" size="2">java中的对象?qing)其属性数据是存放在堆(heap)内存中的,而引用变量则是存攑֜栈内存中?</font> <p><font color="#0000ff" size="2">    Person p = new Person();</font> <p><font color="#0000ff" size="2">    pȝ?x)生成两个实?一个是引用变量p,一个是Person对象.栈内存中的引用变量p指向堆内存中的Person对象,变量p只是存储了一个地址?本n不包含Q何实际数?堆内存中的数据不允许直接讉K,只能通过该对象的引用去访问该对象.</font> <p><font color="#0000ff" size="2">    Person p1 = p;</font> <p><font color="#0000ff" size="2">    p变量赋给p1,也就是将p变量保存的地址Dlp1.q样p1也指向了堆内存中的同一个对?q样不管是通过pq是p1讉K对象的属性和Ҏ(gu),l果都是讉K同一个对象的属性和Ҏ(gu),q回的结果都是一L(fng).也就是说堆内存中的对象可以有多个引用.</font> <p><font color="#0000ff" size="2">    p = null;</font> <p><font color="#0000ff" size="2">    当堆内存中的某个对象没有引用执行它时,E序无法再讉K到这个对?q个对象也就是无用的?垃圾回收机制回收这个对?释放该对象所占用的内存空?因此,如果需要垃圑֛收机制回收某个对?只要切断该对象的所有引用即?也就是将相关的引用赋为null.</font> <p><font color="#0000ff" size="2">内存C意?</font></p> <p></p> <div id="wmqeeuq" class="wlWriterEditableSmartContent" id="scid:51CF81A4-8F44-4a2c-8837-198C090B9994:2ae6a00c-fdb8-4900-a7a7-40742adc1c42" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"><p><a atomicselection="true"><img style="border-right: 2px; border-top: 2px; border-left: 2px; border-bottom: 2px" height="233" src="http://lh6.ggpht.com/_L3nkUmUbBp0/SWQEPuAD9wI/AAAAAAAAACs/1V-hgK2Rwsk/s400/%E5%AF%B9%E8%B1%A1.jpg" width="400"></a></p></div> <p></p> <p><font color="#0000ff" size="2">2. 面向对象的特?/font></p> <p><font color="#0000ff" size="2">    1. 装</font></p> <p><font color="#0000ff" size="2">     对象的状态信息隐藏在对象内部,不允许外部程序直接访问对象内部信?而是通过该类所提供的方法来实现对内部信息的操作和访?cM客观世界中的属性都是隐藏在对象内部?外部无法直接操作和修?良好的封装是通过对外暴露_的方法来操作和访问类的内部信?</font></p> <p><font color="#0000ff" size="2">  讉K控制U别从private到public逐次变大</font></p> <p></p> <div id="wmqeeuq" class="wlWriterEditableSmartContent" id="scid:51CF81A4-8F44-4a2c-8837-198C090B9994:96ec8c0b-e73c-4c44-89aa-65685fb2077a" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"><p><a atomicselection="true"><img style="border-right: 2px; border-top: 2px; border-left: 2px; border-bottom: 2px" height="27" src="http://lh4.ggpht.com/_L3nkUmUbBp0/SWyr2xk84WI/AAAAAAAAAKg/ux9ikE68j6Q/s400/java%E8%AE%BF%E9%97%AE%E6%8E%A7%E5%88%B6%E7%AC%A6.jpg" width="400"></a></p></div> <p></p> <p></p> <div id="wmqeeuq" class="wlWriterEditableSmartContent" id="scid:51CF81A4-8F44-4a2c-8837-198C090B9994:3f9deb35-f919-48c7-9f46-55b81a7b0f4c" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"><p><a atomicselection="true"><img style="border-right: 2px; border-top: 2px; border-left: 2px; border-bottom: 2px" height="339" src="http://lh4.ggpht.com/_L3nkUmUbBp0/SWyt17JMSFI/AAAAAAAAAKk/4EzuPZQ7G24/s400/%E8%AE%BF%E9%97%AE%E6%8E%A7%E5%88%B6%E7%AC%A6%E7%BA%A7%E5%88%AB%E8%A1%A8.jpg" width="326"></a></p></div> <p></p> <p><font color="#0000ff" size="2"></font> </p> <p><font color="#0000ff" size="2">    2. l承</font></p> <p><font color="#0000ff" size="2">       1. java不支持多重?每个子类只有一个直接父c?/font></p> <p><font color="#0000ff" size="2">       2. ObjectcL一切类的父c?/font></p> <p><font color="#0000ff" size="2">       3. 子类l承不能l承父类的私有的成员变量和成员方?</font></p> <p><font color="#0000ff" size="2">           在抽象类中定义了U有的成员方法或变量,子类不能讉K到父cȝU有成员,所以子cM重写父类的私有方?实际上是新徏了一个方?在接口定义中的成员是不能为私有的,否则~译?x)报?因ؓ(f)接口是用来公开?U有的变量和Ҏ(gu)不能被承或实现.</font></p> <p><font color="#0000ff" size="2">       4. 覆盖和被覆盖的方法必d是实例方法或同时cL?/font></p> <p><font color="#0000ff" size="2">       5. 子类中的变量可以覆盖父类中同名的变量,但ƈ不是覆盖,而只是屏?父类的变量ƈ未改?/font></p> <p><font color="#0000ff" size="2">       6. 调用子类构造方法之?pȝ?x)先调用父类的构造方?如果有多个父c?则从上到下依ơ调?/font></p> <p><font color="#0000ff" size="2">       7. 内存机制?父类和子cL占用同一块内存空间的,只是子类在父cȝ基础上增加自q属性和Ҏ(gu).</font></p> <p><font color="#0000ff" size="2">       8. 在子cM可通过super讉K父类中的成员.</font></p> <p><font color="#0000ff" size="2">       9. pȝ创徏某个cȝ对象?pȝM(x)隐式地创cȝcȝ对象.在子cL法中,super指向该方法调用者的子类对象的父cd?</font></p> <p><font color="#0000ff" size="2">代码清单: l承</font></p> <div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; max-height: 200px"> <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">abstract</span> <span style="color: #0000ff">class</span> A { </pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> <span style="color: #0000ff">int</span> i=1; </pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> printI() { </pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> System.<span style="color: #0000ff">out</span>.println(<span style="color: #006080">"i="</span>+i); </pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> } </pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">} </pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> </pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> B extends A{ </pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> <span style="color: #0000ff">int</span> i=2; </pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> printI() {</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> super.printI();</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> }</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> main(String[] args) { </pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> B b=<span style="color: #0000ff">new</span> B(); </pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> b.printI(); <span style="color: #008000">//i=1;</span></pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> } </pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> } </pre></div></div> <p> <font color="#0000ff" size="2">3. 多?/font></p> <p><font color="#0000ff" size="2">     1. 相同cd的变?执行同一个方法时呈现Z同的行ؓ(f)特征,是多?多态发生在~译时类型和q行时类型不一致时.</font></p> <p><font color="#0000ff" size="2">     2. 引用cd在编译阶D只能调用其~译时类型所h的方?q行时则执行它运行时cd所h的方?/font></p> <p><font color="#0000ff" size="2">     3. 引用变量只能调用声明该变量的所用的cd含的Ҏ(gu),通过引用变量只能讉K到编译时cL定义的属?</font></p> <p><font color="#0000ff" size="2">     例如 BaseClass bsc = new SubClass()通过变量bsc只能调用BaseClass所定义的方?而不能访问sub(),所以bsc.sub()?x)编译不通过.可以通过bsc讉Ktest(),但运行时调用的是SubClasscȝҎ(gu).通过bsc讉K的属性book是BaseClass的属?</font></p> <div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; max-height: 200px"> <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> </pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">class</span> BaseClass {</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> </pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> book = 1;</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> </pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> <span style="color: #0000ff">base</span>() {</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> System.<span style="color: #0000ff">out</span>.println(<span style="color: #006080">"父类的普通方?</span>);</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> }</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> </pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> test() {</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> System.<span style="color: #0000ff">out</span>.println(<span style="color: #006080">"父类被覆盖的Ҏ(gu)"</span>);</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> }</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> </pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> </pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> SubClass extends BaseClass{</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> </pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> <span style="color: #0000ff">public</span> String book = <span style="color: #006080">"JAVA"</span>;</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> </pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> test() {</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> System.<span style="color: #0000ff">out</span>.println(<span style="color: #006080">"子类覆盖父类的方?</span>);</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> }</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> </pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> sub() {</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> System.<span style="color: #0000ff">out</span>.println(<span style="color: #006080">"子类的普通方?</span>);</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> }</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> </pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> main(String[] args) {</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> </pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> System.<span style="color: #0000ff">out</span>.println(<span style="color: #006080">"=======BaseClass bc = new BaseClass();======="</span>);</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> BaseClass bc = <span style="color: #0000ff">new</span> BaseClass();</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> System.<span style="color: #0000ff">out</span>.println(bc.book);</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> bc.test();</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> bc.<span style="color: #0000ff">base</span>();</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> </pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> System.<span style="color: #0000ff">out</span>.println(<span style="color: #006080">"=======SubClass sc = new SubClass();======="</span>);</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> SubClass sc = <span style="color: #0000ff">new</span> SubClass();</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> System.<span style="color: #0000ff">out</span>.println(sc.book);</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> sc.test();</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> sc.<span style="color: #0000ff">base</span>();</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> </pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> System.<span style="color: #0000ff">out</span>.println(<span style="color: #006080">"=======BaseClass bsc = new SubClass();======="</span>);</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> </pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> BaseClass bsc = <span style="color: #0000ff">new</span> SubClass();<span style="color: #008000">// ~译时类型和q行时类型不一致时,发生多?/span></pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> System.<span style="color: #0000ff">out</span>.println(bsc.book);<span style="color: #008000">//打印:1,对象的属性不h多态?只能讉K~译时类型中的属?/span></pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> bsc.test();<span style="color: #008000">//子类覆盖父类的方?q行时调用的是运行时cd所定义的方?</span></pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> bsc.<span style="color: #0000ff">base</span>();<span style="color: #008000">//父类的普通方?/span></pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> <span style="color: #008000">//bsc.sub();// 只能讉K~译时类型中所定义的方?/span></pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> </pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> }</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> </pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre></div></div><font color="#0000ff" size="2"></font><img src ="http://www.aygfsteel.com/wangjc0801/aggbug/251359.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/wangjc0801/" target="_blank">wangjc</a> 2009-01-14 23:01 <a href="http://www.aygfsteel.com/wangjc0801/archive/2009/01/14/251359.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>数组cdhttp://www.aygfsteel.com/wangjc0801/archive/2009/01/12/251036.htmlwangjcwangjcMon, 12 Jan 2009 15:07:00 GMThttp://www.aygfsteel.com/wangjc0801/archive/2009/01/12/251036.htmlhttp://www.aygfsteel.com/wangjc0801/comments/251036.htmlhttp://www.aygfsteel.com/wangjc0801/archive/2009/01/12/251036.html#Feedback0http://www.aygfsteel.com/wangjc0801/comments/commentRss/251036.htmlhttp://www.aygfsteel.com/wangjc0801/services/trackbacks/251036.html

 

1. 定义

    1. 同一个数l中只能存储一U数据类型的数据

    2. 存储的数据类型可以是基本数据cd,也可以是引用数据cd

    3. 数组也是一U类?是一U引用数据类?所以数l在定义的时候ƈ未分配内?

2. 初始?/font>

    1. 静态初始化

        q序员昄指定每个数组元素的初始?ql决定需要的数组长度

    2. 动态初始化

        ql程序员指定数组长度,ql按照数据类型指定数l元素初始?

    数组的初始化q程,是为数l的数组元素分配内存I间的过E?静态初始化和动态初始化不能同时q行,

也就是不能即指定数组长度,又ؓ(f)数组元素指定初始?

    如果数组是引用类型的,每个数组元素中的数据是引用数据cd,q时的引用数据类型是保存在堆内存?/font>

?而每个引用数据类型又指向另一块堆内存I间,存储有效的数?

3. 内存中的数组

int[] arrayA = new int[3];

    如上,arrayA只是一个引用变?和创建类时一?引用变量攑֜栈内存中.只有在new?才会(x)在堆内存中ؓ(f)对象分配内存I间.当将一个数l变量a,赋给数组变量b?卛_引用变量b也执行堆内存中a变量指向的对象实?q样两个变量a和b都指向了同一个数l对?而第二个数组则变成了垃圾,ql垃圑֛收机制在适当的时候回收这个对象所占用的内?

    代码清单: 数组的定义和初始?/font>

 
public class ArrayTest {
    
    public static void main(String[] args) {
        
        int[] a = {1,2,3};//静态初始化
        int[] b = new int[4];//动态初始化
        
        System.out.println("数组a的长?"+a.length);
        for (int i=0;i<a.length;i++) {
            System.out.println("a["+i+"]="+a[i]);
        }
        
        System.out.println("数组b的长?"+b.length);
        for (int i=0;i<b.length;i++) {
            System.out.println("b["+i+"]="+b[i]);
        }
        
        b = a;
        System.out.println("数组b的长?"+b.length);
        
    }
 
    /*
    result:
    数组a的长?3
    a[0]=1
    a[1]=2
    a[2]=3
    数组b的长?4
    b[0]=0
    b[1]=0
    b[2]=0
    b[3]=0
    数组b的长?3
    */
}

 

4. 多维数组

    java中的多维数组实质上还是一l数l?比如二维数组,数组元素也是引用,而数l元素中的引用指向一l?/font>

数组.

    代码清单: 多维数组

 
import java.util.Arrays;
 // ArrayscM包含常用的方?包括复制数组,排序,查找,Ҏ(gu),填充{功?/pre>
public class ArrayTest2 {
 
    public static void main(String[] args) 
    {
        int[][] a = new int[3][2];//定义一个二l数la
        
        // 首先a是一个长度ؓ(f)3的一l数l?/span>
        for (int i = 0; i < a.length ; i++ )
        {
            System.out.println(Arrays.toString(a[i]));//每个数组元素都是一个引用一l数l的引用变量
        }
        
        a[0][0] = 1;
        a[0][1] = 2;
        //在这个一l数l中的元素又是一个一l数l?/span>
        System.out.println("二维数组的元素是一l数l?");
        for (int j = 0; j < 2; j++) {
            System.out.println("a[0][" + j + "]=" + a[0][j]);
        }
        
        System.out.println("重新赋值后的二l数l?");
        for (int i = 0; i < a.length ; i++ )
        {
            System.out.println(Arrays.toString(a[i]));
        }
 
        // 二维数组的遍?/span>
        System.out.println("二维数组的遍?");
        for (int i=0;i<3;i++) {
            for (int j=0;j<2;j++) {
                System.out.println("a["+i+"]["+j+"]="+a[i][j]);
            }
        }
    }
    /*
    [0, 0]
    [0, 0]
    [0, 0]
    二维数组的元素是一l数l?
    a[0][0]=1
    a[0][1]=2
    重新赋值后的二l数l?
    [1, 2]
    [0, 0]
    [0, 0]
    二维数组的遍?
    a[0][0]=1
    a[0][1]=2
    a[1][0]=0
    a[1][1]=0
    a[2][0]=0
    a[2][1]=0
    */
    
}


wangjc 2009-01-12 23:07 发表评论
]]>
java程控制http://www.aygfsteel.com/wangjc0801/archive/2009/01/12/250910.htmlwangjcwangjcSun, 11 Jan 2009 16:17:00 GMThttp://www.aygfsteel.com/wangjc0801/archive/2009/01/12/250910.htmlhttp://www.aygfsteel.com/wangjc0801/comments/250910.htmlhttp://www.aygfsteel.com/wangjc0801/archive/2009/01/12/250910.html#Feedback0http://www.aygfsteel.com/wangjc0801/comments/commentRss/250910.htmlhttp://www.aygfsteel.com/wangjc0801/services/trackbacks/250910.html

 

1. 条g语句后的语句,如果只有一?可以省略花括?q可以是一个空语句,也就是只有一个分?/font>

2. switch语句中每个case语句中应该已breakl尾,否则?x)执行完后面的所有case

3. break和continue语句用来控制循环l构.break用来l束整个循环?continue用来l束当前循环.在嵌套@环中,二者都可以配合标签来结束标{所标识的@?

   E序清单: l束标识的@?/font>

 
public class BreakTest {
 
    public static void main(String[] args) {
        
        outFor://标记外部的@?/span>
        for (int i=0;i<3;i++) {
            //内部循环
            for (int j=0;j<3;j++) {
                
                System.out.println("i="+i+",j="+j);
                
                if (j==1) {
                    
                    break outFor; //跛_外部循环
                    /* result:
                    i=0,j=0
                    i=0,j=1
                    */
                    //continue outFor;
                    /* result:
                    i=0,j=0
                    i=0,j=1
                    i=1,j=0
                    i=1,j=1
                    i=2,j=0
                    i=2,j=1
                    */
                    
                }
            }
            
        }
 
    }
 
}


wangjc 2009-01-12 00:17 发表评论
]]>
javaq算W?/title><link>http://www.aygfsteel.com/wangjc0801/archive/2009/01/11/250905.html</link><dc:creator>wangjc</dc:creator><author>wangjc</author><pubDate>Sun, 11 Jan 2009 15:41:00 GMT</pubDate><guid>http://www.aygfsteel.com/wangjc0801/archive/2009/01/11/250905.html</guid><wfw:comment>http://www.aygfsteel.com/wangjc0801/comments/250905.html</wfw:comment><comments>http://www.aygfsteel.com/wangjc0801/archive/2009/01/11/250905.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/wangjc0801/comments/commentRss/250905.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/wangjc0801/services/trackbacks/250905.html</trackback:ping><description><![CDATA[<div id="wmqeeuq" class="wlWriterEditableSmartContent" id="scid:51CF81A4-8F44-4a2c-8837-198C090B9994:bd3dcffa-ba9b-4cbc-b49f-10e6fca80101" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"><p><a atomicselection="true"><img style="border-right: 2px; border-top: 2px; border-left: 2px; border-bottom: 2px" height="172" src="http://lh3.ggpht.com/_L3nkUmUbBp0/SWoFAjMOhHI/AAAAAAAAAGY/kzBvlgs-c7s/s400/java%E8%BF%90%E7%AE%97%E7%AC%A6.jpg" width="400"></a></p></div> <p><font color="#0000ff" size="2"></font> </p> <p><font color="#0000ff" size="2">1. 比较q算W??=?/font></p> <p><font color="#0000ff" size="2">    如果q行比较的两个操作数是数值型?则是比较两个操作数的?只有二者的值相{?p回true.</font></p> <p><font color="#0000ff" size="2">    如果q行比较的是引用cd,则当二者都执行相同的实例时,才返回true.</font></p> <p><font color="#0000ff" size="2">    E序清单QStringcd的比?/font></p> <div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; max-height: 200px"> <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> StringTest {</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> </pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> main(String[] args) {</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> </pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> String str1 = <span style="color: #0000ff">new</span> String(<span style="color: #006080">"123"</span>);</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> String str2 = <span style="color: #0000ff">new</span> String(<span style="color: #006080">"123"</span>);</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> </pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> System.<span style="color: #0000ff">out</span>.println(str1 == str2);<span style="color: #008000">//q里q回false</span></pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> System.<span style="color: #0000ff">out</span>.println(str1.equals(str2));<span style="color: #008000">//true</span></pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> </pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> String str3 = <span style="color: #006080">"123"</span>;</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> String str4 = <span style="color: #006080">"123"</span>;</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> System.<span style="color: #0000ff">out</span>.println(str3 == str4);<span style="color: #008000">//q里q回true</span></pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> System.<span style="color: #0000ff">out</span>.println(str3.equals(str4));<span style="color: #008000">//true</span></pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> </pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> </pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> }</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre></div></div> <p></p> <p><font color="#0000ff" size="2">  如代码所C,两个new得到的String对象虽然值相{?str1.equals(str2)q回true,但由于两个分别是?/font></p> <p><font color="#0000ff" size="2">同的对象实例对象,在堆内存中占有不同的内存I间,所以str1 == str2q回false.当执行str3 = "123"?p?/font></p> <p><font color="#0000ff" size="2">l会(x)先从~存中检查是否有一个内容ؓ(f)"123"的实例存?如果没有则会(x)在缓存中创徏一个String对象"123",</font></p> <p><font color="#0000ff" size="2">执行str4 = ?23”时,~存中已l有了相同内容的对象,pȝ则不再生成对?直接str4指向那个对象.所</font></p> <p><font color="#0000ff" size="2">以str3 == str4?x)返回true.</font></p> <p><font color="#0000ff" size="2">     同样的系l会(x)把一些创建成本大,需要频J用的对象q行~存,从而提高程序的q行性能.再比?/font></p> <p><font color="#0000ff" size="2">Integerc?</font></p> <p><font color="#0000ff" size="2">     E序清单,Integer的比?/font></p> <div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; max-height: 200px"> <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> IntegerTest {</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> </pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> main(String[] args) {</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> </pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> Integer ia = 1;</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> Integer ib = 1;</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> </pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> System.<span style="color: #0000ff">out</span>.println(ia == ib); <span style="color: #008000">//true</span></pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> </pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> Integer ic = 128;</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> Integer id = 128;</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> </pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> System.<span style="color: #0000ff">out</span>.println(ic == id); <span style="color: #008000">//false</span></pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> }</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> </pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre></div></div> <p><font color="#0000ff" size="2">  pȝ?x)将int整数自动装箱成Integer实例,新徏实例?pȝ?x)先创徏一个长度ؓ(f)256的Integer数组,</font></p> <p><font color="#0000ff" size="2">其中存放-128?27的Integer实例,当再需要一个在q个范围中的Integer对象?pȝ?x)直接将引用变?/font></p> <p><font color="#0000ff" size="2">指向相应的实例对?ia和ib都指向相同的Integer对象所以上面的ia == ib q回true;而当需要创建的</font></p> <p><font color="#0000ff" size="2">Integer对象不在q个范围中时,pȝ?/font><font color="#0000ff" size="2">?x)新创徏对?实际上ic和id是两个不同的对象,则ic == id q回false.</font></p> <p><font color="#0000ff" size="2">2. 逻辑q算W?/font></p> <div id="wmqeeuq" class="wlWriterEditableSmartContent" id="scid:51CF81A4-8F44-4a2c-8837-198C090B9994:19a68c7a-351e-4442-ae3e-8ce1f521be2b" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"><p><a atomicselection="true"><img style="border-right: 2px; border-top: 2px; border-left: 2px; border-bottom: 2px" height="158" src="http://lh3.ggpht.com/_L3nkUmUbBp0/SWoRJuRm8-I/AAAAAAAAAG0/08pQT4oYrxg/s400/%E9%80%BB%E8%BE%91%E8%BF%90%E7%AE%97%E7%AC%A6.jpg" width="400"></a></p></div> <p><font color="#0000ff" size="2"></font> </p> <p></p> <p> <font color="#0000ff" size="2">E序清单Q|| ?| 的区?/font></p> <div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; max-height: 200px"> <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> LogicTest {</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> </pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> main(String[] args) {</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> </pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> <span style="color: #0000ff">int</span> a = 1;</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> <span style="color: #0000ff">int</span> b = 2;</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> </pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> <span style="color: #0000ff">if</span> (a == 1 || b++ == 3) {</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> System.<span style="color: #0000ff">out</span>.println(<span style="color: #006080">"a="</span>+a+<span style="color: #006080">",b="</span>+b);<span style="color: #008000">//a=1,b=2</span></pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> }</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> </pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> <span style="color: #0000ff">if</span> (a == 1 | b++ ==3) {</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> System.<span style="color: #0000ff">out</span>.println(<span style="color: #006080">"a="</span>+a+<span style="color: #006080">",b="</span>+b);<span style="color: #008000">//a=1,b=3</span></pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> }</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> </pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"> }</pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"> </pre><pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre></div></div> <p>  <font color="#0000ff" size="2">"||" 当前面的l果为true?不会(x)在计后面的l果,?x)直接返回true;"|"L?x)计前后两个表辑ּ?</font></p><img src ="http://www.aygfsteel.com/wangjc0801/aggbug/250905.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/wangjc0801/" target="_blank">wangjc</a> 2009-01-11 23:41 <a href="http://www.aygfsteel.com/wangjc0801/archive/2009/01/11/250905.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>理解面向对象http://www.aygfsteel.com/wangjc0801/archive/2009/01/10/250761.htmlwangjcwangjcFri, 09 Jan 2009 16:17:00 GMThttp://www.aygfsteel.com/wangjc0801/archive/2009/01/10/250761.htmlhttp://www.aygfsteel.com/wangjc0801/comments/250761.htmlhttp://www.aygfsteel.com/wangjc0801/archive/2009/01/10/250761.html#Feedback0http://www.aygfsteel.com/wangjc0801/comments/commentRss/250761.htmlhttp://www.aygfsteel.com/wangjc0801/services/trackbacks/250761.html目前软g开发的两种开发方?l构化开发方法和面向对象开发方?

  • 一. l构化开发方?

1.d按照功能来分析系l需?也称为面向功能程序设?/font>

2.l构化程序设计首先采用结构化分析Ҏ(gu)(Structrued Analysis SA)对系l进行需求分?然后使用l构化设?Structrued Design SD)Ҏ(gu)对系l进行概要设?详细设计,最后采用结构化~程(Structrued Program SP)的方式来实现pȝ

3.最程序单元是"函数",每个函数完成一个功?

  • ? 面向对象开发方?

     1.从现实世界中的客观存在事物出发来构造Y件系l?q在pȝ构造中可能地q用人类的自然思维方式,以现实世界中的客观存在ؓ(f)中心,来思考问?认识问题,q根据事物的本质特点,它们抽象ؓ(f)pȝ中的c?作ؓ(f)pȝ的基本构造单?

     2.使用c?对象,l承,装,消息{基本概忉|q行E序设计.

     3.区别?Z对象".Z对象也用了对象,但无法利用现有的对象M生新的对?也就是说不能l承.比如javascript

     4.面向对象的三个基本特?   

         装:

       对象的实现l节隐藏h,在通过公用的方法来暴露该对象的功能.java提供private,protected和public三个讉K控制修饰W实现良好的装

         l承:

       提供了extends来让子类l承父类.l承是实UY件复用的重要手段.

         多?

       使用l承关系来实现复用时,子类对象可以直接赋给父类变量.

    5.java中的cM要有两种l构关系:

       a 一?Ҏ(gu):l承关系,如h-男h,是一U?is a"的关p?/font>

       b 整体-部分:l合关系,如h-背,是一U?has a"的关p?/font>



wangjc 2009-01-10 00:17 发表评论
]]>
java标识W?/title><link>http://www.aygfsteel.com/wangjc0801/archive/2009/01/09/250753.html</link><dc:creator>wangjc</dc:creator><author>wangjc</author><pubDate>Fri, 09 Jan 2009 15:07:00 GMT</pubDate><guid>http://www.aygfsteel.com/wangjc0801/archive/2009/01/09/250753.html</guid><wfw:comment>http://www.aygfsteel.com/wangjc0801/comments/250753.html</wfw:comment><comments>http://www.aygfsteel.com/wangjc0801/archive/2009/01/09/250753.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/wangjc0801/comments/commentRss/250753.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/wangjc0801/services/trackbacks/250753.html</trackback:ping><description><![CDATA[<ul> <li><font color="#0000ff" size="2">1. 只能以字?下划U?_)或美元符($)开?数字不能作ؓ(f)开?/font> <li><font color="#0000ff" size="2">2. 不能包含元W?$)以外的特D符?/font> <li><font color="#0000ff" size="2">3. 不能包含I格</font> <li><font color="#0000ff" size="2">4. 可以是中文字W或日文字符</font></li></ul><img src ="http://www.aygfsteel.com/wangjc0801/aggbug/250753.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/wangjc0801/" target="_blank">wangjc</a> 2009-01-09 23:07 <a href="http://www.aygfsteel.com/wangjc0801/archive/2009/01/09/250753.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>java数据cdhttp://www.aygfsteel.com/wangjc0801/archive/2009/01/09/250751.htmlwangjcwangjcFri, 09 Jan 2009 15:03:00 GMThttp://www.aygfsteel.com/wangjc0801/archive/2009/01/09/250751.htmlhttp://www.aygfsteel.com/wangjc0801/comments/250751.htmlhttp://www.aygfsteel.com/wangjc0801/archive/2009/01/09/250751.html#Feedback0http://www.aygfsteel.com/wangjc0801/comments/commentRss/250751.htmlhttp://www.aygfsteel.com/wangjc0801/services/trackbacks/250751.html1. 整型

    整型有三张表C方?

  • 10q制 :int i = 10;
  • 8q制   :int i = 013;
  • 16q制 :int i = 0xaF;

2. 字符?/font>

    字符型有四种表示方式:

  • 单个字符     : char c = ‘a?
  • 转义字符     : char c = ‘\n?
  • unicode~码: char c = ‘\u9999?
  • int            : char c = 97;

3. 点?/font>

     点型有两种表示方式:

  • 10q制?nbsp;   : 5.123
  • U学计数?nbsp; : 5.123E2

public class FloatTest {
 
 
    public static void main(String[] args) 
    {
        //点型数据必d含小数点,否则?x)当作int处理
        //点型默认是double型的,默认?4位的,所以定义floatcd?必须在最后加f表示float?/span>
        float f = 5.0f;
        float af = 5.123456789f;
        double df = 5.12345678987654321;
        //点数是使用二进制数据的U学计数法表C?所以不可能_表示一个QҎ(gu).下面看到的值已l发生了改变
        System.out.println("af="+af);
        System.out.println("df="+df);
        //result:af=5.123457,df=5.123456789876543
        double a = 0.0;
        double c = Double.NEGATIVE_INFINITY;
        float d = Float.NEGATIVE_INFINITY;
        //看到float和double的负无穷大的是相{的?/span>
        System.out.println(c == d);
        //0.0除以0.0出现非?/span>
        System.out.println(a / a);
        //两个非数之间是不相等?/span>
        System.out.println(a / a == Float.NaN);
        //所有正无穷大都是相{的
        System.out.println(6.0 / 0 == 555.0/0);
        //负数除以0.0得到负无I大
        System.out.println(-8 / a);
        
        System.out.println(0.0 / 0.0);
        //result:NaN
 
        //下面代码抛出除?的异?/span>
        System.out.println(0 / 0);
        
    }
}

4. 布尔?/font>

    只能?true"?false",不能和其他基本数据类型相转换

5. cd转换

   a. 自动cd转换

数据范围的变量或数值可以直接赋l数据范围大的变量或数?pȝ可以自动转换

   b. 强制cd转换

        cM一个大瓶子的水倒入一个小瓶子?如果大瓶子中的水q多,可能导致溢?

     代码清单:强制cd转换

 
public class Conversion {
 
    /**
     * @param args
     */
    public static void main(String[] args) {
        /*
         * int?2位的,例如233:
         *     0000 0000 0000 0000 0000 0000 1110 1001
         * int赋给byte,byte?位的,所以会(x)前面的24截取?变成?/span>
         *                                1110 1001
         *  整数在计机中是以补码的形式保存?正数的补码和原码相同,负数的补码是原码取反?,最高位不变.所?/span>
         *  11101001                               
         *     | ?,成反?/span>
         *  11101000
         *     | 取反成原?/span>
         *  10010111 也就?23
        */
        int intValue = 233;
        byte byteValue = (byte)intValue;
        System.out.println(byteValue);
        
        double doubleValue = 3.98;
        int intValue1 = (int)doubleValue;
        System.out.println(intValue1);
    }
 
}


wangjc 2009-01-09 23:03 发表评论
]]>
javaҎ(gu)http://www.aygfsteel.com/wangjc0801/archive/2009/01/05/249983.htmlwangjcwangjcMon, 05 Jan 2009 12:50:00 GMThttp://www.aygfsteel.com/wangjc0801/archive/2009/01/05/249983.htmlhttp://www.aygfsteel.com/wangjc0801/comments/249983.htmlhttp://www.aygfsteel.com/wangjc0801/archive/2009/01/05/249983.html#Feedback0http://www.aygfsteel.com/wangjc0801/comments/commentRss/249983.htmlhttp://www.aygfsteel.com/wangjc0801/services/trackbacks/249983.htmljavaҎ(gu)

       1. java中方法不能单独存?所有方法必d义在cM.

       2. 从逻辑上说,一个方法要么属于一个对?要么属于一个类(使用static修改的方?

       3. 永远不可能独立执行方?执行Ҏ(gu)必须使用cL对象作ؓ(f)调用?包括隐式的this对象

1. 常用的修饰符:

    static 可用于修饰方?属性等成员.修饰成员表示q个成员是属于这个类所共有?而不是属于这个类的某一个实?不用static的成员则表示,q些成员是属于该cȝ某一个实?而不是属于这个类.静态成员不能访问非静态成?

2. Ҏ(gu)的参C递机?/font>

       javaҎ(gu)的参C递机?g?所谓g?是实际参数的副本传入Ҏ(gu)?而参数本w不受Q何媄(jing)?

       基本数据cd和引用类型都是采用g递的方式q行.但如果传递的是引用类?虽然传入Ҏ(gu)内的是引用类型变量的一个副?但两个引用类型变量都是指向了同一个堆内存中的对象,通过M一个引用变量对对象q行操作,都会(x)对这个对象生媄(jing)?

public class Test { 

//基本数据cd的g?/span>
private static void swap(int a ,int b) {
  int tmp = a;
  a = b;
  b = tmp;
  System.out.println("swap()Ҏ(gu)?a="+a+",b="+b);
}
// 引用cd的g?/span>
private static void swap2(Obj o) {
  int tmp = o.a;
  o.a = o.b;
  o.b = tmp;
  System.out.println("swap2()Ҏ(gu)?a="+o.a+",b="+o.b);
}
public static void main(String[] args) {
  int a = 10;
  int b = 20;
  swap(a,b);
  System.out.println("swap()Ҏ(gu)?a="+a+",b="+b);
  Obj o = new Obj();
  swap2(o);
  System.out.println("swap2()Ҏ(gu)?a="+o.a+",b="+o.b);
}
} 

3. 形参长度可变的方?/font>

   长度可变的Ş参只能位于Ş参队列的最?一个方法中只能出现一个长度可变的形参.q个长度可变的Ş参可以传入多个参?也可以传入一个数l?/font>

public class Test3 { 

public String test(String... Strs) {
  String result = "";
  for (String tmp : Strs) {
   result += tmp;
  }
  return result;
}
public static void main(String[] args) {
  Test3 t = new Test3();
  System.out.println(t.test("Hello","!","world"));//可以传入多个String
  String[] tmp = {"Hello","!","world"};
  System.out.println(t.test(tmp));//也可以传入一个String数组
} 

} 

4. Ҏ(gu)重蝲

   1. 出现在同一个类?/font>

   2. Ҏ(gu)名相?Ҏ(gu)的Ş参列表不?/font>

   3. Ҏ(gu)的其他属性如q回值类?修饰W等与方法重载无?/font>

5. Ҏ(gu)重写

    1. 出现在承中,子类和父cd名的Ҏ(gu)之间

    2. Ҏ(gu)?Ҏ(gu)形参列表相同;q回值类?子类比父cL或相等;抛出的异?子类比父cL或相等;讉K权限,子类比父cL大或相等;二者必d时ؓ(f)实例Ҏ(gu),或同时ؓ(f)cL?/font>

    3. U有的父cL?是不能被重写?/font>

6. 构造器

   1. 每个javac都臛_有一个构造器,如果没有昑ּ的提?pȝ?x)提供一个无参数的执行体为空的构造器

   2. 构造器可以重蝲,每个不同的构造器之间可用this怺调用,且只能在W一行调?/font>



wangjc 2009-01-05 20:50 发表评论
]]>
Java概述http://www.aygfsteel.com/wangjc0801/archive/2009/01/04/249782.htmlwangjcwangjcSun, 04 Jan 2009 11:29:00 GMThttp://www.aygfsteel.com/wangjc0801/archive/2009/01/04/249782.htmlhttp://www.aygfsteel.com/wangjc0801/comments/249782.htmlhttp://www.aygfsteel.com/wangjc0801/archive/2009/01/04/249782.html#Feedback0http://www.aygfsteel.com/wangjc0801/comments/commentRss/249782.htmlhttp://www.aygfsteel.com/wangjc0801/services/trackbacks/249782.html1. javaE序的运行机?/font>

    1. java语言是编译型q是解释?

    ~译型语a指用特定的~译?针对特定的^?操作pȝ)源代码一ơ性翻译成可被该^台硬件执行的机器?包括机器指o(h)和操作数),q包装成该^台所能识别的可执行的E序.q个q程UCؓ(f)~译(Compile).~译生成的可执行的程序可以脱d发环?在特定的q_上运?解释型语a指用专门的解释器对源代码逐行解释成特定^台的机器?q立x?解释型语a相当于将~译型语a中的~译和解释工作؜合在一起同时进?

    解释型语a每执行一ơ源代码需要重新编译一?因此执行效率比较?但解释型语言跨^台确比较Ҏ(gu),只需要提供特定^台的解释?该解释器׃(x)负责源代码解释成特定^C的机器指?而编译型语言的运行效率较?但移植比较麻?如果需要移?则需要将源代码复制到新的q_?针对该^台进行修改ƈ重新~译.

    java语言既是~译型语a也是解释型语a.

       java语言在编译的时候不是生成特定^台相关的机器?而是生成与^台无关的字节?只面向JVM(Java Virtual Machine).

       q种字节码必ȝJVM解释执行.

    2. 关于JVM

    JVM可以看做是一个抽象的计算?和实际的计算Z?也具有指令集和存储区?它负责执行指?q要负责理数据,内存和寄存器.不同q_上的JVM是不同的,但是它们都提供相同的~程接口.所以JVM是java语言可以跨^台的关键部分,只要不同的^台实C同的JVM,~译后的class文g可以在该^Cq行.

2. 关于Java的部分概?/font>

    1. JDK=Java SE Development Kit,java标准版开发包,包括java~译?q行时环?JRE)以及(qing)常用的javacd{?/font>

    2. JRE=Java Runtime Environment.javaq行时环?包含JVM以外,q有其他cȝ加蝲?字节码校验器以及(qing)大量的基cd.

    3. Java EE SDK . 包括JDK.学习(fn)java EE的Y件和开发包(Software Development Kit)

3. java的环境配|?/font>

   1. 在windows下用户变量和pȝ变量的区?

   pȝ变量Ҏ(gu)有用h?用户变量只对当前用户有效.对于当前用户而言,两者没有区?只是pȝ变量中的配置?x)优先于用户变量中的配?

   2. classPath需要配|吗?

   JDK1.4以前的版本需要手动的讄classPath变量,让JREL索javacL?需要配|当前\?.)?JAVA_HOME%\lib\dt.jar?JAVA_HOME%\lib\tools.jar.但在JDK1.5中可以不配置classPath变量,JRE可以自动搜烦(ch)当前路径下的cL件和自动加蝲dt.jar和tools.jar,同时如果配置了classPath变量,则JRE?x)严格按照变量的路径L索类文g.也就是说如果要配|classPath路径׃定要配置准确,否则q是要报错的.

   3. classPath变量中的路径量不要出现I格,否则可以引发错误

4. java的垃圑֛收机?Garbage Collection GC)

    1. java语言不需要程序员直接控制内存回收.垃圾回收的发生有不可预知?不徏议用finalize()和System.gc()来徏议系l进行垃圑֛?因ؓ(f)q样做不不能_և的控制垃圑֛收机?可以通过暗示使垃圑֛收机制可以尽快的回收那些不再使用的对?也就是将该对象的引用变量讄为null.

    2. javaE序的内存分配和回收都是由JRE在后台进行维?通常JRE?x)提供一个超U线E来q行(g)和控制,一般是在CPUI闲或内存不的情况下自动进行的,E序员无法精控制垃圑֛收的旉和顺?

    3. javaq行时的数据存放在堆内存,垃圾回收机制是负责q部分内存中的资?它ƈ不能回收其他如磁盘I(y)O{物理资?

    4. 垃圾回收机制的缺Ҏ(gu)它的开销?x)?jing)响程序的性能.JVM所做的是监控E序中所有的对象,认哪些是无用的对象,q最l释放这些无用的对象.而且java的垃圑֛收机制还不是很完?它ƈ不能保证可以100%地收集所有废弃的内存.

    5. 垃圾回收的精性主要包?个方?

       a. 垃圾回收机制可以_地标记活着的对?Java中的对象h唯一?

       b. 垃圾回收机制可以_地定位对象之间的引用关系.q是实现归ƈ和复制等法的必要条?通过q种引用关系,可以保证所有相关的对象都能被可靠的回收?qing)重新分?从而有效地减少内存片的?/font>

    6. 现在的JVM有多U不同的垃圾回收机制,每种机制?x)因其算法差异而表现各?有的当垃圑֛收开始时停止应用程序的q行,有的当垃圑֛收运行时,同时允许应用E序q行,q有的同一旉垃圾回收有多个线E在q行.



wangjc 2009-01-04 19:29 发表评论
]]>
վ֩ģ壺 | ľ˹| ޭ| Ǹ| ɽ| ԭ| ʤ| | ػ| ƽ| ɶ| Ǭ| ˫| ¤| | ɽ| | | | пѷ| ƽ| | | | ľ˹| | ΢| | Ǭ| ֲ| | ̷| ʤ| ƽ| Ϊ| | | | ʯׯ| ƽ| |