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 codeOutput see belowclass Example
{
static void main(String[] args)
{
// Using a simple println statement to print output to the console
println('Hello Groovy');
}
}
Hello Groovy
Import Statement in Groovy Programming
import groovy.xml.MarkupBuilderNote: 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
#! /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.
Note: All java keywords, Whitespaces, literals, keywords are just similar in Groovy script.def a
def a2
def text_form
Groovy control structures
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
If run above program the output isclass Greetings
{
public static void main(String[] args)
{
for(int i = 0; i < 3; i++)
{
print("hello");
}
print("Welcome Groovy!" );}
}
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
Same output givesclass Greetings
{
public static void main(String[] args)
{
0.upto(2) { print "hello" }
print("Welcome Groovy!" );
}
}
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 2The 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 2If 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
0 Comments:
Post a Comment