The similarities between groovy and Java are as follows:
3 +, 4 +, 6 +, 8 +, 10 +, 12 +, 13, 14, 15, 18 +, 20 +, 21, 22, 23, 28 +, 29 +, 30 +, 31 +, 32 +
+ It indicates that groovy not only covers Java syntax, but also enhancements.
The differences between groovy and Java are as follows:
0, 1, 2, 5, 7, 9, 11, 16, 17, 19, 24, 25, 26, 27
The sequence numbers listed below are in no particular order:
0. In groovy, Def can be used to define non-type variables (similar to VaR in Javascript in defining variables), and methods that return values are non-type without def in Java.
GROOVY:
Class man {def name = "mountain boy" def introduce () {return "I'm $ name" // return can be omitted }}
1. The equals method in Java corresponds to = in groovy, And the = (to determine whether to reference the same object) in Java corresponds to the is method in groovy.
Eg. test1.java:
Public class test {public static void main (string [] ARGs) {string name1 = "Mountain Wind boy"; string name2 = new string ("Mountain Wind boy "); // write name1 = name2 in groovy if (name1.equals (name2) {system. out. println ("Equal");} else {system. out. println ("not equal");} // write name1.is (name2) if (name1 = name2) {system. out. println ("identical");} else {system. out. println ("not identical ");}}
Corresponds to test1.java.
Test1.groovy:
String name1 = "Mountain Wind boy" // You can also write: def name1 = "Mountain Wind boy"
Def can be understood as varstring name2 = new string ("Mountain Wind kid") of the variable defined in Javascript. // this parameter is optional when the semicolon at the end of groovy,
If you want to use name1.equals (name2) if (name1 = name2) {system. out. println ("Equal");} else {system. out. println ("not equal");} // write name1 = name2if (name1.is (name2) {system. out. println ("identical");} else {system. out. println ("not identical ");}
2. Define the array in Java int [] A = {1, 2, 3}; write int [] A = [1, 2, 3] in groovy
3. in Java, the for loop for (INT I = 0; I <Len; I ++) can also be written as for (I in 0 .. len-1) or for (I in 0 ..
Java:
for (int i =0; i < len; i++) { // do something}
GROOVY:
For (INT I = 0; I <Len; I ++) {// do something} // or for (I in 0 .. len-1) {// do something} // or for (I in 0 ..
4. in Java, return is written as return; or return OBJ; in groovy, return is optional.
Java:
Public String sayhello () {return "Hello, mountain wind boy ";}
GROOVY:
Public String sayhello () {return "Hello, mountain wind boy"} // or Public String sayhello () {"Hello, mountain wind boy"} // or string sayhello () {"Hello, mountain wind boy"} // or public sayhello () {"Hello, mountain wind boy"} // Or Def sayhello () {"Hello, mountain wind boy "}
5. The inner class in Java is an internal class, which is implemented using closure in groovy (closure is a feature that Java 7 is considering and is more refined than the inner class in terms of semantics ).
6. Comments in groovy have more comments than Java #!, Others are the same as Java, such as single-line comment: // multi-line comment:/**/or/***/that supports javadoc /***/
Java:
/** Multi-line comment * // *** javadoc comment * // single-line comment}
GROOVY:
#! The first line of comment enables the Unix shell to locate the groovy startup program to run groovy code, such #! /Usr/bin/groovy/** multiline comment * // *** javadoc comment * // single line comment}
7. For-each: For (type T: iteratable) in Java 5 in groovy, for (T in iteratable)
Java:
for (Type t : iterable) { // do something}
GROOVY:
for (t in iterable) { // do something}
8. Switch statements in groovy are the same as those in Java, but more types are supported, such as string.
9. the while statements of groovy are the same as those of Java, but do-while statements are discarded (taking into account semantic issues, and do-while statements can be replaced by other forms of loop statements with a low usage frequency ).
10. The String constant in Java is represented as "Hello, mountain wind boy", which can be expressed as follows in GROOVY:
// Double quotation marks "Hello, Shan Feng boy" // single quotation marks can also be 'hello, Shan Feng kid' // multi-line string "Hello, mountain Wind kiddies "" // or '''hello, mountain wind kids' ''' // replace string def name = "Mountain Wind kiddies" "Hello, $" // or "hello, $ name"
11. classes are defined in groovy. The definition method is the same as the definition class in Java. The only difference is that classes in groovy, attributes and methods are both public by default, while classes are package by default in Java. In addition, def can be used to define methods in groovy. Please refer to the annotations.
Java:
Public class Hello {private string name = "Mountain Wind kiddies"; Public void sayhello () {system. Out. println ("hello," + name );}}
GROOVY:
Class Hello {private string name = "Mountain Wind kid" Public void sayhello () {// println and Java System. out. println () is the same as println "Hello, $ name"}/* sayhello. You can also define def sayhello () {println "Hello, $ name "}*/}
12. The object is created in Java and written as thought t = New Thought (); it can also be written in groovy, but there is also another way: def T = New Thought ();
13. static method calls are the same in Java and groovy, that is, classname. staticmethodname ();
14. Groovy also has the same implementation interface and inheritance parent class as Java, that is, implementation Interface Class classname implements interfacename
Inherited parent class: Class classname extends superclass
15. Groovy and Java are identical in defining interfaces, that is, interface interfacename // in groovy, the default value is public.
16. Regular Expression constants are not in Java and are expressed as/pattern/in groovy/
17. hash constant (type: Java. util. hashmap) does not exist in Java. It is expressed as def frequence = ["The": 5, "hello": 2, "world": 2] in groovy.
18. class variables are static variables. Groovy is the same as Java. Static string name = "Mountain Wind kiddies" can also be written as static name = "Mountain Wind kiddies" in groovy"
19. In the varargs method, groovy is slightly different from Java, as shown below:
Java:
// Java: public void varargsMethod(Type args) { //do something}
GROOVY:
def varargsMethod(Type[] args) { //do something}
20. reference the current object. Groovy and Java are the same. This is used in Java and this is also used in groovy. In groovy, this can appear in the static range, the class object pointing to the class. In this example, this is equivalent to thisinstaticscope. class (written in Java) or thisinstaticscope (written in groovy)
Class thisinstaticscope {static {println this} // do not be surprised. The parameter type can be omitted.
If there are modifier keywords such as public, synchronized, static in the method declaration, the return value type can be omitted. Static main (ARGs) {println this }}
21. The sub-class calls the parent class method. Groovy and Java are also the same. In Java, super. methodname (), in groovy, super. methodname ()
22. namespace definition. Groovy is the same as Java. in Java, package edu. ecust. bluesun; in groovy, package edu. ecust. bluesun (Semicolons can be omitted)
23. In terms of the import class, groovy is the same as Java. in Java, import edu. ecust. bluesun. policytest; in groovy, import edu. ecust. bluesun. policytest
24. list constant (type: Java. util. arraylist) is not in Java. In groovy, Def list = [3, 11, "hello", "Mountain Wind boy ","! "]
25. In terms of exception handling, groovy is the same as Java, except that it does not force the programmer to capture the check exception (checked exception) (this is similar to C #, if I remember correctly :)
In addition, the throws statement can be left empty when the method is declared.
26. The default parameter of the method, which is not in Java, is as follows in GROOVY:
Class Hello {// if no parameter is input, the hello is printed by default. mountain boy def greet (name = "mountain boy") {println ("Hello, $ name ") // brackets () can also be omitted ()}}
27. In groovy, if a statement occupies a single line, the semicolon (;) at the end of the sentence can be omitted. in Java, each statement must be followed by a semicolon (;)
28. in groovy, if it is not of the Boolean OR boolean type, the values of non-null or non-null (Null String, [], [:]) are true, and null is false, objects in Java cannot be true or false. Objects of the Boolean OR boolean type are the same as those in Java.
29. In groovy, everything is an object! This is not the case in Java. The basic type is not an object.
30. In Java, class objects are represented as classname. class, while in groovy, class objects can be directly represented using classname.
31. groovy automatically imports Java. lang. *, Java. util. *, java.net. *, Java. io. *, Java. math. biginteger, Java. math. bigdecimal, groovy. lang. *, groovy. util. *, while Java only automatically imports Java. lang. *
32. What about groovy? : Ternary operator, and? : Two-element operator, but Java only has? : Ternary operator.
GROOVY:
Def A = NULL; // if A is null, the result is? : The value that follows; if it is not null, the result is adef result =? : "Default result" println resulta = "Mountain Wind kid" result =? : "Default result" println result
It can be seen from the above that groovy is almost fully compatible with the Java syntax. It is no wonder that 'jiangnan white yi' is called groovy as a 'illegitimate child 'of Java. But because groovy not only draws on the features of Java 95% and above, it also draws on many excellent dynamic languages, such as Python and Ruby, to make groovy an extremely efficient and Agile programming language, not just a copy of Java. Therefore, Java ++ can be used as an alias for groovy, that is, Java with dynamic features.
Finally, I would like to add one sentence: groovy's features are far from listed, such as mixins, builder series: markupbuilder, swingbuilder, and so on. Many of them are in groovy but not in Java, so I will not list them one by one, I want to continue to learn groovy, you can visit the official groovy Website: http://groovy.codehaus.org, there are a lot of examples and tutorials for your reference, you can also refer to the groovy efficient programming series below (some of them are next notes ). You can also download the groovy in action ebook on the Internet.
References
Differences from Java: http://groovy.codehaus.org/Differences+from+Java
From: http://developer.51cto.com/art/200709/56818_2.htm