Groovy - Basic Syntax


Advertisements

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');
   }
}

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 functionality of other libraries 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 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

Tokens in Groovy

A token is either a keyword, an identifier, a constant, a string literal, or a symbol.

println(“Hello World”);

In the above code line, there are two tokens, the first is the keyword println and the next is the string literal of “Hello World”.

Comments in Groovy

Comments are used to document your code. Comments in Groovy can be single line or multiline.

Single line comments are identified by using the // at any position in the line. 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');
   }
}

Multiline comments are identified with /* in the beginning and */ to identify the end of the multiline 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

Unlike 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 valid 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 lists 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, newline 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 literal 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 literals in the Groovy programming language −

12 
1.45 
‘a’ 
“aa”
Advertisements