Groovy Helpful Tips

Posted on by By Somen Sarkar, in Helical Insight, Uncategorized | 0

Groovy Helpful Tips

Here are some useful information when using groovy

Single Line comment in Groovy
// We can comment using // in groovy as in Java.


Multi Line comment in Groovy
/* Multi-line comment style is same as in Java */


Documentation comment in Groovy
/**
* Similar to Java commenting Style
*/


Shebang style is helpful when running Groovy script directly from linux system
#!/usr/bin/env groovy


as and trait are keyword in Groovy


”’ Triple quoted string help to write strings in multiple line eg.
def multiLineString = ”’
line one
line two
line three
”’


String interpolation

def name = ‘Helical’ // a plain string
def greet = “Hello ${name}” //This will result in Hello Helical


List in groovy.

by default list are ArrayList
def numbers = [1, 2, 3]


To define a Linked List
def linkedList = [2, 3, 4] as LinkedList //Note the usage of as keyword


To define map

def capitalInfo = [state: ‘Delhi’, district: ‘South Delhi’, pin: ‘110019’]

capitalInfo[‘metroStation‘] = ‘Nehru Place’ //adds a new key in the map


Elvis operator
The “Elvis operator” is a shortening of the ternary operator.
userName = user.name ? user.name : ‘Guest’ //classic way
userName = user.name ?: ‘Guest’ //elvis way

logo

Best Open Source Business Intelligence Software Helical Insight is Here

logo

A Business Intelligence Framework

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments