Java、Groovy、JavaScript、Python各语言对比
其他
2019-10-17 12:17:28
阅读次数: 0
Get start
Java
1 2 3 4 5
public class { public static void main (String[] args) { System.out.println("Hello, world." ); } }
Groovy
1 2 3
static void main(String[] args) { println('Hello, world.' ) }
JavaScript
1
console .log("Hello, world." );
Python
1 2
if __name__ == '__main__' : print('Hello, world.' )
字符串
Java
1 2 3 4 5 6 7 8 9 10 11 12 13
public class { public static void main (String[] args) { String a = "Hello" ; System.out.println(a.substring(0 , 3 )); System.out.println(String.valueOf(123 )); System.out.println(a + ", world." ); System.out.println(String.format("%s, I am %d." , a, 10 )); } }
Groovy
1 2 3 4 5 6 7 8 9 10 11
static void main(String[] args) { def a = "Hello" println(a[0. .3 ]) println(String.valueOf(123 )); println(a + ", world." ) println("${a}, I am ${10}." ) }
JavaScript
1 2 3 4 5 6 7 8 9
let a = "Hello" ;console .log(a.substr(0 , 3 ));console .log(String (123 ));console .log(a + ", world." );console .log(`${a} , I am ${10 } .` );
Python
1 2 3 4 5 6 7 8 9 10 11 12 13
if __name__ == '__main__' : a = "Hello" print(a[0 :3 ]) print(str(123 )) print(a + ", world." ) print("%s, I am %d." % (a, 10 )) print("{}, I am {}." .format(a, 10 ))
时间格式化
Java
1 2 3 4 5 6 7 8 9 10 11 12 13
public class { public static void main (String[] args) { long currentTimeMillis = System.currentTimeMillis(); System.out.println(currentTimeMillis); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss" ); LocalDateTime now = LocalDateTime.now(); String format = formatter.format(now); System.out.println(format); } }
Groovy
1 2 3 4 5 6 7 8 9 10 11
static void main(String[] args) { def currentTimeMillis = System.currentTimeMillis() println(currentTimeMillis) def formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss" ) def now = LocalDateTime.now() def format = formatter.format(now) println(format) }
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
let currentTimeMillis = Date .parse(new Date ());console .log(currentTimeMillis);Date .prototype.Format = function (fmt ) { let o = { "M+" : this .getMonth() + 1 , "d+" : this .getDate(), "h+" : this .getHours(), "m+" : this .getMinutes(), "s+" : this .getSeconds(), "q+" : Math .floor((this .getMonth() + 3 ) / 3 ), "S" : this .getMilliseconds() }; if (/(y+)/ .test(fmt)) fmt = fmt.replace(RegExp .$1 , (this .getFullYear() + "" ).substr(4 - RegExp .$1. length)); for (let k in o) if (new RegExp ("(" + k + ")" ).test(fmt)) fmt = fmt.replace(RegExp .$1 , (RegExp .$1. length === 1 ) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))); return fmt; }; let format = new Date ().Format("yyyy-MM-dd hh:mm:ss" );console .log(format);
Python
1 2 3 4 5 6 7 8 9 10
import timeimport datetimeif __name__ == '__main__' : print(int(round(time.time()))) format = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S' ) print(format)
函数
Java
1 2 3 4 5 6 7 8 9 10
public class { public static void main (String[] args) { int result = plus(1 , 2 ); System.out.println(result); } private static int plus (int a, int b) { return a + b; } }
Groovy
1 2 3 4 5 6 7 8 9
static void main(String[] args) { def result = plus 1 , 2 println(result) } static def plus(int a, int b) { return a + b }
JavaScript
1 2 3 4 5 6
function plus (a, b ) { return a + b; } let result = plus(1 , 2 );console .log(result);
Python
1 2 3 4 5 6
def plus (a=0 , b=0 ) : return a + b if __name__ == '__main__' : result = plus(1 , 2 ) print(result)
列表
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
public class { public static void main (String[] args) { List<String> list = new ArrayList<>(); list.add("a" ); list.add("b" ); list.add("c" ); list.remove("c" ); String first = list.get(0 ); List<String> added = Arrays.asList("m" , "n" ); 大专栏 Java、Groovy、JavaScript、Python各语言对比 list.addAll(added); for (String s : list) { System.out.println(s); } } }
Groovy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
static void main (String[] args) { def list = ['a' , 'b' ] list << 'c' list -= 'c' def first = list[0 ] def added = ['m' , 'n' ] list += added println(list) for (i in list) { println(i) } list.each { println(it) } }
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
let list = ['a' , 'b' ];list.push('c' ); list = list.filter(s => s !== 'c' ); let first = list[0 ];let added = ['m' , 'n' ];list = [...list, ...added]; console .log(list);for (let i = 0 ; i < list.length; i++) { console .log(list[i]); }
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
if __name__ == '__main__' : list = ['a' , 'b' ] list.append('c' ) del list[2 ] print(list) first = list[0 ] added = ['m' , 'n' ] list = list + added print(list) for i in list: print(i)
字典
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
public class Client { public static void main (String[] args) { Map<String, Integer> map = new HashMap<>(); map.put("a" , 1 ); map.put("b" , 2 ); map.put("c" , 3 ); map.remove("c" ); Integer first = map.get("a" ); Map<String, Integer> added = new HashMap<>(); added.put("m" , 4 ); added.put("n" , 5 ); map.putAll(added); map.forEach((key, value) -> { System.out.println(key + ": " + value); }); } }
Groovy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
static void main(String[] args) { def map = ['a' : 1 , 'b' : 2 ] map['c' ] = 3 map.remove('c' ) Integer first = map.get('a' ) def added = ['m' : 4 , 'n' : 5 ] map += added map.each { println(it.key + ': ' + it.value) } }
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
let map = {'a' : 1 , 'b' : 2 };map['c' ] = 3 ; delete map['c' ];let first = map['a' ];let added = {'m' : 4 , 'n' : 5 };map = {...map, ...added}; console .log(map);for (let key in map) { if (map.hasOwnProperty(key)) { console .log(`${key} : ${map[key]} ` ); } }
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
if __name__ == '__main__' : map = {'a' : 1 , 'b' : 2 } map['c' ] = 3 del map['c' ] first = map['a' ] added = {'m' : 4 , 'n' : 5 } map.update(added) print(map) for key, value in map.items(): print('%s: %s' % (key, value))
文件
Java
1 2 3 4 5 6 7
public class Client { public static void main (String[] args) throws IOException { File file = new File("/Users/puke/Desktop/TODO.md" ); String content = Files.readFile(file); System.out.println(content); } }
Groovy
1 2 3 4 5
static void main(String[] args) { def file = new File("/Users/puke/Desktop/TODO.md" ) def content = file.text println(content) }
Python
1 2 3 4
if __name__ == '__main__' : with open("/Users/puke/Desktop/TODO.md" ) as f: content = f.read() print(content)
网络
Java
1 2 3 4 5 6 7 8
public class Client { public static void main (String[] args) throws IOException { String url = "https://www.baidu.com" ; InputStream inputStream = new URL(url).openStream(); String content = Files.readFile(inputStream); System.out.println(content); } }
Groovy
1 2 3 4 5
static void main(String[] args) { def url = "https://www.baidu.com" def content = new URL(url).openStream().text println(content) }
Python
1 2 3 4 5 6 7
import urllib2if __name__ == '__main__' : url = "https://www.baidu.com" response = urllib2.urlopen(url) content = response.read() print(content)
转载自 www.cnblogs.com/wangziqiang123/p/11691146.html