New features of Groovy 1.5

Source: Internet
Author: User
Tags assert new features object model

Groovy, the Java Dynamic language for the JVM, is as ripe as a vintage wine. Following the successful release of Groovy 1.0 in January 2007, the next major milestone, version 1.5, has been released. There are some interesting and novel places in version 1.5, and we'll look at these features in this article. Language primarily enhances support for Java 5 features, including annotations, generics, and enumerations, making groovy the only candidate dynamic language for a framework that is fully supported by the JVM, including Spring,hibernate,jpa,goole Guice or testng. In addition to the new Java 5 features, groovy adds new syntax enhancements to the language, as well as more robust dynamic feature customization, a steroids swing UI builder, and improved tooling support.

Why is it important to have a more groovy groovy?

The key selling point of groovy has always been its seamless integration with Java. You can easily mix groovy and Java classes: You could have a Java class implement a groovy interface, and then let a groovy class inherit that Java class, or vice versa. Unfortunately, most of the other candidate JVM languages don't allow you to seamlessly exchange classes between two different languages. So, if you want to use the best language for your work without giving up the graceful class hierarchy, you don't have much choice, and groovy allows you to be free to integrate the two languages in almost transparent ways.

Groovy shares the same library with Java, the same object model, the same threading model, and the same security model. In a sense, you can think of groovy as an implementation detail of your Java project without having to endure impedance mismatch problems.

Groovy is Java, and groovy makes Java more groovy. Groovy provides the smoothest learning curve for Java developers, thanks to a very similar syntax, compared to other languages.

Keep in mind that groovy produces normal Java bytecode and uses a common JDK library, so you don't need to learn all the new APIs and don't need complex integration mechanisms: it's extremely convenient that groovy and Java can be exchanged. The added benefit is that you can protect your Java Developer's Java skills Investment, or expensive application servers, or Third-party or company-developed libraries that you can reuse in groovy without any problems.

Other candidate languages that do not support strong typing are not always able to invoke all Java methods when invoking JDK, third-party libraries, or the company's own libraries because they cannot discern a polymorphic variant of the same method. When you choose a language to improve your productivity or make your code more readable, if you need to invoke other Java classes, you have to be very cautious about choosing the language because you may encounter a lot of trouble.

Today, all major enterprise frameworks require the use of language features such as annotations, enumerations, or generics to fully improve their efficiency. Fortunately, developers use Groovy1.5 to use all of the Java 5 features in their projects and benefit from it. Let's take a look at how to use annotations, enumerations, and generics in groovy.

Java 5 Added section

The groovy compiler always produces Java bytecode that is compatible with the previous Java VM, but groovy relies on JDK1.4 because groovy uses the JDK1.4 core library. However, for the additions to these Java 5, it is definitely necessary to use the Java 5 bytecode. For example, the resulting class might contain bytecode information that represents a run-time retention policy annotation. So while Groovy1.5 can run on JDK1.4, some of groovy's features can only be used on JDK1.5-this is the case when this article makes a statement.

Variable parameters

The ellipsis notation is created in Java 5, and the parameter representing the method is variable length. With three dots, Java allows the user to enter any number of parameters of the same type at the end of a method--in fact, the variable length parameter (vararg) is an array of elements of that type. Variable-length parameters are already available in Groovy 1.0-you can still work in the JDK1.4 run-time environment, and 1.0 is enough to show you how to use them. Basically, when the last parameter of a method is an array of objects, or a three-point argument, you can pass in multiple parameters to this method.

The first example describes the method of using an ellipsis in groovy to use variable-length variables:

int sum(int... someInts) {
def total = 0
for (int i = 0; i < someInts.size(); i++)
total += someInts[i]
return total
}
assert sum(1)    == 1
assert sum(1, 2)  == 3
assert sum(1, 2, 3) == 6

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.