5. ARM blocks
ARM (Automatic Resource Management) block from Java 7 are not supported in Groovy. Instead,Groovy provides various methods relying on closures,which have the same effect while being more idiomatic. For example:
Path file Paths.get("/path/to/file");Charset charset CharsetforName"UTF-8"tryBufferedReader reader FilesnewBufferedReaderfile charset)) linewhile((line readerreadLine())!=nullSystemoutprintlnlinecatchIOException eprintStackTrace();}
can be written like this:
).eachLine'UTF-8' println it }
or,if you want a version closer to Java:
6. Inner classes
|
The implementation of anonymous inner classes and nested classes follows the Java lead,but you should not take out the Java Language Spec and keep shaking the head about things that are different. The implementation done looks much like what we do for?groovy.lang.Closure ,with some benefits and some differences. Accessing private fields and methods for example can become a problem,but on the other hand local variables don’t have to be final. |
6.1. Static inner classes
Here’s an example of static inner class:
import javautilconcurrent.CountDownLatchTimeUnit called CountDownLatchTimer timer Timer() timerschedule(TimerTaskvoid run calledcountDown},102)">0assertawait10TimeUnitSECONDS)
6.3. Creating Instances of Non-Static Inner Classes
y.new X()?syntax. Instead,you have to write?new X(y)
,like in the code below:
7. Lambdas
Java 8 supports lambdas and method references:
println 'run'each // or list.each(this.&println)
8. GStrings
As double-quoted string literals are interpreted as?GString
?values,Groovy may fail with compile error or produce subtly different code if a class with?String
?literal containing a dollar character is compiled with Groovy and Java compiler.
While typically,Groovy will auto-cast between?GString
?and?String
?if an API declares the type of a parameter,beware of Java APIs that accept an?Object
?parameter and then check the actual type.
9. String and Character literals
Singly-quoted literals in Groovy are used for?String
?or?GString
,depending whether there is interpolation in the literal.
getClass()=="c""c${1}"inGString
Groovy will automatically cast a single-character?String
?to?char
?only when assigning to a variable of type?char
. When calling methods with arguments of type?char
?we need to either cast explicitly or make sure the value has been cast in advance.
Characterdigita16)==10:'But Groovy does boxing'((char'a'false'Need explicit cast'catchMissingMethodException}
Groovy supports two styles of casting and in the case of casting to?char
?there are subtle differences when casting a multi-char strings. The Groovy style cast is more lenient and will take the first character,while the C-style cast will fail with exception.
// for single char strings,both are the same).class==Character"c"as// for multi char strings they are not'cx'=='c''will fail - not castable'GroovyCastException'cx'asType'c'
10. Primitives and wrappers
Because Groovy uses Objects for everything,it?autowraps?references to primitives. Because of this,it does not follow Java’s behavior of widening taking priority over boxing. Here’s an example using?int
11. Behaviour of?==
In Java?==
?means equality of primitive types or identity for objects. In Groovy?==
?translates to?a.compareTo(b)==0
,if they are?Comparable
,and?a.equals(b)
?otherwise. To check for identity,there is?is
. E.g.?a.is(b)
.
12. Conversions
Java does automatic widening and narrowing?conversions.
Table 1. Java Conversions
? |
|
Converts from |
|
|
|
|
|
|
|
|
boolean |
|
|
|
|
|
|
|
|
byte |
|
|
|
|
|
|
|
|
short |
|
|
|
|
|
|
|
|
char |
|
|
|
|
|
|
|
|
int |
|
|
|
|
|
|
|
|
long |
|
|
|
|
|
|
|
|
float |
|
|
|
|
|
|
|
|
double |
|
|
|
|
|
|
|
|
*?'Y' indicates a conversion Java can make,'C' indicates a conversion Java can make when there is an explicit cast,'T` indicates a conversion Java can make but data is truncated,'N' indicates a conversion Java can’t make.
Groovy expands greatly on this.
Table 2. Groovy Conversions
? |
|
Converts from |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
boolean |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Boolean |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
byte |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Byte |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
short |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Short |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
char |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Character |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Integer |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
long |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Long |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BigInteger |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
float |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Float |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
double |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Double |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BigDecimal |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*?'Y' indicates a conversion Groovy can make,'D' indicates a conversion Groovy can make when compiled dynamically or explicitly cast,'T` indicates a conversion Groovy can make but data is truncated,'B' indicates a boxing/unboxing operation,'N' indicates a conversion Groovy can’t make.
The truncation uses?Groovy Truth?when converting to?boolean
/Boolean
. Converting from a number to a character casts the?Number.intvalue()
?to?char
. Groovy constructs?BigInteger
?and?BigDecimal
?using?Number.doubleValue()
?when converting from a?Float
?or?Double
,otherwise it constructs using?toString()
. Other conversions have their behavior defined by?java.lang.Number
.
There are a few more keywords in Groovy than in Java. Don’t use them for variable names etc.
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!