Why Groovy?

Here, are major reasons why you should use and learn Groovy-

Groovy is an agile and dynamic language Seamlessly integration with all existing Java objects and libraries Feels easy and natural to Java developers More concise and meaningful code compares to Java You can use it as much or as little as you like with Java apps

Groovy History

2003: Developed by Bob McWhirter & James Strachan 2004: Commissioned into JSR 241 but it was abandoned 2005: Brought back by Jeremy Rayner & Guillaume Laforge 2007: Groovy version 1.0 2012: Groovy version 2 2014: Groovy version 2.3 (official support given for JDK 8) 2015: Groovy became a project at the Apache Software Foundation

Features of Groovy

List, map, range, regular expression literals Multimethod and metaprogramming Groovy classes and scripts are usually stored in .groovy files Scripts contain Groovy statements without any class declaration. Scripts can also contain method definitions outside of class definitions. It can be compiled and fully integrated with traditional Java application. Language level support for maps, lists, regular expressions Supports closures, dynamic typing, metaobject protocol Support for static and dynamic typing & operator overloading Literal declaration for lists (arrays), maps, ranges, and regular expressions

How to install Groovy

Step 1) Ensure you have Java installed. https://www.guru99.com/install-java.html Step 2) Go to http://groovy-lang.org/download.html and click installer.

Note: You can also install Groovy using the Zip file or as an Eclipse IDE. In this Groovy tutorial, we will stick to Windows Installer Step 3) Launch the downloaded installer. Select language and click OK

Step 4) Launch. In welcome screen, click NEXT

Step 5) Agree with the license terms

Step 6) Select components you want to install and click NEXT

Step 7) Select Installation Directory and click NEXT

Step 8) Choose Start Menu Folder and click NEXT

Step 9) Once install is done, let the paths default and click NEXT

Step 10) Click NEXT

Step 11) In start Menu search for Groovy Console

Groovy Hello World Example

Consider we want to print a simple string “Hello World” in Java. The code to achieve the string Groovy hello world would be The above code is valid in both Java and Groovy as Groovy is a superset of Java. But the advantage with Groovy is that we can do we away with class creation, public method creation, etc and achieve the same output with a single line code as follows: There is no need for semicolons There is no need for parenthesis System.out.println is reduced to println

Groovy Variables

In Java, static binding is compulsory. Meaning the type of a variable has to be declared in advance. In the above example of this Groovy tutorial, type of variable (integer) is declared in advance using the keyword “int”. If you were to declare a floating point number you use the keyword float. If you try to assign a String value to an int (uncomment line #5), you will get the following error In contrast, Groovy supports Dynamic Typing. Variables are defined using the keyword “def,” and the type of a variable does not need to be declared in advance. The compiler figures out the variable type at runtime and you can even the variable type. Consider the following groovy example, Output In Groovy, you can create multiline strings. Just ensure that you enclosed the String in triple quotes. Output Note: You can still variable types like byte, short, int, long, etc with Groovy. But you cannot dynamically change the variable type as you have explicitly declared it. Consider the following code: It gives the following error

Groovy-Operators

An operator is a symbol which tells the compiler to do certain mathematical or logical manipulations. Groovy has the following five types of operators –

Arithmetic operators: Add (+), Subtract (-), Multiplication (*), Division(/) Relational operators: equal to (==), Not equal to (!=), Less than (<) Less than or equal to (<=), Greater than (>), Greater than or equal to (>=) Logical operators: And (&&), Or(||), Not(!) Bitwise operators: And(&), Or(|), (^), Xor or Exclusive-or operator Assignment operators: Negation operator (~)

Groovy-Loops

In Java, you would define a loop as follows Output You can achieve the same output in Groovy using upto keywords You get the same output as above. $it is a closure that gives the value of the current loop. Consider the following code It gives an output You can also use the “times” keyword to get the same output Consider, you want to print 0,2,4 with for loop in Java Output: You can use the step method for the same

Groovy- Decision Making

Groovy List

List structure allows you to store a collection of data Items. In a Groovy programming language, the List holds a sequence of object references. It also shows a position in the sequence. A List literal is presented as a series of objects separated by commas and enclosed in square brackets. Example of Grrovy list: A list of Strings- [‘Angular’, ‘Nodejs,] A list of object references – [‘Groovy’, 2,4 2.6] A list of integer values – [16, 17, 18, 19] An empty list- [ ] Following are list methods available in Groovy: Consider the following Groovy script example Output

Groovy Maps

A Map Groovy is a collection of Key Value Pairs Examples of Groovy maps:

[Tutorial: ‘Java, Tutorial: ‘Groovy] – Collection of key-value pairs which has Tutorial as the key and their respective values [ : ] Represent an Empty map

Here, is a list of map methods available in Groovy. Groovy Example: Output

Groovy- Closures

A groovy closure is a piece of code wrapped as an object. It acts as a method or a function. Example of simple closure Output: A closure can accept parameters. The list of identifies is comma separated with an arrow (->) marking the end of the parameter list. Output A closure can return a value. Output There are many built-in closures like “It”, “identity”, etc. Closures can take other closure as parameters.

Groovy Vs. Java

Myths about Groovy

Cons of using Groovy

JVM and Groovy script start time is slow which limits OS-level scripting Groovy is not entirely accepted in other communities. It is not convenient to use Groovy without using IDE Groovy can be slower which increased the development time Groovy may need lots of memory Knowledge of Java is imperative.

Groovy Tools

We will discuss 3 important tools in this Groovy script tutorial But Groovy offers more than that.

  1. groovysh: Executes code interactively.

  2. groovyConsole: GUI for interactive code execution

  3. groovy: Executes groovy scripts. You can use it like Perl, Python, etc. Groovysh

    command-line shell Helps you to execute Groovy code interactively Allows entering statements or whole scripts

Groovy console

Swing interface which acts as a minimal Groovy development editor. Allows you to interacts Groovy code Helps you to load and run Groovy script files

Groovy It is the processor which executes Groovy programs and scripts. U It can be used to test simple Groovy expressions.

Summary

Groovy is an Object-oriented programming language used for Java platform Groovy scripting offers seamless integration with all existing Java objects and libraries Bob McWhirter & James Strachan developed groovy in 2003 List, map, range, regular expression literals are important features of Groovy Four type of operators support by Groovy are 1. Relational 2.Logical 3. Bitwise 4. Assignment Groovy performed decision making using if, if/else, Nested if, switch, Netsted switch statements List structure allows you to store a collection of Data Items A Map Groovy is a collection of Key Value Pairs In Groovy, Getters and setters are automatically generated for class members In Java, you can use provide getters and setters method for fields The biggest myth about Groovy is that it can only use for scripting which is not correct Some time Groovy can be slower which increased the development time Three Groovy Tools are: groovysh which executes code, groovy Console which is GUI for interactive code execution and groovy which executes scripts