Groovy Learning Notes (ii)-from Java to groovy

Source: Internet
Author: User

It has been said before that many people choose groovy because of their low learning costs, as long as you have the basics of Java and learn quite quickly. In contrast, Scala (another JVM dynamic language) syntax is more difficult to learn (though not yet learned, but online reviews are).

As a Java programmer, I'm going to get myself groovy.

From JDK to GDK

1. As the core of Java development, JDK is the part that all Java programmers need to learn and use. Groovy in the design of the GDK is very intimate and intimate (important feelings to express three times), GDK can be considered as JDK extensions and superset, a lot of Java API in the GDK you can find, even Java files can be compiled with groovy run, This provides a great convenience for mixing Java and groovy, and Java programmers can embed groovy code at a very low cost, extending Java and writing more efficiently.

2, groovy and Java of the blood supporting

1 def str = "I ' m a String"2 println str.class. Name

Operation Result:

1 java.lang.String 2 [finished in 0.7s]

As you can see, the object in groovy is actually a Java object, although it may have been extended.

One more example:

1 def num = 32 num.upto (5) {println "$it"}3 println Num.class. Name

Operation Result:

1 32 43 54java.lang.Integer5 [finished in 0.7s]

Groovy expands the integer and joins the upto () method.

Groovy is lightweight Java (various provinces)

Again, this is mainly because in the course of learning groovy, often there is such a sigh "Alas, this unexpectedly does not need to do" or "this unexpectedly can be so simple to write!" , its light-weight, fast characteristics are reflected in many aspects.

1, safe navigation operator (?. )

Java is flooded with if (obj! = null) {obj.dosomething ();} Code, but in groovy, only obj is required. DoSomething (). Instantly feel Java weak burst ~

2, semicolon can be saved, the reference can not be named (with Def on it)

3, do not want to catch the exception, not to deal with the Java is throws forced to write

4, return can also save

5, getter, setter also save, similar to Car.engine is actually called car.getengine ()

6, named parameters (no longer remember the parameter order)

7, multi-assignment (sense of Python, do not need to return to multi-valued and built a strange class)

8. By using as and Astype, the code block can be passed directly as an interface implementation

9. More convenient loops such as for (I in 0..100) {}

10, static import, import static methods (similar to Python, you can use as to create aliases), directly through the method name reference

11. No type-checking paradigm at compile time (groovy only infers at run time, inference fails and throws exception)

12. Annotations similar to Lombok

13. Operator overloading (extremely useful when designing a DSL)

Well, groovy has a part of me that I hate (learning for the moment)

1, the solution of Boolean value

Different types of rules are not the same, such as collection non-empty for True,number not 0 and so on. Implicit is disaster! Who knows the dynamic run-time Momin wonderfully takes what type of object to judge, computes a Boolean value.

Groovy designers dig holes for Java programmers:

1. The = = in Groovy is equal to the Equals () method in Java, and = = in Java is used to determine if the reference is the same, and in groovy it corresponds to the IS () method

2, Java code if the variable name is it such groovy keyword, will be error.

In a nutshell, groovy has a very similar syntax to Java, except for a small number of different things, and groovy is a dynamic extension of Java.

Groovy Learning Notes (ii)-from Java to groovy

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.