java programmers Why Groovy?

java programmers Why Groovy?



Java world has heard that there is a Groovy, but has no time to get to know how it really is a way of. We now project put a lot of open source packages, processes and projects to do when the rule is implemented in Groovy. Recently nothing better to do, began to seriously look at Groory what is good. Actually, when I touch it because it a Grails framework, both of which are to achieve a purpose, agile development of java, java and seamless connection. In some cases, java need to do a half-day treatment, Groovy just a few minutes, yes, a few minutes ... to the rest of the time, programmers finally have time to soak up sister, ^ _ ^ ...... Technical house of brothers, quickly and see it.

Scripting languages, dynamic languages? Is it a kind of javascript? Well, it does and javascript a bit like, but it is too strong, far beyond what javascript can handle. Here javascript and do not compare, compare it directly and Java, Groovy and see what to do.

Groovy's website (http://groovy.codehaus.org) gives the best definition of groovy: groovy is java on the platform, with like Python, Ruby and Smalltalk language features flexible and dynamic languages, groovy ensure these properties the syntax is the same as using a java java developers. Groovy and learn to javaer, it is very easy thing. If you have not started learning Ruby, Come with me Groovying about ............ and comes with a Grails to rival Ruby on rails.


 1.Groovy compatible java.

Yes, you can use java syntax to write groovy, finally can also generate class files, using java call directly. You will say, what is good, not that it changed the name suffix? Vomit, maybe ... but it's certainly not the highlight, the highlight of it is that you can mix and match with java syntax to write directly in Groovy which, of course, not very recommended to do so.


2.Groovy is dynamic.

Java reflection does not need and does not require lengthy code, do not need to try catch a variety of abnormalities, you can achieve all kinds of amazing things. Groovy's class can add such a method, or call it.


//用闭包定义一个方法 var1为参数 ,->后面是执行语句(当然参数不是必须的)
def methodA={var1-> print "this is methodA"}

//用闭包定义一个方法 var1为参数 ,->后面是执行语句(当然参数不是必须的)
def methodB={var1-> print "this is  methodB"}

String.metaClass.addMethodA=methodA;   //将methodA绑定为成员方法。
String.metaClass.'static'.addMethodB=methodB;   //将methodB绑定为静态方法

String s="str";
s.addMethodA('good');  //实例调用方法A
String.addMethodB('hello'); //静态类调用方法B


The above two methods to add the code to the String class which (God, final String is also broken), then calls each addMethodA instance object, a static class calls AddMethodB.


3.Groovy is a Swiss Army knife.

java is a basic tool, so when you need, you need to look everywhere jar package, to fill their own program, and you need to see to understand the various API use. In addition, the preparation of java is too long, in order to print a HelloWorld, you need to write a class, write a main function. For Groovy is:. Print "! Hello world" this sentence is enough, this is just the beginning ...

By groovy you can quickly open the console to output all the file names (recursive processing) by entering the following code:

groovy -e "new File('.').eachFileRecurse { println it }"

Even if java has a name for the method eachFileRecurse and an interface FileListener, you still need to show the creation of a class, declare a main method to save the source code into a file and compile it, then you can run it, based on the comparison, Let's look at the code java to do the same job, we assume that there is a separate eachFileRecurse method:

public class ListFiles { // JAVA !!

public static void main(String[] args) {

new java.io.File(".").eachFileRecurse( // 假设 java 存在该方法

new FileListener() {

   public void onFile (File file) {
        System.out.println(file.toString());
       }
   }
);
}


Well, there is a need "Groovy in Action [Text] [Chinese] .pdf" direct private letter I, Groovy now start learning it.


Special Note: This article material from the network, only to share learning, if infringement, please contact deleted!




Recommended Reading

Gold and three silver four seasons, Ali 10 years working Java Daniel's "experience", dedicated to the confusion in you


Guess you like

Origin blog.51cto.com/14751386/2483856