1. Introduction
Groovy is an agile Dynamic Language for Java virtual machines. It is a mature object-oriented programming language that can be used for both Object-Oriented Programming and pure scripting languages. This language does not have to write too much code, but also has closures and other features in dynamic languages.
Groovy is an alternative language of JVM (alternative refers to Java programming on the Java platform using groovy). It is basically used in the same way as Java code, this language is especially suitable for use with spring's Dynamic Language Support. Java integration is fully considered during design, which makes it easy for groovy to interoperate with Java code.
2. Features
Features 2.1
1. Built on powerful Java languages and added many features learned from languages such as Python, Ruby, and smalltalk, such as dynamic type conversion, closures, and metaprogramming support ..
2. It provides Java developers with the most popular modern programming language features and has a low learning cost (almost zero ).
3. Supports DSL and other concise syntaxes, making the code easy to read and maintain.
4. Checked exception does not need to be captured.
5. Groovy processes native types, object-oriented objects, and an ant DSL, making it very easy to create shell scripts.
6. When developing web, Gui, database, or console programs, the efficiency of developers is greatly improved by reducing framework code.
7. unit testing and simulation (object) are supported to simplify testing.
8. seamlessly integrate all existing Java objects and class libraries.
9. Compile the Code directly into a Java bytecode so that groovy Can be used wherever Java is used. [2]
10. Supports functional programming without the need for the main function.
11. Some new operators.
12. Common packages are imported by default.
13. assertions do not support JVM-ea parameters.
14. Support boolean evaluation of objects.
15. The class does not support the default scope, and the default scope is public.
16. The basic type in groovy is also an object. You can call the object method directly.
2.2 dynamic type
Types are optional for variables, attributes, methods, closure parameters, and return types of methods. They are determined only when a value is assigned to a variable, different types will be used later, and any types can be used, even for basic types (via autoboxing )). if necessary, conversions between many types will automatically occur. For example, conversions between these types: String, basic type (such as INT), and type packaging class (such as integer) you can add different basic types to the same Array (collections.
2.3 Closure
Closures are code snippets that can use parameters. Each closure is compiled to inherit groovy. lang. closure class. This class has a call method that can pass parameters and call the closure. they can access and modify the variables within the scope of the closure creation. The variables created in the closure can also be referenced within the scope of the closure being called, closures can be stored in variables and passed to methods as parameters.
3. Syntax
Groovy syntax is similar to Java syntax. Although groovy syntax is derived from the concepts of smalltalk and Ruby, however, you can think of it as a simpler and more expressive variant of the Java language. (At This Point, Ruby is different from groovy because its syntax is very different from Java syntax .)
Many Java developers like the similarities between groovy code and Java code. From the perspective of learning, if you know how to write Java code, you already know groovy. The main difference between groovy and Java is that less groovy code is required to complete the same task than Java code.
4. Class
Like java classes, groovy classes can be defined using standard Java Bean syntax. But as another language, you can define classes in a more groovy way. The advantage is that you can write more than half of the JavaBean code.
(1) Public modifier not required
As mentioned above, groovy's default access modifier is public. If groovy Class Members need public modifier, they do not need to be written.
(2) No type description is required.
As mentioned earlier, groovy does not care about the specific types of variables and method parameters.
(3) The getter/setter method is not required.
Many IDES (such as Eclipse) have long been able to automatically generate the getter/setter Method for programmers. In groovy, The getter/setter method is not required-all class members (if it is the default public) you do not need to reference them through the getter/setter method at all (of course, if you must access member attributes through the getter/setter method, groovy also provides them ).
(4) No constructor is required
Programmers no longer need to declare any constructor, because actually only two constructor are required (one default constructor without parameters, one constructor with only one map parameter-because it is of the map type, this parameter can be used to construct a member variable of an object and initialize it at will ).
(5) No return required
In groovy, return is not required for methods.
(6) No ()
Method calls in groovy can be omitted () (except constructors ).
5. Relationship between files and Classes
In groovy, The ing between classes and files is not as fixed as in Java (a file in Java can only have one class declared as public and some other non-public classes and embedded classes ). The same groovy file can contain multiple public class definitions. The specific rules are as follows:
If there is no class definition in a groovy file, it will be processed as a script, which means that the file will be transparently converted to a class of the script type, the Automatically converted class uses the original groovy file name (excluding the extension, without the package name, in the default package) as the class name. The content of the groovy file is packaged into the run method, and a main method is added to the newly generated class to execute the script externally.
If there is a class definition in the groovy file and the class name is the same as the file name, this is the same as the one-to-one correspondence between classes and files in Java.
A groovy file can contain multiple class definitions with different visibility, and there is no mandatory requirement that the class name of one class is the same as the file name. The policyc compiler will be happy to compile all the classes defined in this file into the *. Class file. If you want to directly call this groovy script, such as using the groovy command line or executing it in an IDE, you should define a main method in the first class of the file.
In a groovy file, the class definition and script definition can be mixed. In this case, the script code will become the main class directly called, so in this case, you should not define a class with the same name as the file.
When a class is executed without a clear compilation process, groovy searches for classes by file names. In this case, the name will be very important. Groovy can only find classes that match the file name. When you find a class that matches the name, other classes defined in the file will be parsed and become visible to groovy.