Thursday, October 4, 2012

Groovy

Groovy









WHAT IS GROOVY? 
Groovy is a dynamic object-oriented programming language for the Java virtual machine (JVM) that can be used anywhere Java is used. The language can be used to combine Java modules, extend existing Java applications and write new applications.
Groovy can serve as a scripting language for developers new to the Java platform and can also be useful for veteran Java developers interested in enhancing the expediency and flexibility of that language.
Groovy has a Java-like syntax and works seamlessly with Java bytecode. Many of the language's features resemble those of PerlPythonRuby and Smalltalk.
FEATURES: 
·         Easy learning curve
·         Support for domain-specific languages
·         Compact syntax
·         Support for dynamic typing
·         Powerful processing primitives
·         Ease of Web application development
·         Support for unit testing.

SHORT HISTORY: 

      The Groovy concept was introduced in 2003 by James Strachan. Beta versions were made available from 2004 to 2006. Version 1.0 was released in January 2007 and version 1.1 was released in December 2007. Groovy is going through standardization under Java Specification Request (JSR) 241. The project manager is Guillaume Laforge.
SAMPLE HELLO WORLD CODE: 
In the top part of the window of the groovyConsole, type the following
println "Hello, World!"
And then type <CTRL-R>.
Notice that the text gets printed out in the OS console window (the black one behind the groovyConsole window) and the bottom part of the groovyConsole says:
groovy> println "Hello, World!"
null


The line starting with "groovy>" is just the text of what the console processed. The "null" is what the expression "evaluated to". Turns out the expression to print out a message doesn't have any "value" so the groovyConsole printed "null".
WHY WOULD WE CHOOSE GROOVY?
There are many languages developed for scripting. To name a few popular ones JRuby, Jython are some of them. JRuby and Groovy has gained significant
importance in the recent years and are competing each other in the recent years.
One could choose groovy for a variety of reasons.
·         It has the java-like syntax
·         for a java programmer has a faster learning curve
·         contains a vast set of library features which could enhance productivity.
·         Provides coding structure in such a way that it is easy to achieve in a few lines of code than which could be achieved in java with much larger code steps.
As groovy is designed to run on JVM, it serves as a protector for the codes that’s been developed for years in the past.
In the first look, it may seem that groovy is confusing, but in the long run it makes a big difference.

GROOVY OVER JAVA
The main advantage of having groovy over java is the rapid development of CRUD based applications and much lesser lines of code.  Java’s capability to achieve things is done in much fewer lines of code.
Groovy tries to be as natural as possible for Java developers. We've tried to follow the principle of least surprise when designing Groovy, particularly for developers learning Groovy who've come from a Java background.
DEFAULT IMPORTS
All these packages and classes are imported by default, i.e. you do not have to use an explicit import statement to use them:
·         java.io.*
·         java.lang.*
·         java.math.BigDecimal
·         java.math.BigInteger
·         java.net.*
·         java.util.*
·         groovy.lang.*
·         groovy.util.*

THINGS TO BE AWARE OF: 

·     Semicolons are optional. Use them if you like (though you must use them to put several statements on one line).
·         The return keyword is optional.
·         You can use the this keyword inside static methods (which refers to this class).
·         Methods and classes are public by default.
·         Protected in Groovy has the same meaning as protected in Java, i.e. you can have friends in the same package and derived classes can also see protected members.
·         Inner classes are not supported at the moment. In most cases you can use closures instead.
·         The throws clause in a method signature is not checked by the Groovy compiler, because there is no difference between checked and unchecked exceptions.
·         You will not get compile errors like you would in Java for using undefined members or passing arguments of the wrong type. See Runtime vs Compile time, Static vs Dynamic.

NEW FEATURES ADDED TO GROOVY NOT AVAILABLE IN JAVA
·         Closures
·         native syntax for lists and maps
·         GroovyMarkup and GPath support
·         native support for regular expressions
·         polymorphic iteration and powerful switch statement
·         dynamic and static typing is supported - so you can omit the type declarations on methods, fields and variables
·         you can embed expressions inside strings
      ·         simpler syntax for writing beans for both properties and adding event listeners
      ·         safe navigation using the ?. operator, e.g. "variable?.field" and "variable?.method()" - no            more nested ifs to check for null clogging up your code
·         lots of new helper methods added to the JDK



COMMENTS ON GROOVY:

“Like any project in its infancy, Groovy is a work in progress. Developers accustomed to working with Ruby and Python (or Jython) might miss the convenience of such features as mixins, script import (although it's possible to compile the desired importable script into its corresponding Java class), and named parameters for method calls. But Groovy is definitely a language on the move. It will likely incorporate these features and more as its developer base grows.”
--Andrew Glover, 2004

“Groovy, the language, is quite stable at this point. The upcoming 1.8 release has a lot of "features", but these are really more like library additions than language changes. Groovy has always been focused on removing Java boilerplate, and the new annotations@Synchronized, @WithReadLock and @WithWriteLock, and @AutoClone are good examples of this. These are all Groovy compiler extensions (known as AST Transformations) that affect the .class file produced. I've heard the criticism that these things should be libraries and not part of the language, but that's really a debate about distribution. The interesting part is that the Groovy compiler became extensible and we're seeing more and more frameworks like Spock, GContracts, and Griffon take advantage of this with interesting results. Other than that, technical lead Jochen Theodorou just checked in a whole bunch of performance optimizations for Groovy and I'm pretty excited to see the results. It happened with the Groovy 1.8 Beta 3 release, so this is the newest of the new. It would be great if people could download the release and test their apps with it, even if it is just to run the unit tests.”

--Hamlet D’Arcy, 2010

"I have tried coding in Groovy when i was a second year student and i learned it easily since it is familiar to Java. I solved some problems i had with C, using Groovy and it did worked well 
Here's a sample code i made for Groovy when i was still a second year student: 


import javax.swing.*;
import java.util.*;

sizearray = new ArrayList();

def generate(str, n){
    if ((str.split(",").size() == n) && !sizearray.contains(str)){
        sizearray.add(str);    
        return;
    }
    sarray = str.toString().split(",");
    for (i = 0; i < sarray.size(); i++){
        str2 = "";
        for(j=0; j < sarray.size(); j++){
            if(j == i)
                continue;
            str2+= (sarray[j].toString()+",");
        }
        generate(str2, n);
    }
}
    
if((number = Integer.parseInt(JOptionPane.showInputDialog("Number of line segments:"))) && (number<=1000) && (number>=3) ){
    sizestr = "";
    for(i=0;i<number;i++){
        size = Integer.parseInt(JOptionPane.showInputDialog("Enter size of line segments:"));
        if((size <= (10**500)) && (size >= 1))
            sizestr+=size.toString()+",";
    }
    generate(sizestr, 3);
    for(j=0;j<sizearray.size();j++){
        sizes = sizearray.get(j).toString().split(",");
        
         if( (Integer.parseInt(sizes[0]) + Integer.parseInt(sizes[2]) > Integer.parseInt(sizes[1])) && (Integer.parseInt(sizes[1]) + Integer.parseInt(sizes[2]) > Integer.parseInt(sizes[0])) && (Integer.parseInt(sizes[0]) + Integer.parseInt(sizes[1]) > Integer.parseInt(sizes[2])))
            println ("These are valid length of sides for a triangle: "+sizes[0].toString() +", "+sizes[1].toString() +", "+sizes[2].toString() +".");
    }
}



Overall, i think Groovy is best for Java developers and it is easy to understand
Since it also has its own console, it has its interface in creating programs. " 

- Jeunesse Doree E. Burce, 2012

REFERENCES: 

MEMBERS: 
- Jeunesse Doree E. Burce(2010-26654) nero17
- Farah Jane Arato
CMSC 124 T-1L

































No comments:

Post a Comment