Você está na página 1de 3

1.

Groovy was made to seamlessly integrate with Java in all possible ways while at the same time adding all the goodies you would expect from a dynamic language.

2. Groovy is an open source dynamic language. 3. Dynamic languages have the ability to extend a program at runtime, including changing the structure of the objects, types and behaviour. 4. Dynamic languages do things at runtime that static languages do at compile time. They allow you to execute program statements that were created on the fly at runtime. 5. With the use of dynamic languages, we can achieve high productivity i.e. higher abstraction in less time. 6. The importance for Dynamic Languages is becoming more and more because a. c. e. a. c. Machine speed Awareness of Unit Testing Ability to run on JVM Ruby Python Rails Grails Django Scala b. Availability d. Killer Applications 7. Dynamic Web Application Development b. Groovy d. Lift

8. Groovy is a language similar to Java Compiles to bytecode that runs on JVM. 9. In Groovy a lot things are optional that are mandatory in Java for Ex. Semicolon, parenthesis for methods, class names, declarations etc. 10. By default java.lang.*, java.util.*, java.io.*, java.net.*, java.math.BigDecimal, java.math.BigInteger, groovy.lang.*, groovy.util.* are available for all. 11. You can use abrreviations for the imported classes for example a. Import com.tcs.TestClass as TC

Groovy for Java EYES.


1. Groovy preserves java syntax and semantics. We can mix java style and groovy style as needed. 2. Java HelloWorld to Groovy HelloWrold transformation show examples. 3. For Loop Variations: a. For(i in 0..2) b. 0.upto 2 {println $it} here c. e. f. g. 3.times{} For(int i=0; i<len; i++) //Java 1.4 or lower For(int I : array/list) //java 1.5 or higher For(I in array/list)

$it is the default name in Groovy. As long the upto

closure uses only one parameter we can use $it. d. 0.step(10,3)

h. For(I in 0..<len) 4. JDK Java Development Kit 5. GDK Groovy Development KIT which extends JDK. 6. Note: Process(java.lang.Process) works only with executable files not with shell commands. For Ex. Dir is a shell command. And Java.exe is an executable program. i.e. dir.execute().text doesnt work but java.execute().text will work. 7. Groovoy has a SafeNavigatoin Operator - ?. It automatically checks the null condition. For example str?.reverse(). 8. Groovy doesnt force you to handle exceptions. Any Exception that is not handled passed on to a higher level automatically. 9. All execptions can be handled by using the following format a. Def openFile(fileName){ try { new FileInputStream(fileName) }catch(FileNotFoundException fnfe){ } b. If you want to catch all ltypes of exceptions then the catch block should be Catch(ex){ }. It actually catches only Exceptions. If you want to catch all (including errors & throwables)you would have to use catch block and use throwable. 2. Groovy as a lightweight JAVA 1. The return statement is almost optional. But when the last line of a function is not an expression(i.e. statement or codeblock like try/catch, if conditions etc) then the return statement is required. 2. Semicolon is not required. Unless if you differentiate clousures or more than one statement is written on the same line. 3. Methods and classes are public by default. 4. You can initialized java beans using named parameters 5. You are not forced to catch exceptions that you dont care to handle. They get passed to the caller of the code. 6. This can be used within static methods to refer the class object. 7. Groovy creates getters and setters for all the attributes by default 8. If the attribute is final then there will not be a setter method created. 9. If you want to access properties you dont have to work on getters and setters . You can work directly with the attributes. Groovy internally uses getters and setters. 10. When constructing an object, simply give values for properties as comma separated name-value pairs. This is a post construction operation if the class has no argument constructor. To take advantage of this feature defines the first parameter as a Map. For Ex. If the number of parameters we send are more than the number of arguments the method expects and if the excess parameters are name-value pairs,

then the groovy assumes that the first argument is a Map and groups all the namevalue pairs as a map for the first parameter. It takes the rest of the parameters in the presented order. 11. Optional Parameters a. In Groovy we can make parameters optional as many as we want. We just have to provide the default value for each of the optional parameters. b. Also it supports java var args for optional parameters. We can use both the options in combination. c. Ex: def optionalArgs(x, y=10, int ...z)[three dots dots .. is for for loop iteration for (I in 0..5)

is for var args and two

Você também pode gostar