todayx.org
          todayx.org
          posts - 39,comments - 60,trackbacks - 0

          這些最為奇怪的程序語言的特性,來自stackoverflow.com,原貼在這里。我摘選了一些例子,的確是比較怪異,讓我們一個一個來看看。

          1、C語言中的數組

          在C/C++中,a[10] 可以寫成 10[a]

          “Hello World”[i] 也可以寫成 i["Hello World"] 

          2、在Javascript中 

           ’5′ + 3 的結果是:’53′

           ’5′ – 3 的結果是:2              更多javascript點擊這里

          3、C/C++中的Trigraphs 

          Cpp代碼  收藏代碼
          1. int main() {  
          2.     cout << "LOL??!";  
          3. }  

           上面的這段程序會輸出: “LOL|”,這是因為 ??! 被轉成了 | ,關于Trigraphs,下面有個表格: 

          ??= #
          ??( [
          ??/ \
          ??) ]
          ??’ ^
          ??< {
          ??! |
          ??> }
          ??- ~

          4、JavaScript 的條件表 

          看到下面這個表,不難理解為什么Javascript程序員為什么痛苦了

          Js代碼  收藏代碼
          1. ''        ==   '0'          //false  
          2. 0         ==   ''           //true  
          3. 0         ==   '0'          //true  
          4. false     ==   'false'      //false  
          5. false     ==   '0'          //true  
          6. false     ==   undefined    //false  
          7. false     ==   null         //false  
          8. null      ==   undefined    //true  
          9. " \t\r\n" ==   0            //true  

           

          5、Java的Integer cache

          Java代碼  收藏代碼
          1. Integer foo = 1000;  
          2. Integer bar = 1000;  
          3.   
          4. foo <= bar; // true  
          5. foo >= bar; // true  
          6. foo == bar; // false  
          7.   
          8. //然后,如果你的 foo 和 bar 的值在 127 和 -128 之間(包括)  
          9. //那么,其行為則改變了:  
          10.   
          11. Integer foo = 42;  
          12. Integer bar = 42;  
          13.   
          14. foo <= bar; // true  
          15. foo >= bar; // true  
          16. foo == bar; // true  

          為什么會這樣呢?你需要了解一下Java Interger Cache,下面是相關的程序,注意其中的注釋

          Java代碼  收藏代碼
          1. /** 
          2.  
          3.      * Returns a <tt>Integer</tt> instance representing the specified 
          4.  
          5.      * <tt>int</tt> value. 
          6.  
          7.      * If a new <tt>Integer</tt> instance is not required, this method 
          8.  
          9.      * should generally be used in preference to the constructor 
          10.      * <a href="mailto:{@link">{@link</a> #Integer(int)}, as this method is likely to yield 
          11.      * significantly better space and time performance by caching 
          12.      * frequently requested values. 
          13.      * 
          14.      * @param  i an <code>int</code> value. 
          15.      * @return a <tt>Integer</tt> instance representing <tt>i</tt>. 
          16.      * @since  1.5 
          17.      */  
          18.     public static Integer valueOf(int i) {  
          19.         if(i >= -128 && i <= IntegerCache.high)  
          20.             return IntegerCache.cache[i + 128];  
          21.         else  
          22.             return new Integer(i);  
          23.     }  

          5、Perl的那些奇怪的變量

          Php代碼  收藏代碼
          1. $.  
          2. $_  
          3. $_#  
          4. $$  
          5. $[  
          6. @_  

           其所有的這些怪異的變量請參看:http://www.kichwa.com/quik_ref/spec_variables.html

           

          6、Java的異常返回

          請看下面這段程序,你覺得其返回true還是false?

          Java代碼  收藏代碼
          1. try {  
          2.     return true;  
          3. finally {  
          4.     return false;  
          5. }  

           在 javascript 和python下,其行為和Java的是一樣的。 

          7、C語言中的Duff device

          下面的這段程序你能看得懂嗎?這就是所謂的Duff Device,相當的怪異。

          C代碼  收藏代碼
          1. void duff_memcpy( char* to, char* from, size_t count ) {  
          2.     size_t n = (count+7)/8;  
          3.     switch( count%8 ) {  
          4.     case 0: do{ *to++ = *from++;  
          5.     case 7:     *to++ = *from++;  
          6.     case 6:     *to++ = *from++;  
          7.     case 5:     *to++ = *from++;  
          8.     case 4:     *to++ = *from++;  
          9.     case 3:     *to++ = *from++;  
          10.     case 2:     *to++ = *from++;  
          11.     case 1:     *to++ = *from++;  
          12.             }while(--n>0);  
          13.     }  
          14. }   

          8、PHP中的字符串當函數用

          PHP中的某些用法也是很怪異的

          Php代碼  收藏代碼
          1. $x = "foo";  
          2. function foo(){ echo "wtf"; }  
          3. $x();  

          9、在C++中,你可以使用空指針調用靜態函數

          Cpp代碼  收藏代碼
          1. class Foo {  
          2.   public:  
          3.     static void bar() {  
          4.       std::cout << "bar()" << std::endl;  
          5.     }  
          6. };  

          呵呵。的確是挺怪異的。

          轉自iteye
          http://justjavac.iteye.com/blog/1297756

          歷史上的今天
          回顧歷史的今天,歷史就像生活的一面鏡子;可以了解歷史的這一天發生的事件;借古可以鑒今;歷史是不能忘記的.要記住歷史的每一天
          http://www.todayx.org/
          posted on 2011-12-08 19:23 todayx.org 閱讀(1929) 評論(6)  編輯  收藏

          FeedBack:
          # re: 細數那些令人發狂的程序語言的特性
          2011-12-09 16:01 | DB Compare Tool
          有意思  回復  更多評論
            
          # re: 細數那些令人發狂的程序語言的特性
          2011-12-10 07:08 | 淘寶靴子
          學習了。  回復  更多評論
            
          # re: 細數那些令人發狂的程序語言的特性
          2011-12-10 10:55 | tb
          這個真不錯  回復  更多評論
            
          # re: 細數那些令人發狂的程序語言的特性
          2011-12-10 20:58 | 何楊
          不錯,收藏了。  回復  更多評論
            
          # re: 細數那些令人發狂的程序語言的特性[未登錄]
          2011-12-13 17:00 | tbw
          不錯  回復  更多評論
            
          # re: 細數那些令人發狂的程序語言的特性
          2011-12-17 15:53 | 王鵬飛
          用得到4 5 6  回復  更多評論
            

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 菏泽市| 株洲市| 宁武县| 余江县| 盖州市| 庆城县| 朔州市| 吉水县| 洞口县| 阳朔县| 斗六市| 汝南县| 三江| 东乌| 平山县| 河间市| 科技| 中宁县| 保德县| 雅江县| 宁南县| 兰溪市| 乐业县| 昌邑市| 临猗县| 阿坝县| 无锡市| 中西区| 余庆县| 普陀区| 陇南市| 南丰县| 汶川县| 台北县| 璧山县| 金秀| 尚志市| 会昌县| 项城市| 锦屏县| 鄯善县|