Groovy is an object oriented language which is based on Java
platform. Groovy 1.0 was released in January 2, 2007 with Groovy 2.4 as
the current major release. Groovy is distributed via the Apache License v
2.0.
পৃষ্ঠাসমূহ
Labels
Search Your Article
CS
Pageviews
Showing posts with label Groovy Tutorial. Show all posts
Showing posts with label Groovy Tutorial. Show all posts
Thursday, February 2, 2017
Groovy - Environment
There are a variety of ways to get the Groovy environment setup.
Binary download and installation − Go to the link www.groovy-lang.org/download.html to get the Windows Installer section. Click on this option to start the download of the Groovy installer.
Binary download and installation − Go to the link www.groovy-lang.org/download.html to get the Windows Installer section. Click on this option to start the download of the Groovy installer.
Groovy - Basic Syntax
In order to understand the basic syntax of Groovy, let’s first look at a simple Hello World program.
Creating Your First Hello World Program
Creating your first hello world program is as simple as just entering the following code line −class Example { static void main(String[] args) { // Using a simple println statement to print output to the console println('Hello World');
Groovy - Data Types
In any programming language, you need to use various variables to
store various types of information. Variables are nothing but reserved
memory locations to store values. This means that when you create a
variable you reserve some space in memory to store the value associated
with the variable.
Groovy - Variables
Variables in Groovy can be defined in two ways − using the native syntax for the data type or the next is by using the def keyword.
For variable definitions it is mandatory to either provide a type name
explicitly or to use "def" in replacement. This is required by the
Groovy parser.
Groovy - Operators
An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations.
Groovy has the following types of operators −
Groovy has the following types of operators −
- Arithmetic operators
- Relational operators
- Logical operators
- Bitwise operators
- Assignment operators
Groovy - Loops
So far, we have seen statements which have been executed one after
the other in a sequential manner. Additionally, statements are provided
in Groovy to alter the flow of control in a program’s logic. They are
then classified into flow of control statements which we will see in
detail.
Groovy - Decision Making
Decision-making structures require that the programmer specify one or
more conditions to be evaluated or tested by the program, along with a
statement or statements to be executed if the condition is determined to
be true, and optionally, other statements to be executed if the condition is determined to be false.
Groovy - Methods
A method is in Groovy is defined with a return type or with the def
keyword. Methods can receive any number of arguments. It’s not
necessary that the types are explicitly defined when defining the
arguments. Modifiers such as public, private and protected can be added.
By default, if no visibility modifier is provided, the method is
public.
Groovy - File I/O
Groovy provides a number of helper methods when working with I/O.
Groovy provides easier classes to provide the following functionalities
for files.
- Reading files
- Writing to files
- Traversing file trees
- Reading and writing data objects to files
Groovy - Optionals
Groovy is an “optionally” typed language, and that distinction is an
important one when understanding the fundamentals of the language. When
compared to Java, which is a “strongly” typed language, whereby the
compiler knows all of the types for every variable and can understand
and honor contracts at compile time. This means that method calls are
able to be determined at compile time.
Groovy - Numbers
In Groovy, Numbers are actually represented as object’s, all of them
being an instance of the class Integer. To make an object do something,
we need to invoke one of the methods declared in its class.
Groovy supports integer and floating point numbers.
Groovy supports integer and floating point numbers.
Groovy - Strings
A String literal is constructed in Groovy by enclosing the string text in quotations.
Groovy offers a variety of ways to denote a String literal. Strings in Groovy can be enclosed in single quotes (’), double quotes (“), or triple quotes (“””). Further, a Groovy String enclosed by triple quotes may span multiple lines.
Groovy offers a variety of ways to denote a String literal. Strings in Groovy can be enclosed in single quotes (’), double quotes (“), or triple quotes (“””). Further, a Groovy String enclosed by triple quotes may span multiple lines.
Groovy - Ranges
A range is shorthand for specifying a sequence of values. A Range is
denoted by the first and last values in the sequence, and Range can be
inclusive or exclusive. An inclusive Range includes all the values from
the first to the last, while an exclusive Range includes all values
except the last. Here are some examples of Range literals −
Groovy - Lists
The List is a structure used to store a collection of data items. In
Groovy, the List holds a sequence of object references. Object
references in a List occupy a position in the sequence and are
distinguished by an integer index. A List literal is presented as a
series of objects separated by commas and enclosed in square brackets.
Groovy - Maps
A Map (also known as an associative array, dictionary, table, and
hash) is an unordered collection of object references. The elements in a
Map collection are accessed by a key value. The keys used in a Map can
be of any class. When we insert into a Map collection, two values are
required: the key and the value.
Following are some examples of maps −
Following are some examples of maps −
Groovy - Dates & Times
The class Date represents a specific instant in time, with
millisecond precision. The Date class has two constructors as shown
below.
Groovy - Regular Expressions
A regular expression is a pattern that is used to find substrings in
text. Groovy supports regular expressions natively using the ~”regex”
expression. The text enclosed within the quotations represent the
expression for comparison.
Groovy - Exception Handling
Exception handling is required in any programming language to handle
the runtime errors so that normal flow of the application can be
maintained.
Exception normally disrupts the normal flow of the application, which is the reason why we need to use Exception handling in our application.
Exceptions are broadly classified into the following categories −
Exception normally disrupts the normal flow of the application, which is the reason why we need to use Exception handling in our application.
Exceptions are broadly classified into the following categories −
Groovy - Object Oriented
In Groovy, as in any other Object-Oriented language, there is the
concept of classes and objects to represent the objected oriented nature
of the programming language. A Groovy class is a collection of data and
the methods that operate on that data. Together, the data and methods
of a class are used to represent some real world object from the problem
domain.
Subscribe to:
Posts (Atom)