Java vs Python,谁能胜出?作为程序猿,这个问题除了从数据角度分析外,我们更应该从编程语言本身的使用和体验来感受对比一番,然后再谨慎的给出自己的答案。
Java诞生于1995年,Python诞生于1991年,是不是很意外,大家以为Java廉颇老矣,Python年纪却更大。
Java之父James Gosling
python之父Guido van Rossum
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.
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在人工智能快速崛起,各有各的好,非要说谁胜出,当然是我们程序猿自己了,理解编程语言的共性,拥抱编程语言的多样性,让我们可以快速在不同场景做出合适的选择,创造属于程序猿的未来。