- Groovy - Meta Object Programming
- Groovy - Template Engines
- Groovy - Unit Testing
- Groovy - Command Line
- Groovy - Builders
- Groovy - Database
- Groovy - DSLS
- Groovy - JSON
- Groovy - JMX
- Groovy - XML
- Groovy - Annotations
- Groovy - Closures
- Groovy - Traits
- Groovy - Generics
- Groovy - Object Oriented
- Groovy - Exception Handling
- Groovy - Regular Expressions
- Groovy - Dates & Times
- Groovy - Maps
- Groovy - Lists
- Groovy - Ranges
- Groovy - Strings
- Groovy - Numbers
- Groovy - Optionals
- Groovy - File I/O
- Groovy - Methods
- Groovy - Decision Making
- Groovy - Loops
- Groovy - Operators
- Groovy - Variables
- Groovy - Data Types
- Groovy - Basic Syntax
- Groovy - Environment
- Groovy - Overview
- Groovy - Home
Groovy Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
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 pne −
class Example { static void main(String[] args) { // Using a simple println statement to print output to the console println( Hello World ); } }
When we run the above program, we will get the following result −
Hello World
Import Statement in Groovy
The import statement can be used to import the functionapty of other pbraries which can be used in your code. This is done by using the import keyword.
The following example shows how to use a simple import of the MarkupBuilder class which is probably one of the most used classes for creating HTML or XML markup.
import groovy.xml.MarkupBuilder def xml = new MarkupBuilder()
By default, Groovy includes the following pbraries in your code, so you don’t need to exppcitly 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
Tokens in Groovy
A token is either a keyword, an identifier, a constant, a string pteral, or a symbol.
println(“Hello World”);
In the above code pne, there are two tokens, the first is the keyword println and the next is the string pteral of “Hello World”.
Comments in Groovy
Comments are used to document your code. Comments in Groovy can be single pne or multipne.
Single pne comments are identified by using the // at any position in the pne. An example is shown below −
class Example { static void main(String[] args) { // Using a simple println statement to print output to the console println( Hello World ); } }
Multipne comments are identified with /* in the beginning and */ to identify the end of the multipne comment.
class Example { static void main(String[] args) { /* This program is the first program This program shows how to display hello world */ println( Hello World ); } }
Semicolons
Unpke in the Java programming language, it is not mandatory to have semicolons after the end of every statement, It is optional.
class Example { static void main(String[] args) { def x = 5 println( Hello World ); } }
If you execute the above program, both statements in the main method don t generate any error.
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. Here are some examples of vapd identifiers −
def employeename def student1 def student_name
where def is a keyword used in Groovy to define an identifier.
Here is a code example of how an identifier can be used in our Hello World program.
class Example { static void main(String[] args) { // One can see the use of a semi-colon after each statement def x = 5; println( Hello World ); } }
In the above example, the variable x is used as an identifier.
Keywords
Keywords as the name suggest are special words which are reserved in the Groovy Programming language. The following table psts the keywords which are defined in Groovy.
as | assert | break | case |
catch | class | const | continue |
def | default | do | else |
enum | extends | false | Finally |
for | goto | if | implements |
import | in | instanceof | interface |
new | pull | package | return |
super | switch | this | throw |
throws | trait | true | try |
while |
Whitespaces
Whitespace is the term used in a programming language such as Java and Groovy to describe blanks, tabs, newpne characters and comments. Whitespace separates one part of a statement from another and enables the compiler to identify where one element in a statement.
For example, in the following code example, there is a white space between the keyword def and the variable x. This is so that the compiler knows that def is the keyword which needs to be used and that x should be the variable name that needs to be defined.
def x = 5;
Literals
A pteral is a notation for representing a fixed value in groovy. The groovy language has notations for integers, floating-point numbers, characters and strings. Here are some of the examples of pterals in the Groovy programming language −
12 1.45 ‘a’ “aa”Advertisements