這些年來我陸陸續續已經學習了六種程式設計語言,有些人會說語言學到最後不都差不多嗎。其實可以這樣講,也可以不這樣講。雖然每種語言的表達能力大部分是重合的,只是文法表現形式不一樣,但是由於曆史發展的原因,每種語言形成了自己的支撐環境,所以都有其主要的適用範圍。
C、C++、Python和Java四種是通用程式設計語言,JavaScript和PHP算是Web環境的專用程式設計語言。C語言由於其底層操作特性和曆史的積累,在嵌入式領域是當之無愧的王者;C++是一種支援最廣泛編程範式的複雜語言,這些年來發展不太好,目前在伺服器後台和遊戲領域還有其一席之地;Python作為一種靈活的輕便的通用型指令碼語言,使用範圍比較廣,從應用軟體到Web開發都有它的身影,由於其解釋語言的特點,比較適合輕量級或原型開發;JavaScript語言由於其是瀏覽器內建的指令碼語言,是Web前端開發的主流,近年來由於google的V8引擎開源,出現了Node.js之類JavaScript後台開發架構,把JavaScript的應用領域擴充到了Web後台;PHP作為一種簡單的Web伺服器後台指令碼語言,在全世界範圍內的網站上有最大的使用率;Java由於其跨平台可移植性,在Web開發領域大放異彩,特別是在企業級Web開發,同時由於Android系統採用Java來開發應用程式,所以也隨著Android的發展而應用越發廣泛。
理清不同語言間主要文法特性的差異,才能更好的在合適的領域或情境下去應用合適的程式設計語言,以滿足我們所面對的需求。這六種語言都是從C語言發展而來,所以它們的文法都比較像C語言,下面我就主要文法特性對各個語言做一個對比。
1、常量定義
C:#define TEST 0
C++:#define TEST 0
或者
const test = 0;
Python:test = 0
JavaScript:不支援
PHP:define('test', 1);
Java:final int test = 0;
分析:JavaScript不支援常量,C、C++都用特有的預定義宏,PHP用特殊的define文法,其它的都用定義不變變數的方式。
2、變數定義
C:int test = 0;
C++:int test = 0;
Python:test = 0
JavaScript:val test = 0;
PHP:$test = 0;
Java:int test = 0;
分析:這個最基本的都支援了。
3、函數定義
C:int test(int param){}
C++:int test(int param){}
Python:def test(param):
JavaScript:function test(param){}
PHP:function test($param){}
Java:public class test{
public int test(int param){} }
分析:這個也是最基本的了,只是Java比較特殊,不支援定義類之外的函數。
4、類定義(含繼承)
C:不支援
C++:class test2: public test1{}
Python:class test2(test1):
JavaScript:function test2(){}
test2.prototype =inherit(test1.prototype){}
PHP:class test2 extend test1{}
Java:class test2 extends test1{}
分析:C由於是傳統面向過程的語言不支援類,其他的都支援了,只是JavaScript的類模型比較特殊,把函數作為類來使用。
5、對象定義
C:不支援
C++:test2 obj = new test2();
Python:obj = test2()
JavaScript:var obj = new test2();
PHP:$obj = new test2();
Java:test2 obj = new test2();
分析:除了C外其它語言都是通過new一個對象。
6、數組定義
C:int a[] = {1, 2, 3};
C++:int a[] = {1, 2, 3};
Python:a = [1, 2, 3]
JavaScript:var a = [1, 2, 3];
PHP:$a = array("1", "2", "3");
Java:int a[] = {1, 2, 3};
分析:數組是語言的基本特性,都支援了,只是PHP通過類似函數調用的文法來完成。
7、條件陳述式
C:if (test > 0){}
else if (test < 0){}
else{}
C++:if (test > 0){}
else if (test < 0){}
else{}
Python:if test > 0:
elif test < 0:
else:
JavaScript:if (test > 0){}
else if (test < 0){}
else{}
PHP:if ($test > 0){}
elseif ($test < 0){}
else{}
Java:if (test > 0){}
else if (test < 0){}
else{}
分析:這是最基本的語句,都支援了。
8、迴圈語句
C:for (idx=0; idx<num; idx++){}
C++:for (idx=0; idx<num; idx++){}
Python:for idx in range(1,10):
JavaScript:for (var idx=0; idx<num; idx++){}
PHP:for ($idx=0; $idx<$num; $idx++){}
Java:for (idx=0; idx<num; idx++){}
分析:這個也是基本的語句,都支援了。
9、foreach語句
C:不支援
C++:不支援
Python:for i in a:
或者
for key in d:
d[key]
JavaScript:for(i in a){}
PHP:foreach($a as $i){}
Java:for(int i : a){}
分析:foreach算是迴圈語句的一個變種,在操作順序容器的時候非常有用,可以看到C和C++不支援,其它的都語言內建支援了。
10、列印語句
C:printf("test: %d", val);
C++:cout<<"test: "<<val<<endl;
Python:print "test: "+val
JavaScript:不支援
PHP:echo "test: $val";
Java:System.out.println("test :"+val);
分析:列印算是語言所運行環境的支援庫功能,除了JavaScript外都支援了,因為JavaScript主要使用來操控DOM樹的,沒有自己的輸出視窗所以也沒必要支援。
11、字串定義
C:char test[] = {"helloworld"};
C++:String test = "helloworld";
Python:test = "helloworld"
JavaScript:var test = "helloworld";
PHP:$test = "helloworld";
Java:String test = "helloworld";
分析:這個都支援了,其中C++、Java都是用標準庫來現實的。
12、字串串接
C:test = strcat(test1, test2);
C++:test = test1 + test2;(STL庫)
Python:test = test1 + test2
JavaScript:var test = test1 + test2;
PHP:$test = $test1 .= $test2;
Java:test = test1 + test2;
分析:很有用的功能,除了C是用標準庫函數來實現,其它都是語言內建支援了。
13、字串分割
C:不支援
C++:test.substr(3, 8);
Python:test[3:8]
JavaScript:test.slice(3, 5);
PHP:substr($test, 3, 5);
Java:test.substring(3, 8);
分析:常用的功能,C不支援,Python是語言內建支援,其他的都依靠庫來完成。
14、字串Regex
C:不支援
C++:不支援
Python:test.replace("test1", "test2")
JavaScript:test.replace(/test1/gi, "test2");
PHP:str_replace($test, "test1", "test2");
Java:test.replaceAll("test1", "test2");
分析:常用的功能,可惜C、C++不支援,其他都有標準庫來支援。
15、內建容器類型
C:數組
C++:數組
順序容器 Vector
關聯容器 Pair MapSet
Python:列表/元組
字典
JavaScript:數組
對象
PHP:數組(含關聯陣列)
Java:數組
序列 Collection
映射表 Map
分析:C最簡單只支援數組,其他都支援容器,不過主要還是順序容器和關聯容器兩大類。
16、注釋方式
C:/* */
C++://
Python:#
JavaScript:/* */
//
PHP:/* */
//
#
Java:/* */
//
分析:大概就/**/、//、#三種方式,各自支援情況不一。
17、多線程支援
C:支援
C++:支援
Python:支援
JavaScript:不支援
PHP:不支援
Java:支援
分析:四種通用程式設計語言都支援了,兩種專用程式設計語言都不支援。
18、socket支援
C:支援
C++:支援
Python:支援
JavaScript:不支援
PHP:支援
Java:支援
分析:除了JavaScript以外都支援,這也是JavaScript的應用領域限制所決定的。
19、記憶體回收機制
C:不支援
C++:不支援
Python:支援
JavaScript:支援
PHP:支援
Java:支援
分析:這是現代語言的重要機制,C和C++不支援,其他的都支援了。
20、引入其他檔案中的函數
C:export int test();
C++:export int test();
Python:from test import *
JavaScript:<script language='javascript' src="test.js"charset="utf-8"></script>
PHP:require_once('test.php');
或者
include_once('test.php');
Java:import java.util.test.*;
分析:都支援,C和C++用export,Python和Java用import,JavaScript依靠HTML指令碼,PHP用自己的函數調用。
21、將字串作為指令執行
C:不支援
C++:不支援
Python:eval("port=5060")
JavaScript:eval("port=5060;");
PHP:eval("port=5060;");
Java:Porcess proc = new ProcessBuilder(“test”).start();
分析:很有用的一個動態語言特性,C和C++都不支援,Java要類庫來支援,其它的語言內建eval關鍵字來支援。
(完)