這些年來我陸陸續(xù)續(xù)已經(jīng)學(xué)習(xí)了六種編程語言,有些人會說語言學(xué)到最后不都差不多嗎?其實(shí)可以這樣講,也可以不這樣講。雖然每種語言的表達(dá)能力大部分是重合的,只是語法表現(xiàn)形式不一樣,但是由于歷史發(fā)展的原因,每種語言形成了自己的支撐環(huán)境,所以都有其主要的適用范圍。 C、C++、Python和Java四種是通用編程語言,JavaScript和PHP算是Web環(huán)境的專用編程語言。C語言由于其底層操作特性和歷史的積累,在嵌入式領(lǐng)域是當(dāng)之無愧的王者;C++是一種支持最廣泛編程范式的復(fù)雜語言,這些年來發(fā)展不太好,目前在服務(wù)器后臺和游戲領(lǐng)域還有其一席之地;Python作為一種靈活的輕便的通用型腳本語言,使用范圍比較廣,從應(yīng)用軟件到Web開發(fā)都有它的身影,由于其解釋語言的特點(diǎn),比較適合輕量級或原型開發(fā);JavaScript語言由于其是瀏覽器內(nèi)置的腳本語言,是Web前端開發(fā)的主流,近年來由于google的V8引擎開源,出現(xiàn)了Node.js之類JavaScript后臺開發(fā)框架,把JavaScript的應(yīng)用領(lǐng)域擴(kuò)展到了Web后臺;PHP作為一種簡單的Web服務(wù)器后臺腳本語言,在全世界范圍內(nèi)的網(wǎng)站上有最大的使用率;Java由于其跨平臺可移植性,在Web開發(fā)領(lǐng)域大放異彩,特別是在企業(yè)級Web開發(fā),同時(shí)由于Android系統(tǒng)采用Java來開發(fā)應(yīng)用程序,所以也隨著Android的發(fā)展而應(yīng)用越發(fā)廣泛。 理清不同語言間主要語法特性的差異,才能更好的在合適的領(lǐng)域或場景下去應(yīng)用合適的編程語言,以滿足我們所面對的需求。這六種語言都是從C語言發(fā)展而來,所以它們的語法都比較像C語言,下面我就主要語法特性對各個(gè)語言做一個(gè)對比。 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++都用特有的預(yù)定義宏,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; 分析:這個(gè)最基本的都支持了。 3、函數(shù)定義 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){} } 分析:這個(gè)也是最基本的了,只是Java比較特殊,不支持定義類之外的函數(shù)。 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由于是傳統(tǒng)面向過程的語言不支持類,其他的都支持了,只是JavaScript的類模型比較特殊,把函數(shù)作為類來使用。 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一個(gè)對象。 6、數(shù)組定義 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}; 分析:數(shù)組是語言的基本特性,都支持了,只是PHP通過類似函數(shù)調(diào)用的語法來完成。 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、循環(huán)語句 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++){} 分析:這個(gè)也是基本的語句,都支持了。 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算是循環(huán)語句的一個(gè)變種,在操作順序容器的時(shí)候非常有用,可以看到C和C++不支持,其它的都語言內(nèi)置支持了。 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); 分析:打印算是語言所運(yùn)行環(huán)境的支持庫功能,除了JavaScript外都支持了,因?yàn)镴avaScript主要使用來操控DOM樹的,沒有自己的輸出窗口所以也沒必要支持。 11、字符串定義 C:char test[] = {"helloworld"}; C++:String test = "helloworld"; Python:test = "helloworld" JavaScript:var test = "helloworld"; PHP:$test = "helloworld"; Java:String test = "helloworld"; 分析:這個(gè)都支持了,其中C++、Java都是用標(biāo)準(zhǔn)庫來現(xiàn)實(shí)的。 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是用標(biāo)準(zhǔn)庫函數(shù)來實(shí)現(xiàn),其它都是語言內(nèi)置支持了。 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是語言內(nèi)置支持,其他的都依靠庫來完成。 14、字符串正則表達(dá)式 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++不支持,其他都有標(biāo)準(zhǔn)庫來支持。 15、內(nèi)置容器類型 C:數(shù)組 C++:數(shù)組 順序容器 Vector 關(guān)聯(lián)容器 Pair MapSet Python:列表/元組 字典 JavaScript:數(shù)組 對象 PHP:數(shù)組(含關(guān)聯(lián)數(shù)組) Java:數(shù)組 序列 Collection 映射表 Map 分析:C最簡單只支持?jǐn)?shù)組,其他都支持容器,不過主要還是順序容器和關(guān)聯(lián)容器兩大類。 16、注釋方式 C:/* */ C++:// Python:# JavaScript:/* */ // PHP:/* */ // # Java:/* */ // 分析:大概就/**/、//、#三種方式,各自支持情況不一。 17、多線程支持 C:支持 C++:支持 Python:支持 JavaScript:不支持 PHP:不支持 Java:支持 分析:四種通用編程語言都支持了,兩種專用編程語言都不支持。 18、socket支持 C:支持 C++:支持 Python:支持 JavaScript:不支持 PHP:支持 Java:支持 分析:除了JavaScript以外都支持,這也是JavaScript的應(yīng)用領(lǐng)域限制所決定的。 19、垃圾回收機(jī)制 C:不支持 C++:不支持 Python:支持 JavaScript:支持 PHP:支持 Java:支持 分析:這是現(xiàn)代語言的重要機(jī)制,C和C++不支持,其他的都支持了。 20、引入其他文件中的函數(shù) 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用自己的函數(shù)調(diào)用。 21、將字符串作為指令執(zhí)行 C:不支持 C++:不支持 Python:eval("port=5060") JavaScript:eval("port=5060;"); PHP:eval("port=5060;"); Java:Porcess proc = new ProcessBuilder(“test”).start(); 分析:很有用的一個(gè)動態(tài)語言特性,C和C++都不支持,Java要類庫來支持,其它的語言內(nèi)置eval關(guān)鍵字來支持。 (完)
|
|