Explain what is Groovy?
Groovy is an object-oriented programming language for JVM (Java Virtual Machines).  It is used to combine Java modules, to write Java application and to extend existing Java application.
Why use Groovy?
·         For Java programmers it provides familiar syntax
·         It has a rich stock of Java Libraries
·  It easily integrates with your existing infrastructure like Servlet Containers, App Servers, Loads of databases with JDBC drivers,
·         Completely Object Oriented
·         It possesses a reusable and assignable pieces of code
·         Operators can be overloaded
·         Literal declaration for maps, arrays, ranges and regular expressions
·         It has efficient object navigation
What is the limitation of Groovy?
·         Groovy can be slower
·         Groovy might need lots of memory
·         Groovy start up time requires improvement
·         It requires Java knowledge
·     It takes sometimes to get use to like New Syntax, closures, default typing,
·         Documentation is thin
Explain how Scripts are run in Groovy?
Groovy supports plain script; it does not require a class declaration. At the front of the script, imports are supported at the same way that it can be at the front of a class. In Groovy, you have to use word def to declare a function outside of a class.
Mention what are some features does Groovy JDK offers?
Groovy has added new methods compare to old version like
Various array types and object streams with newly Groovy oriented methods
like Object.every(), Object.each() etc. and also include new features like “String BufferedReader.getText()” and “InputStream.eachLine(Closure)”.
Mention what is the role of closure and listeners in Groovy?
Groovy does not support anonymous inner classes; it is possible to determine action listeners inline through the means of closures. In Groovy, listeners closure are used as a Listener Adapter where only one method of interest is overridden.
Explain how you can add stuff to the classpath when running things in groovy or groovysh?
You can add things to your $CLASSPATH environment variable.  Another possibility is to build a groovy/lib directory in your home directory and append whatever jars you want to be available by default.
Mention what is the license for Groovy?
Groovy depend at runtime on the ASM library as well as Java 1.4 and the Groovy jar.
Explain what is ExpandoMetaClass in Groovy?
ExpandoMetaClass is used to add methods, properties, static methods and constructors. Expandoclass does not inherited by default; you have to call ExpandoMetaClass.enableGlobally().
Explain how Groovy string is expressed?
Groovy string is referred as Gstring.
·         It is surrounded by double quotes, for regular strings it uses single quotes
·         It may contain Groovy Expressions noted in ${}
·         Square bracket syntax may be applied like charAt(i)
How could you retrieve a single value from data base using Groovy?
To recover a single value from the database you can use the command
row = sql.firstRow (‘select columnA, column from tableName’)
println “Row: columnA = $ {row.columnA} and column = ${row.columnB}”
Explain how you can query in Groovy?
Let see a simple example of how Groovy calls out the query
import groovy.sql.sql
sql = Sql.newInstance (‘jdbc: jtds: sqlserver://serverName/dbName-Class;domain=domainName’,’username’,’password’,’net.sourceforge.jtds.jdbc.driver’)
sql.eachRow (‘select * from tableName’) {print “$it.id–${it.firstName} –“  }
Explain how you can build AST (Abstract Syntax Trees) in Groovy from string?
You can build AST in Groovy from
·         Strings
·         Code
·         From DSL like specification
An AstBuilder object provides an API to build AST from strings of Groovy Source Code. For example
List<ASTNode> nodes = new AstBuilder (). buildFromString (“\”Hello\” “)
Explain how you can include a groovy script in another groovy?
You can include a groovy script with another groovy by using the following code. When put this code at the top of the script it will bring in the contents of a groovy file.
Evaluate(new file(“../tools/Tools.groovy”))
Explain what is Groovysh?
Groovysh is a command line application that enables an easy access to evaluate Groovy expressions, define classes and run experiments.
Explain GroovyDoc comment?
Like multiline comments, GroovyDoc comments are multiline but it starts with a /** and end with */.  Those comments are related with
·         Type definitions (classes, interfaces, enums, annotations)
·         Fields and properties definitions
·         Methods definitions
Explain what are Bitwise Operators in Groovy?
Bitwise operators can be implemented on a BYTE or an INT and return and INT. Bitwise operators offer 4 bitwise operators
·         &: bitwise “and”
·         I: bitwise “or”
·         A: bitwise “xor”
·         ~: bitwise negation
List out the differences between Groovy and Java?
·         All the packages and classes in Groovy is imported by default, you do not have to use the import statement explicitly
·         Unlike Java where the methods are chosen at compile time, the methods in the Groovy are chosen based on the types of arguments at runtime
·         In {…} block is reserved for closures, which means that you cannot build array literals with this syntax
·         Like in Java, omitting a modifier on a field does not result in a package private field
·         Automatic Resource Management or ARM block from java 7 are not supported in Groovy
·         Java 8 lambdas are more or less considered as anonymous inner classes, and Groovy does not support that syntax
Explain the role of Grape dependency in Groovy?
Grape is a JAR dependency manager included into Groovy.  It allows you to add quickly maven repository dependencies to your classpath, making scripting easier. The simplest use is adding an annotation to your script.
Explain what does the JsonSlurper class indicates?
The JsonSlurper is a class that parses JSON text or reader content into Groovy data structures (objects) such as lists, maps, and primitive types like double, Boolean, string and Integer.
When “propertyMissing (String)” method is called?
The “propertyMissing (String)” method is called when no getter method for a given property can be detected by the Groovy runtime.
Mention what relational operators is used for in Groovy?
Relational operators allows you to compare between objects, to check whether the two objects are different or same or if one is less than, greater than or equal to others.
Why Groovy? Why don’t you just use Jython, JRuby, bsh, rhino, pnuts, …
Firstly ports of existing languages like Python, Ruby, Smalltalk and JavaScript to the JVM are a good thing and we welcome them. If you already use and/or are fond of these languages please be our guests to use the Java-port of them.One of the main design goals of Groovy is to be a scripting language for Java developers to use. So we wanted to reuse both Java’s semantics and the whole set of J2SE APIs rather than introduce a port of a different language with different semantics and APIs to learn and implement/maintain.
e.g. in Groovy, java.lang.Object is the root of the object hierarchy, Object.equals(), Object.hashCode() and Comparable are used for comparions and lookups of objects, that java.util.List and java.util.Map are used for collections, Java Beans are fully supported and that Java and Groovy classes are interchangable inside the VM. Groovy is built on top of the J2SE APIs, rather than having 2 parallel platforms etc.
In other words we wanted the Groovy language to be very easy to pick up if you’re already a Java developer and for there to be a very small number of new APIs to learn. By this statement we’re not implying that Python / Ruby / JavaScript are hard to learn per se – its just there’s more to know, things are more different and there’s more APIs to learn
Think of Groovy as a Ruby or Python like language that is tightly integrated with the Java platform (as opposed to the Unix/Posix command shell and C-libraries) – allowing you the same powerful and concise coding syntax as Ruby or Pyton but allowing you to stay on the JVM and protect your investment in J2SE, J2EE and all the plethora of great useful Java code out there without any adapter layers or parallel API sets etc. There is a more detailed set of comparisio ther languages here
What is the licence for Groovy?
Groovy is open source using a BSD / Apache style licence
I get errors when trying to run groovy, groovysh or groovyConsole. Whats wrong?
Groovy depends on JDK 1.4 or later. Common errors people have when trying to run Groovy is that there’s an old groovy jar on the CLASSPATH somewhere (have you checked in java/lib/ext?) or that JAVA_HOME points to an old JDK before JDK 1. For more help please see this description of running Groovy code.
How can I add stuff to the classpath when running things in groovysh or groovy?
You can add things to your $CLASSPATH environment variable. Another popular option is to create a .groovy/lib directory in your home directory and add whatever jars you want to be available by default. e.g. if you wish to connect to your favourite JDBC database and do some scripting with it then add your JDBC driver to ~/.groovy/lib.
Things work if I use Suns conventions and put { on the same line, but if I add a new line things break?

When using closures with method calls we have some syntax sugar in Groovy which is sensitive to white space (newlines to be precise). Please see this description in common gotchas for a full description.
Request to Download PDF

Post A Comment:

0 comments: