怦然心动,顺风车-漫威宇宙巡游,漫威英雄爱好者社区

导言

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 v柏子养心丸oid main(String[] args) {
System.out.println("hello java world");
}
}

Python版

print("hello python world")

Java说:Hi, code boy.

  • 先界说一个类
  • 再界说一个main进口办法
  • 然后打印输出h没有情人的情人节ello world

Python说:Hi, code boy. Just hello world!

字符串

要说代码中最丑的字符串是什么,那有必要是sql。

Java中的sql

String uglySql = "SELECT * FROM"
+ " progra神罗天征m_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 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 top3 = top10.subList(0,3);

Python版贾维斯

top3 = top10[:3]

映射/字典

英文不是很好,上面的榜单谁帮助翻译一下?

Java

Map top天龙八部3官网10Map =怦然心动,顺风车-漫威国际巡游,漫威英豪爱好者社区 new HashMap<>();
t退烧药有哪些op10Map.put("Avatar", "阿凡达");
top10Map.put("Titanic", "泰坦尼克号");
top10Map.put("Star Wars: The Force Awakens", "星球大战:原力觉悟");
top10Map.put("Avengers: Infinity War", "复仇者联盟:无限战役");
top1胡富国0Map.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 + "->" + top1潘玮楷0Map.get(movie_en_name));

Python

top10_dic = {
"Avatar": "阿凡达",
"Titanic": "泰坦尼克号",
"Star Wars: The F怦然心动,顺风车-漫威国际巡游,漫威英豪爱好者社区orce Awakens": "星球大战:原力觉悟",
"Avengers: Infinity War": "复仇者联盟:无限战役",
"Jurassic World": "侏罗纪国际",
"Marvel's The Avengers": "复仇者联盟",
"Furious 7": "速度与热情7",
"Avengers: Age of Ultron": "复仇者联盟:奥创recognize纪元",
"Black Panther": "黑豹",
"Avengers: Endgame": "复仇者联盟:结局之战"
}
movryonaie_en_name = "Avengers: Endgame"
print(movie_en_name, "->",dnf令郎 top10_dic[movie_en_name])

分支

假设能够回到曩昔,你会做西内琉奈什么?

Java

Ca汉汉lendar 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.o怦然心动,顺风车-漫威国际巡游,漫威英豪爱好者社区ut.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 = "";
return html;
}
private List parseTop100(String html) {
// parse html...
List top100 = new ArrayList<>();
return top100;
}
private List getTopN(int num) {
String html = fetchHtmlFromMojo();
List top100 = parseTop10末日坍塌0(html);
return top100.subList(0, num);
}

Python版贾维斯

def fetch_ht五官ml_from_mojo():
# fetch html...
html = ""
return html
def parse_top100(html):
# parse html...
top100 = []
return top100
d西伯利亚雪橇犬ef get_topn(num):
html = fe活死人拂晓tch_html_from_mojo()
top100 = parse_top100(html)
return top100[:num]

终极贾维斯

Java

public class Robot {
private String name = "贾维斯";
private Map movieMap = new HashMap<>();

public Robot(String name) {
this.name = name;
}
private String fetchHtmlFromMojo() {
// fetch html...
String html = "";
return html;
}
private List parseTop100(String html) {
// parse html...
List top100 = new ArrayList<>();
return top100;
}
public List getTopN(int num) {
String html = fetchHtmlFromMojo();
List top100 = 廖雪峰parseTop100(html);
return top100.subList(0, num);
}
public String translate(String movieEnName)怦然心动,顺风车-漫威国际巡游,漫威英豪爱好者社区 {
return movieMap.get(movieEnName);
}
}

Python

class Robot:
nam怦然心动,顺风车-漫威国际巡游,漫威英豪爱好者社区e = "贾维斯"
movie_dic = dict()
def __init__(self, name):
self.name = name
def fetch_html_from_mojo(self):
# fetch 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在人工智能快速兴起,各有各的好,非要说谁胜出,当然是咱们程序猿自己了,了解编程言语的共性,拥抱编程言语的多样性,让咱们能够快速在不同场景做出适宜的挑选,发明归于程序猿的未来。


 关键词: