Hello World!  with Groovy.

Hello World! with Groovy.

Hello Guys! Welcome to the developer's world again. In this article, we will be learning the basics of Groovy. Let's start!!

About Groovy.

Groovy is an object-oriented language based on the Java platform. It is distributed via the Apache License v 2.0. It is a powerful, optionally typed and dynamic language, with static-typing and static compilation capabilities. It supports functional-programming constructs, including first-class functions, currying, and more. It has multiple inheritance, type inference, and meta-programming. Groovy comes in 2003 partly as a response to Ruby. Its main features were dynamic typing, meta-programming (the ability to change classes at runtime), and tight integration with Java.

Groovy has easy-to-learn syntax and it integrates smoothly with any Java program.

Some useful features of Groovy.

  • Support for both static and dynamic typing.

  • Support for operator overloading.

  • Support for regular expressions.

  • Support for unit testing.

  • Support for various markup languages such as XML and HTML.

  • Everything is an Object.

Everything is an object in Groovy-Unlike in Java, primitives can be used like Objects at any time so there appears to be no distinction. For example, since the GDK adds the times method to Number you can do the following:

100.times { println "hi" }

This would print "hi" 100 times.

image.png

Advantage of Groovy over java.

One of the advantages of Groovy over java is that it can be used both as Programming as well as scripting language and also can be integrated with any web applications or scripting applications.

Keywords in Groovy:

image.png

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”.

Prerequisites.

Make sure java must be installed in your system as It is not difficult for someone who is proficient in Java to get started with Groovy. Install Java .

Install Groovy

step 1. Go to (groovy.apache.org/download.html).

image.png

step 2. click on the windows installer shown in the above image.

image.png

step 3. Open the .msi file and click on next for the wizard.

image.png

step 4. Accept the license agreement and click on Next.

image.png

step 5. Choose your setup type.

image.png

step 6. Click on install.

image.png

step 7. And finally, click on finish. Congrats! you have successfully installed Groovy.

image.png

Groovy in ecllipse.

Create a project in ecllipse

Groovy Hello world! Program.

As we are biased to write Hello world! program at the beginning of the path to learning any programming language then, why treat Groovy differently.

So, let's start to welcome the world in the life of Groovy.

create a file Funwithgroovy.groovy, and write the below code in that.

class FunwithGroovy {
   static void main(String[] args) {
      // Using a simple println statement to print output to the console
      println('Hello World');
   }
}

How to run a groovy program in eclipse.

To run the above program on eclipse, right-click on the screen and click on the Run As, and then on the java application.

How to run the above program by using the command prompt.

step 1. To run the program using the command prompt, go to the folder where the program is saved. step 2. Now, click on the address bar and type cmd and press enter key. So that, command prompt can be opened in the right folder. step 3. Type the groovy FunwithGroovy.groovy in cmd, and then press enter key.

When we run the above program we will get output like this:-

image.png

That's it for writing the hello world program in groovy. Next part I will be publishing in the next posts till then, Au, revoir!!

image.png