groovy與java比較
默認導入如下包html
- java.io.*
- java.lang.*
- java.math.BigDecimal
- java.math.BigInteger
- java.net.*
- java.util.*
- groovy.lang.*
- groovy.util.*
常見錯誤java
- ==表明equals()
- in是一個關鍵字
- 聲明數組用[ ]
- for (int i=0; i < len; i++) {...}通常用如下簡寫
- for (i in 0..len-1) {...}
- for (i in 0..<len) {...}
- len.times {...}
注意事項express
- 分號是可選的。多條語句寫在一行時必須用分號
- return關鍵字是可選的
- static methods內必須使用this關鍵字
- 方法和類默認是pubilc
- protected同java
- 不支持內部類,可用閉包(closures)代替
- 不會檢查方法throws的異常,there is no difference between checked and unchecked exceptions.
- 沒有定義的成員和類型不編譯是不會報錯,參見Runtime vs Compile time, Static vs Dynamic.
New features added to Groovy not available in Java
- Closures
- native syntax for lists and maps
- GroovyMarkup and GPath support
- native support for regular expressions
- polymorphic iteration and powerful switch statement
- dynamic and static typing is supported - so you can omit the type declarations on methods, fields and variables
- you can embed expressions inside strings
- lots of new helper methods added to the JDK
- simpler syntax for writing beans for both properties and adding event listeners
- safe navigation using the ?. operator, e.g. "variable?.field" and "variable?.method()" - no more nested ifs to check for null clogging up your code
歡迎關注本站公眾號,獲取更多信息