What is groovy?

Source: Internet
Author: User
What is groovy?

Groovy is an alternative language for JVM-SubstitutionJava programming on the Java platform using groovy is basically the same as using Java code. Note: it does not mean that groovy replaces Java, but that groovy and Java are well integrated for programming.

 

● It is an agile Dynamic Language Based on Java virtual machine.

 

● Built on a powerful Java language and added many features learned from languages such as Python, Ruby, and smalltalk.

 

● Provides Java developers with the most popular modern programming language features, and the learning cost is very low (almost zero ).

 

● Supports DSL and other concise syntaxes, making your code easy to read and maintain.

 

● Groovy Can process native types, object-oriented objects, and an ant DSL, making it easy to create shell scripts.

 

● When developing web, Gui, database, or console programs, the efficiency of developers is greatly improved by reducing framework code.

 

● Supports unit testing and Simulation (object) to simplify testing.

 

● Seamless integration of all existing Java objects and class libraries.

 

● Compile the Code directly into a Java bytecode so that groovy Can be used wherever Java is used.

 

One benefit of groovy is that its syntax is similar to that of Java. Although groovy syntax originates from the concepts of smalltalk and ruby, it can be considered a simpler and more expressive variation of Java. (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. (Sometimes there will be fewer !)

Groovy shortcuts

When you start using groovy, you will find that it makes daily programming activities much faster. After completing this tutorial, you will learn more about groovy syntax shortcuts. However, you only need to know the following points:

 

Groovy's loose Java syntax allows the omission of semicolons and modifiers.

 

Unless otherwise specified, all content in groovy is public.

 

Groovy allows you to define simple scripts without defining formal class objects.

 

Groovy adds some unique methods and shortcuts to common Java objects, making them easier to use.

 

Groovy syntax also allows omitting the variable type.

New Features of groovy

Although groovy allows some elements in the Java syntax to be omitted, It also adds some new features, such as local collections, built-in regular expressions, and closures. In standard Java code, if you want to create a list of items, you must first import <code> JAVA. util. arraylist </code>, and then initialize the <code> arraylist </code> instance programmatically,ThenAdd an item to the instance. In groovy, lists and mappings are built in the syntax-no need to import anything. Regular expressions do not require additional imports or objects. They can be created using special groovy syntax.

About closures

For any Java developer, closures are an exciting new technique. These magical structures will be included in future Java releases (probably Java 7) and become the formal Java syntax, but can now be used in groovy. You can setClosureAs a code block, it can be defined now and executed later. You can use these powerful structures to do a lot of beautiful things, but the most famous is simplified iteration. After using groovy, you may no longer need to write iterator instances.

 

  

Dynamic groovy

Technically speaking, groovy may be one of the loose dynamic languages you have heard of recently. From this perspective, groovy and Java are very different. Java is a fixed type language. In groovy, the type is optional, so you do not need to enter string mystr = "hello"; to declare the string variable. You can use def mystr = "hello"; (semicolons are optional ).

 

In addition, groovy code can easily change itself at runtime. This actually means that you can easily specify new methods and attributes for objects at runtime. This field of programming is calledMetaprogramming, Groovy Can well support this programming method. In this tutorial, you will learn more about the dynamic nature of groovy. The only thing you need to add now is that you will be surprised to find that groovy makes it very easy to operate XML or common Java. Io. File instances.

Integration

Any content written in groovy can be compiled into standard Java class files and reused in Java code. Similarly, content written in standard Java code can be reused in groovy. Therefore, you can easily use groovy to write unit tests for Java code. In addition, if you use groovy to write a convenient tool, you can also use this tool in Java programs.

 

Groovy is an open source Scripting Language Implemented in Java and is closely related to it. it requires JDK 1.4. groovy adds many Ruby and Python scripting features to Java. groovy features include dynamic typing, closures, easy object navigation, and more concise lists and maps syntax.

 

Groovy was created by James Strachan and Bob McWhirter. james also participated in the development of many other open-source projects, including jelly, dom4j, jaxen, betwixt, and Maven. bob is the founder of jaxen and drools (an open-source Object-Oriented Java rule engine.

 

Currently, the latest stable version is policy1.8. This groovy version contains many Java 5 features (annotation, generic, Enum...) and fixes many errors.

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.