---
title: "【转】细数那些令人发狂的程序语言的特性"
author: "foohsinglong"
date: "2011-12-11T14:41:16Z"
tags: ["C++", "Java", "JavaScript", "PHP", "Python"]
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2011-12-11T14:46:08Z"
id: 1304954
url: "https://foohsinglong.iteye.com/blog/1304954"
---

\[b]1、C语言中的数组\[/b] 在C/C++中，a\[10] 可以写成 10\[a] “Hello World”\[i] 也可以写成 i\["Hello World"] \[b]2、在Javascript中\[/b] '5' + 3 的结果是：'53' '5' – 3 的结果是：2 \[b]3、C/C++中的Trigraphs \[/b] \[code="Cpp"]int main() { cout &lt;&lt; "LOL??!"; } int main() { cout &lt;&lt; "LOL??!"; }\[/code] 上面的这段程序会输出： “LOL|”，这是因为 ??! 被转成了 | ，关于Trigraphs，下面有个表格： \[table] |??= | # | |??( | \[ | |??/ | \\ | |??) | ] | |??’| ^ | |??&lt; | { | |??! | 丨| |??&gt; | } | |??- | ~ | \[/table] \[b]4、JavaScript 的条件表 \[/b] 看到下面这个表，不难理解为什么Javascript程序员为什么痛苦了 \[code="JS"]'' == '0' //false 0 == '' //true 0 == '0' //true false == 'false' //false false == '0' //true false == undefined //false false == null //false null == undefined //true " \\t\\r\\n" == 0 //true '' == '0' //false 0 == '' //true 0 == '0' //true false == 'false' //false false == '0' //true false == undefined //false false == null //false null == undefined //true " \\t\\r\\n" == 0 //true \[/code] \[b]5、Java的Integer cache\[/b] \[code="java"]Integer foo = 1000; Integer bar = 1000; foo &lt;= bar; // true foo &gt;= bar; // true foo == bar; // false //然后，如果你的 foo 和 bar 的值在 127 和 -128 之间（包括） //那么，其行为则改变了： Integer foo = 42; Integer bar = 42; foo &lt;= bar; // true foo &gt;= bar; // true foo == bar; // true Integer foo = 1000; Integer bar = 1000; foo &lt;= bar; // true foo &gt;= bar; // true foo == bar; // false //然后，如果你的 foo 和 bar 的值在 127 和 -128 之间（包括） //那么，其行为则改变了： Integer foo = 42; Integer bar = 42; foo &lt;= bar; // true foo &gt;= bar; // true foo == bar; // true\[/code] 为什么会这样呢？你需要了解一下Java Interger Cache，下面是相关的程序，注意其中的注释 \[code="java"] /\** * Returns a `Integer` instance representing the specified * `int` value. * If a new `Integer` instance is not required, this method * should generally be used in preference to the constructor * [{@link](mailto:{@link) #Integer(int)}, as this method is likely to yield * significantly better space and time performance by caching * frequently requested values. * * @param i an `int` value. * @return a `Integer` instance representing `i`. * @since 1.5 \*/ public static Integer valueOf(int i) { if(i &gt;= -128 && i &lt;= IntegerCache.high) return IntegerCache.cache\[i + 128]; else return new Integer(i); }\[/code] \[b]5、Perl的那些奇怪的变量\[/b] \[code="Perl"] $. $_ $\_# $$ $\[ @_ \[/code] 其所有的这些怪异的变量请参看：\[url]http://www.kichwa.com/quik\_ref/spec\_variables.html\[/url] \[b]6、Java的异常返回\[/b] 请看下面这段程序，你觉得其返回true还是false？ \[code="java"]try { return true; } finally { return false; } try { return true; } finally { return false; }\[/code] 在 javascript 和python下，其行为和Java的是一样的。 \[b]7、C语言中的Duff device\[/b] 下面的这段程序你能看得懂吗？这就是所谓的Duff Device，相当的怪异。 \[code="C"]void duff\_memcpy( char* to, char* from, size\_t count ) { size\_t n = (count+7)/8; switch( count%8 ) { case 0: do{ \*to++ = \*from++; case 7: \*to++ = \*from++; case 6: \*to++ = \*from++; case 5: \*to++ = \*from++; case 4: \*to++ = \*from++; case 3: \*to++ = \*from++; case 2: \*to++ = \*from++; case 1: \*to++ = \*from++; }while(--n&gt;0); } } void duff\_memcpy( char* to, char* from, size\_t count ) { size\_t n = (count+7)/8; switch( count%8 ) { case 0: do{ \*to++ = \*from++; case 7: \*to++ = \*from++; case 6: \*to++ = \*from++; case 5: \*to++ = \*from++; case 4: \*to++ = \*from++; case 3: \*to++ = \*from++; case 2: \*to++ = \*from++; case 1: \*to++ = \*from++; }while(--n&gt;0); } } \[/code] \[b]8、PHP中的字符串当函数用\[/b] PHP中的某些用法也是很怪异的 \[code="Php"]$x = "foo"; function foo(){ echo "wtf"; } $x(); $x = "foo"; function foo(){ echo "wtf"; } $x();\[/code] \[b]9、在C++中，你可以使用空指针调用静态函数\[/b] \[code="Cpp"]class Foo { public: static void bar() { std::cout &lt;&lt; "bar()" &lt;&lt; std::endl; } }; class Foo { public: static void bar() { std::cout &lt;&lt; "bar()" &lt;&lt; std::endl; } };\[/code]