Differences between Groovy and Java

Source: Internet
Author: User

1. In Groovy, def can be used to define non-type variables (which is similar to var in JavaScript in defining variables), and return a non-type method without def in Java.

2. 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.

3. Define the array in Java int [] a = {1, 2, 3}; write int [] a = [1, 2, 3] in Groovy

4. java for loop (int I = 0; I <len; I ++ ){...} in Groovy, you can also write for (I in 0 .. len-1 ){...} or for (I in 0 ..

5. In Java, return is written as return; or return obj; in Groovy, return is optional.

6. 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 better at semantics than the inner class)

7. Notes 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 /***/

8. for-each: for (Type t: iteratable) {...} in Groovy, for (t in iteratable ){...}

9. switch statements in Groovy are the same as those in Java, but more types are supported, such as String

10. The while statement of Groovy is the same as that of Java, but do-while is discarded (considering semantic issues, do-while can be replaced by other forms of loop statements with a low usage frequency)

11. the String constant in Java is represented as "Hello, mountain wind Boy". In Groovy, it can be represented as "Hello, mountain wind boy", "Hello, mountain wind boy", "" Hello, mountain Wind boy "(multi-line), '''hello, mountain wind boy ''' (multi-line), def name =" Mountain Wind boy "" Hello, $ {name} "or" Hello, $ name"

12. 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. The default attribute is private, but the get/set method is automatically generated for external use, the method is public by default, while package is used by default in Java. In addition, def can be used to define the method in Groovy. Please refer to the annotations.

13. 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 ()

14. static method calls are the same in Java and Groovy, that is, ClassName. staticMethodName ();

15. 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 {...}

16. Groovy and Java are identical in defining interfaces, that is, interface InterfaceName {...} // in Groovy, the default value is public.

17. Regular Expression constants are not in Java and are expressed as/pattern/in Groovy/

18. 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.

19. 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"

20. In the varargs method, Groovy has one more expression than Java, java/groovy: varargsMethod (Type... args), groovy: varargsMethod (Type [] args)

21. 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)

22. The sub-class calls the parent class method. Groovy and Java are also the same. In Java, super. methodName (), in Groovy, super. methodName ()

23. namespace definition. Groovy is the same as Java. in Java, package edu. ecust. bluesun; in Groovy, package edu. ecust. bluesun (Semicolons can be omitted)

24. 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

25. list constant (type: java. util. arrayList) is not in Java. In Groovy, def list = [3, 11, "Hello", "Mountain Wind boy ","! "]

26. 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.

27. The default parameter of the method, which is not in Java, is expressed as follows in Groovy: def greet (name = "andy "){...}

28. 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 (;)

29. 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.

30. In Groovy, everything is an object! This is not the case in Java. The basic type is not an object.

31. In Java, Class objects are represented as ClassName. class, while in Groovy, Class objects can be directly represented using ClassName.

32. 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. *

33. What about Groovy? : Ternary operator, and? : Two-element operator, but Java only has? : Ternary operator. Def result? : "Default result". If the result is not null, the result is returned. Otherwise, the default result is returned.

34. Groovy can assign multiple values, but Java cannot. Def a, B (a, B) = [a, B] println ([a, B])

35. the closure owner references the 'owner 'of the closure. The default value of the implicit variable delegate is the owner. If closure does not have 'nesting' in other closure, the value of the closure owner is this; otherwise, the closure owner references the closure that 'directly contains'

36. main Traversal method of the Set, each {...} traverse one by one, collect {...} returns the processed result of the traversal set to the corresponding item, find {...} return the first item found according to the condition, findAll {...} return all items found according to the condition, min/max {...} returns the smallest and largest items in the set. join {'-'} uses '-' to connect all items in the set, any/every, and so on.

37. Mixins, builder series: MarkupBuilder, SwingBuilder, etc ......

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.