Groovy trIEs to be as natural as possible for Java developers. We’ve trIEd to follow the principle of least surprise when designing Groovy,particularly for developers learning Groovy who’ve come from a Java background.
Here we List all the major differences between Java and Groovy.
1. Default importsAll these packages and classes are imported by default,i.e. you do not have to use an explicit import
statement to use them:
java.io.*
java.lang.*
java.math.BigDecimal
java.math.BigInteger
java.net.*
java.util.*
groovy.lang.*
groovy.util.*
2. Multi-methodsIn Groovy,the methods which will be invoked are chosen at runtime. This is called runtime dispatch or multi-methods. It means that the method will be chosen based on the types of the arguments at runtime. In Java,this is the opposite: methods are chosen at compile time,based on the declared types.
The following code,written as Java code,can be compiled in both Java and Groovy,but it will behave differently:
int method(String arg) { return 1;}Object2 o = "Object" result (o);
In Java,you would have:
assertEquals(, result);
Whereas in Groovy:
o is declared as anObject
,whereas Groovy will choose at runtime,when the method is actually called. Since it is called with aString
,then theString
version is called. 3. Array initializersIn Groovy,the
{ … }
block is reserved for closures. That means that you cannot create array literals with this Syntax:[]
4. Package scope visibilityIn Groovy,omitting a modifIEr on a fIEld doesn’t result in a package-private fIEld like in Java:
@PackageScope: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:
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. @H_244_419@ 6.1. Static inner classesHere’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 ClassesIn Java you can do this:
y.new X() Syntax. Instead,you have to writenew X(y)
,like in the code below:Runnable run out"Run" ListforEach::);Java 8 lambdas can be more or less consIDered as anonymous inner classes. Groovy doesn’t support that Syntax,but has closures instead:
each // or List.each(this.&println)8. GStringsAs double-quoted string literals are interpreted as
GString
values,Groovy may fail with compile error or produce subtly different code if a class withString
literal containing a dollar character is compiled with Groovy and Java compiler.While typically,Groovy will auto-cast between
9. String and Character literalsGString
andString
if an API declares the type of a parameter,beware of Java APIs that accept anObject
parameter and then check the actual type.Singly-quoted literals in Groovy are used for
String
orGString
,depending whether there is interpolation in the literal.getClass()=="c""c"inGStringGroovy will automatically cast a single-character
String
tochar
only when assigning to a variable of typechar
. When calling methods with arguments of typechar
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'@H_419_792@ 10. Primitives and wrappersBecause 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
==In Java
12. Conversions==
means equality of primitive types or IDentity for objects. In Groovy==
translates toa.compareto(b)==0
,if they areComparable
,anda.equals(b)
otherwise. To check for IDentity,there isis
. E.g.a.is(b)
.Java does automatic wIDening and narrowing conversions.
Converts to | ||||||||||||||||||
Converts from | boolean | byte | short | char | int | long | float | double | ||||||||||
boolean | - | N | N | N | N | N | N | N | ||||||||||
byte | N | - | Y | C | Y | Y | Y | Y | ||||||||||
short | N | C | - | C | Y | Y | Y | Y | ||||||||||
char | N | C | C | - | Y | Y | Y | Y | ||||||||||
int | N | C | C | C | - | Y | T | Y | ||||||||||
long | N | C | C | C | C | - | T | T | ||||||||||
float | N | C | C | C | C | C | - | Y | ||||||||||
double | N | C | C | C | C | C | C | - |
Converts to | ||||||||||||||||||
Converts from | boolean | Boolean | byte | Byte | short | Short | char | Character | int | Integer | long | Long | BigInteger | float | float | double | Double | BigDecimal |
boolean | - | B | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N |
Boolean | B | - | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N |
byte | T | T | - | B | Y | Y | Y | D | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y |
Byte | T | T | B | - | Y | Y | Y | D | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y |
short | T | T | D | D | - | B | Y | D | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y |
Short | T | T | D | T | B | - | Y | D | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y |
char | T | T | Y | D | Y | D | - | D | Y | D | Y | D | D | Y | D | Y | D | D |
Character | T | T | D | D | D | D | D | - | D | D | D | D | D | D | D | D | D | D |
int | T | T | D | D | D | D | Y | D | - | B | Y | Y | Y | Y | Y | Y | Y | Y |
Integer | T | T | D | D | D | D | Y | D | B | - | Y | Y | Y | Y | Y | Y | Y | Y |
long | T | T | D | D | D | D | Y | D | D | D | - | B | Y | T | T | T | T | Y |
Long | T | T | D | D | D | T | Y | D | D | T | B | - | Y | T | T | T | T | Y |
BigInteger | T | T | D | D | D | D | D | D | D | D | D | D | - | D | D | D | D | T |
float | T | T | D | D | D | D | T | D | D | D | D | D | D | - | B | Y | Y | Y |
float | T | T | D | T | D | T | T | D | D | T | D | T | D | B | - | Y | Y | Y |
double | T | T | D | D | D | D | T | D | D | D | D | D | D | D | D | - | B | Y |
Double | T | T | D | T | D | T | T | D | D | T | D | T | D | D | T | B | - | Y |
BigDecimal | T | T | D | D | D | D | D | D | D | D | D | D | D | T | D | T | D | - |
评论列表(0条)