What is Groovy Script?
Groovy is open source under Apache License, version 2.0. Groovy is a lightweight, dynamic, object-oriented language that runs on the JVM.
It derives
strength from different languages such as Smalltalk, Python, and Ruby while
retaining a syntax familiar to Java programmers. Groovy compiles into Java
bytecode and extends the
Java API and libraries.
Why Groovy?
First, you can take almost any Java code and
run it as Groovy.
Directly use all java classes into Groovy script also. Groovy
classes extend the same good old java.lang.Object—Groovy
classes are Java classes.
Groovy is dynamic, and it is optionally typed. If you’ve
enjoyed the benefits of other dynamic languages such as Smalltalk, Python,
JavaScript, and Ruby, you can realize those in Groovy.
As a Java programmer, you rely heavily on the JDK(Java development Kit) and the
API to get your work done. These are still available in Groovy.
Groovy is an attractive language for a number of reasons:
- It has a flat learning curve.
- It follows Java semantics.
- It bestows dynamic love.
- It extends the JDK.
Groovy Language Features
The
Groovy solution is short, precise, and more compact than regular Java. Groovy
doesn’t need to import the java.util package or specify the Date type.
import java.util.*; // Java Date today = new Date(); // Java today = new Date() // Groovy
Groovy doesn’t require semicolons when it can understand the code without them. Despite being more compact, Groovy is fully comprehensible to a Java programmer.
Groovy has three main enhancements over and above those of Java: language features, libraries specific to Groovy, and additions to the existing Java standard classes (known as the Groovy Development Kit, or GDK).
Many of the library features rely heavily on language features. Idiomatic Groovy code rarely uses one feature in isolation; instead, it usually uses several of them together, like notes in a chord.
File handling in Groovy is made significantly easier with the addition of various methods to classes in the java.io package.
The JavaBean conventions for properties are almost ubiquitous in Java, but the language makes no use of them. Groovy simplifies property access, allowing for far more readable code.
Groovy Execution commands
There
are three commands to execute Groovy code and scripts, as shown in table
Command |
What it does |
groovy |
Starts the processor that executes Groovy scripts. Single-line Groovy scripts can be specified as command-line arguments. |
groovysh |
Starts the groovysh command-line shell, used to execute Groovy code interactively. By entering statements or whole scripts line by line into the shell, code is executed on the fly |
groovyConsole |
Starts a graphical interface that’s used to execute Groovy code interactively; moreover, groovyConsole loads and runs Groovy script files. |
Using web groovyConsole
The groovyConsole is a Swing interface that acts as a minimal Groovy development editor. It lacks support for the command-line options supported by groovysh; however, it hasa File menu to allow Groovy scripts to be loaded, created, and saved.
Compiling and running Groovy from command-line
First Compiling Groovy is straightforward because it comes with a compiler called groovyc.
groovyc d:/classes/Hellowworld.groovy
0 Comments:
Post a Comment