Java vs Python,誰能勝出?
Java vs Python,誰能勝出?作為程式猿,這個問題除了從資料角度分析外,我們更應該從程式語言本身的使用和體驗來感受對比一番,然後再謹慎的給出自己的答案。
資歷
Java誕生於1995年,Python誕生於1991年,是不是很意外,大家以為Java廉頗老矣,Python年紀卻更大。
毛髮的PK
Java之父James Gosling

python之父Guido van Rossum

Hello World
Java版
public class HelloWorld { public static void main(String[] args) { System.out.println("hello java world"); } }
Python版
print("hello python world")
Java說:Hi, code boy.
- 先定義一個類
- 再定義一個main入口方法
- 然後列印輸出hello world
Python說:Hi, code boy. Just hello world!
字串
要說程式碼中最醜的字串是什麼,那必須是sql。
Java中的sql
String uglySql = "SELECT * FROM" + " program_language" + " WHERE" + " NAME = 'Java'" + " AND rank > 5";
Python中的sql
beautiful_sql = """ SELECT * FROM program_language WHERE NAME = 'Python' AND rank > 5 """
列表
鋼鐵俠:Hi,賈維斯,給我一份最新的世界電影票房排名前10的榜單
Java版賈維斯
List<String> top10 = Arrays.asList("Avatar", "Titanic", "Star Wars: The Force Awakens", "Avengers: Infinity War", "Jurassic World", "Marvel's The Avengers", "Furious 7", "Avengers: Age of Ultron", "Black Panther", "Avengers: Endgame");
Python版賈維斯
top10 = [ "Avatar", "Titanic", "Star Wars: The Force Awakens", "Avengers: Infinity War", "Jurassic World", "Marvel's The Avengers", "Furious 7", "Avengers: Age of Ultron", "Black Panther", "Avengers: Endgame" ]
鋼鐵俠:Ok,賈維斯,well done,現在給我排名前3的就行了,我比較關注
Java版賈維斯
List<String> top3 = top10.subList(0,3);
Python版賈維斯
top3 = top10[:3]
對映/字典
英文不是很好,上面的榜單誰幫忙翻譯一下?
Java
Map<String, String> top10Map = new HashMap<>(); top10Map.put("Avatar", "阿凡達"); top10Map.put("Titanic", "泰坦尼克號"); top10Map.put("Star Wars: The Force Awakens", "星球大戰:原力覺醒"); top10Map.put("Avengers: Infinity War", "復仇者聯盟:無限戰爭"); top10Map.put("Jurassic World", "侏羅紀世界"); top10Map.put("Marvel's The Avengers", "復仇者聯盟"); top10Map.put("Furious 7", "速度與激情7"); top10Map.put("Avengers: Age of Ultron", "復仇者聯盟:奧創紀元"); top10Map.put("Black Panther", "黑豹"); top10Map.put("Avengers: Endgame", "復仇者聯盟:終局之戰"); String movie_en_name = "Avengers: Endgame"; System.out.println(movie_en_name + "->" + top10Map.get(movie_en_name));
Python
top10_dic = { "Avatar": "阿凡達", "Titanic": "泰坦尼克號", "Star Wars: The Force Awakens": "星球大戰:原力覺醒", "Avengers: Infinity War": "復仇者聯盟:無限戰爭", "Jurassic World": "侏羅紀世界", "Marvel's The Avengers": "復仇者聯盟", "Furious 7": "速度與激情7", "Avengers: Age of Ultron": "復仇者聯盟:奧創紀元", "Black Panther": "黑豹", "Avengers: Endgame": "復仇者聯盟:終局之戰" } movie_en_name = "Avengers: Endgame" print(movie_en_name, "->", top10_dic[movie_en_name])
分支
假如可以回到過去,你會做什麼?
Java
Calendar timeMachine = Calendar.getInstance(); int year = timeMachine.get(Calendar.YEAR); if (year >= 1980 && year < 1990) { System.out.println("玩"); } else if (year >= 1990 && year < 2000) { System.out.println("學習"); } else if (year >= 2000 && year < 2010) { System.out.println("學習,工作"); } else { System.out.println("工作,成家,一胎,二胎"); }
Python
time_machine = datetime.datetime.now() year = time_machine.year if 1980 <= year < 1990: print("玩") elif 1990 <= year < 2000: print("學習") elif 2000 <= year < 2010: print("學習,工作") else: print("工作,成家,一胎,二胎")
迴圈
90年代,當我們還小時,被罰抄書了,多麼不幸的事,誰來拯救?
Java
try { FileReader fileReader = new FileReader("book.txt"); BufferedReader bookReader = new BufferedReader(fileReader); String line; while ((line = bookReader.readLine()) != null) { System.out.println(line); } fileReader.close(); bookReader.close(); } catch (IOException e) { e.printStackTrace(); }
Python
for line in open("book.txt"): print(line)
方法/函式
在鋼鐵俠多次要求取世界電影票房排名前10的榜單,和前3後,賈維斯進化了。
Java版賈維斯
private String fetchHtmlFromMojo() { // fetch html... String html = "<html></html>"; return html; } private List<String> parseTop100(String html) { // parse html... List<String> top100 = new ArrayList<>(); return top100; } private List<String> getTopN(int num) { String html = fetchHtmlFromMojo(); List<String> top100 = parseTop100(html); return top100.subList(0, num); }
Python版賈維斯
def fetch_html_from_mojo(): # fetch html... html = "<html></html>" return html def parse_top100(html): # parse html... top100 = [] return top100 def get_topn(num): html = fetch_html_from_mojo() top100 = parse_top100(html) return top100[:num]
類
終極賈維斯
Java
public class Robot { private String name = "賈維斯"; private Map<String, String> movieMap = new HashMap<>(); public Robot(String name) { this.name = name; } private String fetchHtmlFromMojo() { // fetch html... String html = "<html></html>"; return html; } private List<String> parseTop100(String html) { // parse html... List<String> top100 = new ArrayList<>(); return top100; } public List<String> getTopN(int num) { String html = fetchHtmlFromMojo(); List<String> top100 = parseTop100(html); return top100.subList(0, num); } public String translate(String movieEnName) { return movieMap.get(movieEnName); } }
Python
class Robot: name = "賈維斯" movie_dic = dict() def __init__(self, name): self.name = name def fetch_html_from_mojo(self): # fetch html... html = "<html></html>" return html def parse_top100(self, html): # parse html... top100 = [] return top100 def get_topn(self, num): html = self.fetch_html_from_mojo() top100 = self.parse_top100(html) return top100[:num] def translate(self, movie_en_name): return self.movie_dic[movie_en_name]
結論
Java vs Python,誰勝出?說實話,顏值方面Python確實高過Java很多(語法簡潔,作者頭髮濃密),但是,Java在網際網路攻城掠地,Python在人工智慧快速崛起,各有各的好,非要說誰勝出,當然是我們程式猿自己了,理解程式語言的共性,擁抱程式語言的多樣性,讓我們可以快速在不同場景做出合適的選擇,創造屬於程式猿的未來。