Introduction to Groovy

Groovy

 

Groovy is a Java-based dynamic language, fully compatible with Java syntax, and Grails is a Groovy-based development framework. Simply put, Groovy is the language and Grails is the framework.

 

Groovy - Introduction

 

Groovy is a flexible dynamic language that runs on the Java platform with features like Python, Ruby and Smalltalk. It can be recognized that Groovy is based on the Java language and extends the Java language. In addition to the language and Java , Groovy is sometimes like a scripting language. When I execute a Groovy script, Groovy will first compile it into Java class bytecode, and then execute the Java class through JVM . The following diagram shows the relationship between Java , Groovy and JVM .



 

In fact, since Groovy Code has become Java bytecode when it is actually executed, the JVM has no idea that it is running Groovy code.

 

From a learning perspective, if you know how to write Java code, you already know Groovy. Java programmers can seamlessly switch to developing programs in Groovy. The main difference between Groovy and the Java language is that less Groovy code than Java code is required to accomplish the same task.

 

Groovy - Features

 

Get started with Groovy and you'll find that it makes everyday programming activities a lot faster

1. Groovy 's loose Java syntax allows omitting semicolons and modifiers.

2. All Groovy content is public unless otherwise specified .

3. Groovy allows to define simple scripts without defining regular class objects.

4. Groovy adds some unique methods and shortcuts to common common Java objects to make them easier to use.

5. Groovy syntax also allows to omit variable types .

 

Groovy - New Features

 

While Groovy allows some elements of Java syntax to be omitted , it also adds new features such as local collections, built-in regular expressions, and closures. In standard Java code, to create a list of items, first import java.util.ArrayList , then programmatically initialize an ArrayList instance, and then add items to the instance. In Groovy , both lists and maps are built into the syntax - no need to import anything. Regular expressions also do not require additional imports or objects; they can be created with special Groovy syntax.

 

Groovy - Closures

 

Closures are an exciting new trick for any Java developer. These magic constructs will be included in a future Java release (most likely Java 7 ) as official Java syntax, but are now available in Groovy . A closure can be thought of as a block of code that can be defined now and executed later. There are many nifty things you can do with these powerful constructs, but the most famous is to simplify iteration. After using Groovy , it is possible to never need to write an Iterator instance again.

 

Groovy - Dynamic

 

Technically, Groovy is probably one of the most loosely typed dynamic languages ​​you've heard of lately. From this point of view, Groovy is very different from the Java language , which is a fixed-type language. In Groovy , types are optional, so you don't have to type String myStr = "Hello"; to declare a String variable.

In addition to that, Groovy code can easily change itself at runtime. This essentially means that new methods and properties can be easily assigned to objects at runtime. This area of ​​programming is called metaprogramming, and Groovy supports it very well. As you work through this tutorial, you will learn more about the dynamic nature of Groovy . The only thing to add now is that you'll be surprised how easy it is to manipulate XML or plain java.io.File instances in Groovy .

 

Groovy - One with two sides

 

Anything written in Groovy can be compiled into standard Java class files and reused in Java code. Similarly, content written in standard Java code can also be reused in Groovy . So, it's easy to write unit tests for Java code using Groovy . And, if you write a handy little tool in Groovy , you can also use that little tool in a Java program.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326444970&siteId=291194637