這些年來我陸陸續續已經學習了六種編程語言,有些人會說語言學到最後不都差很少嗎?其實能夠這樣講,也能夠不這樣講。雖然每種語言的表達能力大部分是重合的,只是語法表現形式不同,可是因爲歷史發展的緣由,每種語言造成了本身的支撐環境,因此都有其主要的適用範圍。javascript
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的發展而應用愈加普遍。php
理清不一樣語言間主要語法特性的差別,才能更好的在合適的領域或場景下去應用合適的編程語言,以知足咱們所面對的需求。這六種語言都是從C語言發展而來,因此它們的語法都比較像C語言,下面我就主要語法特性對各個語言作一個對比。前端
一、常量定義java
C:#define TEST 0web
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語法,其它的都用定義不變變量的方式。
二、變量定義
C:int test = 0;
C++:int test = 0;
Python:test = 0
JavaScript:val test = 0;
PHP:$test = 0;
Java:int test = 0;
分析:這個最基本的都支持了。
三、函數定義
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比較特殊,不支持定義類以外的函數。
四、類定義(含繼承)
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的類模型比較特殊,把函數做爲類來使用。
五、對象定義
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一個對象。
六、數組定義
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經過相似函數調用的語法來完成。
七、條件語句
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{}
分析:這是最基本的語句,都支持了。
八、循環語句
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++){}
分析:這個也是基本的語句,都支持了。
九、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++不支持,其它的都語言內置支持了。
十、打印語句
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樹的,沒有本身的輸出窗口因此也不必支持。
十一、字符串定義
C:char test[] = {"helloworld"};
C++:String test = "helloworld";
Python:test = "helloworld"
JavaScript:var test = "helloworld";
PHP:$test = "helloworld";
Java:String test = "helloworld";
分析:這個都支持了,其中C++、Java都是用標準庫來現實的。
十二、字符串串接
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是用標準庫函數來實現,其它都是語言內置支持了。
1三、字符串分割
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是語言內置支持,其餘的都依靠庫來完成。
1四、字符串正則表達式
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++不支持,其餘都有標準庫來支持。
1五、內置容器類型
C:數組
C++:數組
順序容器 Vector
關聯容器 Pair MapSet
Python:列表/元組
字典
JavaScript:數組
對象
PHP:數組(含關聯數組)
Java:數組
序列 Collection
映射表 Map
分析:C最簡單隻支持數組,其餘都支持容器,不過主要仍是順序容器和關聯容器兩大類。
1六、註釋方式
C:/* */
C++://
Python:#
JavaScript:/* */
//
PHP:/* */
//
#
Java:/* */
//
分析:大概就/**/、//、#三種方式,各自支持狀況不一。
1七、多線程支持
C:支持
C++:支持
Python:支持
JavaScript:不支持
PHP:不支持
Java:支持
分析:四種通用編程語言都支持了,兩種專用編程語言都不支持。
1八、socket支持
C:支持
C++:支持
Python:支持
JavaScript:不支持
PHP:支持
Java:支持
分析:除了JavaScript之外都支持,這也是JavaScript的應用領域限制所決定的。
1九、垃圾回收機制
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用本身的函數調用。
2一、將字符串做爲指令執行
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關鍵字來支持。
(完)