Basics of Groovy Programming

 GROOVY – BASIC SYNTAX

 Nearly Groovy syntax very similar to java program, in this add some more user friendly syntax . Let

Look following are the print Hello Groovy program see below code

class Example
{
  
static void main(String[] args)
  {
      
// Using a simple println statement to print output to the console
      
println('Hello Groovy');
  }
}

Output see below
Hello Groovy

Import Statement in Groovy Programming

The Import is used to import library classes , interfaces and methods into following program.
import groovy.xml.MarkupBuilder
Note: By default, Groovy includes the following libraries in your code, so you don’t need to explicitly import them.

import java.lang.*

import java.util.*

import java.io.*

import java.net.*

import groovy.lang.*

import groovy.util.*

import java.math.BigInteger

import java.math.BigDecimal

Commenting Groovy code 

 Single-line comments and multiline comments are exactly like those in Java, with an additional option for the first line of a script:

#! /usr/bin/env groovy

// single line comment

/* some multi

line comment */

Note from the above 3 lines of comments

Here are some guidelines for writing comments in Groovy:

The #! shebang comment is allowed only in the first line. The shebang allows UNIX shells to locate the Groovy bootstrap script and run code with it.

// denotes single-line comments that end with the current line.

Multiline comments are enclosed in /* ... */ markers.

Javadoc-like comments in /** ... */ markers are treated the same as other multiline comments, but are processed by the groovydoc Ant task.

Other parts of Groovy syntax are similarly Java friendly.

Groovy Identifiers

Identifiers are used to define variables, functions or other user defined variables. Identifiers start with a letter, a dollar or an underscore. They cannot start with a number.

def a

def a2

def text_form
Note: All java keywords, Whitespaces, literals, keywords are just similar in Groovy script.

Groovy control structures

 Control structures allow a programming language to control the flow of execution through code. There are simple versions of everyday control structures like if-else, while, switch, and try-catch-finally in Groovy, just like in Java.

 Example


for(i in x) { body }

if (false) print false

if (null)

{

print false

}

else

{

print true

}

def i = 0

while (i < 10)

{

i++

}

def clinks = 0

for (remainingGuests in 0..9)

{

clinks += remainingGuests

}

def list = [0, 1, 2,3]

for (j in list)

{

assert j == list[j]

}

list.each()

{

item -> assert item == list[item]

}

switch(3)

{

case 1 : assert false; break

case 3 : assert true; break

default: assert false

}

Using For loop in Groovy programming

 Following program shows using for loop in Groovy programming style

class Greetings
{
  
public static void main(String[] args)
  {
    
for(int 03i++)
   {
      
print("hello");
   }
     
print("Welcome Groovy!" );

  }
}

If run above program the output is
hello hello hello Welcome Groovy!
Note the for loop is also use as
for(i in 0..2) { print'hello ' }
You’re not restricted to the traditional for loop in Groovy. You alreadyused the range 0..2 in the for loop.
Groovy has added a convenient upto( ) instance method to java.lang.Integer
0.upto(2) { print "$it " }
Example program as

class Greetings
{
  
public static void main(String[] args)
  {
    
0.upto(2) { print "hello" }
    
print("Welcome Groovy!" );
  }
}

Same output gives
hello hello hello Welcome Groovy!
If you use following upto() method
0.upto(2) { print"$it " }
Here you called upto( ) on 0, which is an instance of Integer. The output from the previous code is as follows:
0 1 2
The upto( ) method allows you to set both lower and upper limits.
If you start at 0, you can also use the times( ) method, as shown here:
3.times { print "$it " }
The output from previous code is as follows:
0 1 2
If you want to skip values while looping, use the step( ) method:
0.step(10, 2) { print "$it " }
The output from the previous code is as follows:
0 2 4 6 8

Table of Groovy Content

0 Comments:

Post a Comment