May 14, 2021 Groovy
To understand Groovy's basic syntax, let's first look at a simple Hello World program.
Create Hello World, you can do this by entering just a few simple lines of code -
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 program above, we get the following results -
Hello World
Import statements can be used for import, allowing your code to use the functionality of other libraries. T his is done by using the Import keyword.
The following example shows how to use MarkupBuilder's class, which is probably one of the most commonly used to create HTML or XML tags.
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
A token can be a keyword, an identifier, constant, string text, or symbol.
println(“Hello World”);
In the line of code above, there are two tokens, first the println of the keyword and then the "Hello World" of the string.
Use comments in your code. G roovy's comments can be single or multiple lines.
A single-line comment is identified using // anywhere on the line. A n 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'); } }
Multi-line comments are identified with the end of the multi-line comments identified at the beginning / s and s/
class Example { static void main(String[] args) { /* This program is the first program This program shows how to display hello world */ println('Hello World'); } }
Like the Java programming language, it needs to have a hymn to distinguish between multiple statements defined by Groovy.
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'); } }
The above example shows that the sign uses different lines of code to distinguish between statements.
Identifiers are used to define variables, functions, or other user-defined variables. T he identifier begins with a letter, using a dollar or underscore. T hey can't start with numbers. H ere are some examples of valid identifiers
def employeename def student1 def student_name
Where DEF is the keyword used to define identifiers in Groovy.
Here's an example of how to use identifiers 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 example above, the variable x is used as an identifier.
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 |
Blanks are terms used in programming languages such as Java and Groovy to describe spaces, tabs, line breaks, and comments. S paces separate claims from another claim that makes the compiler, one of the elements identified.
For example, in the following code example, there is a gap between the keyword def and variable x. T his is to let the compiler know that DEF needs to be used and that x should be the keyword for the variable name that needs to be defined.
def x = 5;
Text is a symbol that represents a fixed value in groovy. /b20> The Groovy language has signed integers, floats, characters, and strings. H ere are some examples of text in the Groovy programming language -
12 1.45 ‘a’ “aa”