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 阅读(1921) 评论(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  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: