⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 1061.html

📁 著名的linux英雄站点的文档打包
💻 HTML
📖 第 1 页 / 共 2 页
字号:
        border=0>
         <TR>
          <TD>
            <TABLE cellSpacing=0 cellPadding=3 width="100%" border=0>
              
              <TR>
                <TD vAlign=top align=middle width="60%">
                  <TABLE cellSpacing=0 cellPadding=0 width="100%" 
                  background="images/back.gif" tppabs="http://www.linuxhero.com/docs/images/back.gif" border=0>
                    <TBODY>
                    <TR>
                        <TD vAlign=top width="80%"> 
                          <DIV align=center>
                        <FORM action="search.html" tppabs="http://www.linuxhero.com/docs/search.html" method=get>
                            </FORM>
                        <TABLE cellSpacing=0 cellPadding=0 width="95%" 
                          border=0><TBODY>
                          <TR>
                            <TD background="images/bgi.gif" tppabs="http://www.linuxhero.com/docs/images/bgi.gif" 
                          height=30></TD></TR></TBODY></TABLE>
                        <TABLE cellSpacing=0 cellPadding=3 width="95%" 
                        align=center border=0>
                          <TBODY>
                          <TR>
                            <TD>
                              <TABLE cellSpacing=0 cellPadding=3 width="100%" 
                              border=0>
                                <TBODY>
                                <TR>
                                      <TD vAlign=top> 
<p><FONT class=normalfont><B><font color=blue>全世界所有程序员都会犯的错误</font></B></FONT><BR><FONT class=smallfont color=#ff9900>2004-04-23 15:18 pm</FONT><BR><FONT class=normalfont>作者:atfa<br>来自:Linux知识宝库<br>联系方式:无名<br><br>当年,国际巨星成龙的「龙种」曝光,众人指责他对不起娇妻林凤娇,逼得他出面召开记者会,向世人自白他犯了「全世界所有男人都会犯的错误」。从来没犯过这种错误的我,也因此常常认为自己不是个男人。<br>
<br>
虽然没犯过「全世界所有男人都会犯的错误」,但是我倒是曾经犯了「全世界所有程序员都会犯的错误」。不管使用何种语言,全世界所有程序员都一定犯过这种错误,那就是:太依赖编译器,却不知道编译器做了哪些事。<br>
<br>
一般来说,越高阶的程序语言,会提供越多语法上的便利,以方便程序撰写,这就俗称为syntactic sugar,我称其为「语法上的甜头」。虽说是甜头,但是如果你未能了解该语法的实质内涵,很可能会未尝甜头,却吃尽苦头。<br>
<br>
不久前,我收到一个电子邮件,读者列出下面的Java程序,向我求救。看过这个程序之后,我确定这又是一个「全世界所有程序员都会犯的错误」。<br>
<br>
// 程序1<br>
class Singleton {<br>
private static Singleton obj = new Singleton();<br>
public static int counter1;<br>
public static int counter2 = 0;<br>
private Singleton() {<br>
counter1++;<br>
counter2++;<br>
}<br>
public static Singleton getInstance() {<br>
return obj;<br>
}<br>
}<br>
<br>
// 程序2<br>
public class MyMain {<br>
public static void main(String[] args) {<br>
Singleton obj = Singleton.getInstance();<br>
System.out.println("obj.counter1=="+obj.counter1);<br>
System.out.println("obj.counter2=="+obj.counter2);<br>
}<br>
}<br>
<br>
执行结果是:<br>
obj.counter1==1<br>
obj.counter2==0<br>
<br>
你有没有被此结果吓一跳?乍看程序代码,你很可能会认为counter1和counter2的值一定会相等,但执行结果显然不是如此。其实,程序1被编译后的程序应该等同于下面的程序3:<br>
<br>
// 程序3<br>
class Singleton {<br>
private static Singleton obj;<br>
public static int counter1;<br>
public static int counter2;<br>
static { // 这就是class constructor<br>
// 在进入此class constructor之前,class已经被JVM<br>
// 配置好内存,所有的static field都会被先设定为0,<br>
// 所以此时counter1和counter2都已经是0,且singleton为null<br>
obj = new Singleton(); // 问题皆由此行程序产生<br>
// counter1不会在此被设定为0<br>
counter2 = 0; // counter2再被设定一次0(其实是多此一举)<br>
}<br>
private Singleton() { // 这是instance constructor<br>
counter1++;<br>
counter2++;<br>
}<br>
public static Singleton getInstance() {<br>
return obj;<br>
}<br>
}<br>
<br>
这是因为:当class具有static field,且直接在宣告处透过「=...」的方式设定其值时,编译器会自动将这些叙述依序搬到class constructor内。同样地,当class具有instance field,且直接在宣告处透过「=...」的方式设定其值时,编译器会自动将这些叙述依序搬到instance constructor内。<br>
<br>
此程序在class constructor内,还未将static field初始化时(这时候,counter1和counter2都是0),就呼叫instance constructor,而instance constructor竟然还会去更动static field的值,使得counter1和counter2都变成1。然后instance constructor执行完,回到class constructor,再把counter2的值设为0(但是<br>
counter1维持不变)。最后的结果:counter1等于1,counter2等于0。<br>
<br>
欲改正程序1,方法有三:<br>
<br>
-方法一:将singleton field的宣告调到counter1与counter2 field之后。<br>
这是最好的作法。<br>
-方法二:将counter2=0的宣告中,「=0」的部分删除。这种作法只有在希望<br>
-方法三:将初始化的动作搬到class constructors内,自行撰写,而不依赖<br>
编译器产生。这是最保险的作法。<br>
<br>
如何避免犯下「全世界所有程序员都会犯的错误」,我给各位Java程序员<br>
的建议是:<br>
-熟读Java Language Specification<br>
-在有疑问时,使用J2SDK所提供的javap来反组译Java Bytecode,直接观察<br>
编译后的结果。<br>
<br>
下面是我用javap来反组译程序1的示范:<br>
<br>
C:&gt;javap -c -classpath . Singleton<br>
<br>
Compiled from MyMain.java<br>
class Singleton extends java.lang.Object {<br>
public static int counter1;<br>
public static int counter2;<br>
public static Singleton getInstance();<br>
static {};<br>
}<br>
<br>
Method Singleton()<br>
0 aload_0<br>
1 invokespecial #1<br>
4 getstatic #2<br>
7 iconst_1<br>
8 iadd<br>
9 putstatic #2<br>
12 getstatic #3<br>
15 iconst_1<br>
16 iadd<br>
17 putstatic #3<br>
20 return<br>
<br>
Method Singleton getInstance()<br>
0 getstatic #4<br>
3 areturn<br>
<br>
Method static {}<br>
0 new #5<br>
3 dup<br>
4 invokespecial #6<br>
7 putstatic #4<br>
10 iconst_0<br>
11 putstatic #3<br>
14 return<br>
<br>
其实Java的syntactic sugar并不算多,C#的syntactic sugar才真的是无所不在,<br>
也因此C#的初学者更容易犯了「全世界所有程序员都会犯的错误」。许多C#的书都会一边介绍C#语法,一边介绍编译之后MSIL(.NET的中间语言,类似Java的Bytecode)的结果,然而Java的书却鲜少这么做。<br>
<br>
虽说是「全世界所有程序员都会犯的错误」,但是这不代表你犯了此错误之后,仍可以同爱借钱的曹启泰一般地「抬头挺胸、理直气壮」。只要有心,其实这一类的错误仍是可以避免的。
_________________
</FONT><br>
                                      </TD>
                                    </TR>
                                <TR>
                                <TD colSpan=2><FONT 
                                class=middlefont></FONT><BR>
                                        <FONT 
                                class=normalfont>全文结束</FONT> </TD>
                                    </TR>
                                <TR>
                                <TD background="images/dot.gif" tppabs="http://www.linuxhero.com/docs/images/dot.gif" colSpan=2 
                                height=10></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></DIV></TD>
                        <TD vAlign=top width="20%" 
                      background="images/line.gif" tppabs="http://www.linuxhero.com/docs/images/line.gif" rowSpan=2> 
                          <DIV align=center> 
                            <table class=tableoutline cellspacing=1 cellpadding=4 
                        width="100%" align=center border=0>
                              <tr class=firstalt> 
                                <td noWrap background="images/bgline.gif" tppabs="http://www.linuxhero.com/docs/images/bgline.gif" colspan=2 height=21>
                                <font class=normalfont><b>所有分类</b></font></td>
                              </tr>
<tr class=secondalt> <td noWrap width=27%> <font class=normalfont>1:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type1.html" tppabs="http://www.linuxhero.com/docs/type1.html">非技术类</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>2:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type2.html" tppabs="http://www.linuxhero.com/docs/type2.html">基础知识</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>3:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type3.html" tppabs="http://www.linuxhero.com/docs/type3.html">指令大全</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>4:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type4.html" tppabs="http://www.linuxhero.com/docs/type4.html">shell</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>5:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type5.html" tppabs="http://www.linuxhero.com/docs/type5.html">安装启动</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>6:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type6.html" tppabs="http://www.linuxhero.com/docs/type6.html">xwindow</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>7:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type7.html" tppabs="http://www.linuxhero.com/docs/type7.html">kde</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>8:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type8.html" tppabs="http://www.linuxhero.com/docs/type8.html">gnome</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>9:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type9.html" tppabs="http://www.linuxhero.com/docs/type9.html">输入法类</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>10:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type10.html" tppabs="http://www.linuxhero.com/docs/type10.html">美化汉化</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>11:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type11.html" tppabs="http://www.linuxhero.com/docs/type11.html">网络配置</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>12:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type12.html" tppabs="http://www.linuxhero.com/docs/type12.html">存储备份</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>13:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type13.html" tppabs="http://www.linuxhero.com/docs/type13.html">杂项工具</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>14:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type14.html" tppabs="http://www.linuxhero.com/docs/type14.html">编程技术</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>15:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type15.html" tppabs="http://www.linuxhero.com/docs/type15.html">网络安全</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>16:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type16.html" tppabs="http://www.linuxhero.com/docs/type16.html">内核技术</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>17:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type17.html" tppabs="http://www.linuxhero.com/docs/type17.html">速度优化</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>18:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type18.html" tppabs="http://www.linuxhero.com/docs/type18.html">apache</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>19:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type19.html" tppabs="http://www.linuxhero.com/docs/type19.html">email</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>20:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type20.html" tppabs="http://www.linuxhero.com/docs/type20.html">ftp服务</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>21:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type21.html" tppabs="http://www.linuxhero.com/docs/type21.html">cvs服务</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>22:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type22.html" tppabs="http://www.linuxhero.com/docs/type22.html">代理服务</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>23:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type23.html" tppabs="http://www.linuxhero.com/docs/type23.html">samba</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>24:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type24.html" tppabs="http://www.linuxhero.com/docs/type24.html">域名服务</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>25:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type25.html" tppabs="http://www.linuxhero.com/docs/type25.html">网络过滤</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>26:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type26.html" tppabs="http://www.linuxhero.com/docs/type26.html">其他服务</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>27:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type27.html" tppabs="http://www.linuxhero.com/docs/type27.html">nfs</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>28:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type28.html" tppabs="http://www.linuxhero.com/docs/type28.html">oracle</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>29:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type29.html" tppabs="http://www.linuxhero.com/docs/type29.html">dhcp</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>30:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type30.html" tppabs="http://www.linuxhero.com/docs/type30.html">mysql</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>31:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type31.html" tppabs="http://www.linuxhero.com/docs/type31.html">php</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>32:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type32.html" tppabs="http://www.linuxhero.com/docs/type32.html">ldap</a></font></td>    </tr>  </table></td></tr>                            </table>
                          </DIV></TD></TR>
                    <TR vAlign=top>
                        <TD width="80%"> 
                          <DIV align=center><BR>
                          </DIV>
                        </TD></TR></TBODY></TABLE></TD></TR>
                </TABLE></TD></TR>
          </TABLE>
      <TABLE cellSpacing=0 cellPadding=4 width="100%" bgColor=#eeeeee 
        border=0><TBODY>
        <TR>
          <TD width="50%">
              <P><FONT class=middlefont>版权所有 &copy; 2004 <A 
            href="mailto:bjchenxu@sina.com">linux知识宝库</A><BR>
                违者必究. </FONT></P>
            </TD>
          <TD width="50%">
              <DIV align=right><FONT class=middlefont>Powered by: <A 
            href="mailto:bjchenxu@sina.com">Linux知识宝库</A> Version 0.9.0 </FONT></DIV>
            </TD></TR></TBODY></TABLE>
      <CENTER></CENTER></TD></TR>
    </TABLE></CENTER></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -