Groovy tutorial pdf

Sunday, November 25, 2018 admin Comments(0)

More than a tutorial on the Groovy language, Programming Groovy is more than just “Java++”—in nice, edible chunks for the Groovy pro- grammer to consume. The Groovy 2 Tutorial. An introductory text for Groovy developers. Duncan Dickinson. This book is for sale at I'm delighted that Venkat has revised this essential Groovy tutorial to reflect the Programming Groovy 2 is no exception—it is crafted with small palatable.

Language: English, Spanish, Hindi
Country: Bosnia Herzegovina
Genre: Children & Youth
Pages: 103
Published (Last): 16.05.2016
ISBN: 200-2-63907-708-1
ePub File Size: 25.67 MB
PDF File Size: 18.12 MB
Distribution: Free* [*Regsitration Required]
Downloads: 22474
Uploaded by: GLENN

A joint Java / Groovy compiler to let you mix and match Groovy and Java classes in .. Before proceeding through the content of this tutorial, please make sure to. Groovy Tutorial in PDF - Learn Groovy in simple and easy steps starting from Overview, Environment, Basic Syntax, Data Types, Variables, Operators, Loops. Groovy is an object oriented language which is based on Java platform. Groovy In this tutorial, we would explain all the fundamentals of Groovy and how.

This will produce the output Hello, D. If you want to use more complex statements in the string you need to add curly braces after the dollar sign, e. The ternary operator in Java is often use to assign default values if an actual value is not present, like in the following example:. If null is a valid data for a contact and an address, you have to check if they are not null before accessing their properties. SmartThings Classic Developer Documentation latest. Like JavaScript Groovy evaluates every object to a boolean value if required e.

For example, assert true is valid, and the program will continue. Anything that evaluates to false will cause the program to halt, so assert false will terminate with an informative message. Neither is the method println , for that matter. For security and performance reasons, SmartThings runs in a sandboxed environment that restricts access to certain features. The sandboxed environment is discussed further in the Groovy With SmartThings tutorial. Methods are the things the object can do, and similar to other languages, are optionally more on that later invoked with parentheses that may contain arguments.

In Groovy, we can use def in place of an explicit type.

Groovy Tutorial for Java Developers – Part 1: The Basics

The exact type of object that will be assigned will vary when using def. Why use def instead of explicit types? While not required, def is commonly used in Groovy and in SmartThings because it provides greater flexibility and readability.

In the example above, addThem is defined to accept two String parameters. What happens when we try to invoke addThem with two numbers? Because addThem is defined to accept two String parameters, we get a MissingMethodException when calling addThem 1, 2 , since there is no method named addThem that accepts two numbers.

If we use def instead of an explicit type, we can take advantage of something called duck typing. In programming terms, this means that if an object supports certain properties or methods, then we can use those regardless of its type.

To illustrate this with an example, consider the above example refactored to use def:. Omitting the explicit type information in favor of def allows us to build flexible programs without getting bogged down in ensuring we have all our typing information correct.

This is particularly useful for smaller programs, which is what you will be writing with SmartThings. Strict statically typed languages like Java determine the method that will be called at compile time.

Groovy determines the methods to invoke at runtime , using something called multi-methods or dynamic dispatch. You can read more about multi-methods here in the Groovy documentation. Groovy supports all the typical operators, such as arithmetic operators, assignment operators, and relational operators:. There a few other notable operators that you may not have seen in other languages; one of them is the Safe Navigation Operator.

Suppose we have a property named location , that also has a method getHelloHome. Further, suppose that the object returned by getHelloHome has a method named getPhrases. Ultimately, we want to get the phrases. But, what if getHelloHome returns null? That works, and is valid Groovy, but we can do better.

Groovy Tutorial in PDF

Using the safe navigation operator?. If any objects are null, the method simply will not be invoked and null will be returned. If it does return null, the whole expression simply returns null. There are many more Groovy operators documented here.

Strings defined with double quotes support interpolation. This allows us to substitute any Groovy expression into a String at the specified location.

Pdf groovy tutorial

Of course, more interesting interpolations are possible. Dotted expressions are expressions of the form a. There are some other handy Groovy String features, like the ability to remove part of a string using the - operator:.

You can read more about Strings here. While lists and maps are simple in Groovy, there are many powerful methods in the Groovy collections APIs that extend their power. You are encouraged to read the Groovy documentation for more information, but here are some cool examples:. You can also use the switch statement to handle possible values conditionally:.

When invoking methods, parentheses are sometimes optional. Methods that do not accept any parameters must include the parentheses. Groovy adds in some convenience JavaBean style getter and setter methods. How did referencing someValue end up invoking the method getSomeValue?

When Groovy sees a reference to the property named someValue , it first looks to see if it is defined somewhere. In the above example, it is not. So, Groovy then looks to see if there is a getter method. Methods are generally defined and invoked as in other modern languages, with some notable enhancements. The return statement is optional in a Groovy method.

The value of the last expression evaluated is returned by default:. Methods can also be defined to accept named parameters. This is frequently used in SmartThings, as it allows for flexible and easily-extendable methods. This is accomplished by accepting a Map parameter the typing is optional, but used here for clarity:.

Methods can also define default values for parameters. If not passed when calling the method, the default will be used:. Worth noting is that none of the above definitions include any type of explicit visibility modifier information. By default, when using def , the method is public. Want to make your method private?

So, we typically just omit any visibility modifier for simplicity. Like other programming languages, Groovy has error conditions, or exceptions. Because Groovy is based on Java, there are similarities to how Java handles exceptions. The big difference is that Groovy does not require you to handle so-called checked exceptions. In Groovy, we are always free to handle exceptions if we want, or disregard them and let them percolate up the call stack.

First, consider a simple example. Say we have a List of numbers, and want to do something with each item in the list. All we really care about is doing something to each item! Consider an example where given a list of numbers, we want to know which numbers are greater than Without closures, we would probably write something like this:.

This is valid Groovy, but with the ability to use closures, we can write code that is much more expressive and concise:. Simply put, Groovy Closures are anonymous blocks of code that can be passed to other methods, and those methods can then call that block of code. The example above uses the findAll method that is available on all Groovy collections. Groovy is an object-oriented programming language which could be described as Java paired with a dynamic scripting language like JavaScript or Python.

That way you just need to package it with the groovy library and it can run in the JVM like Java can. You can use your build system e. Besides using it in a compiled way, you can use it as a scripting language. Just write your Groovy code inside a file and execute this file by calling once you have Groovy installed:. That way you can use Groovy just like you can use Python scripts for console usage. Besides running directly from the console, Groovy offers a scripting engine, which you can include into your Java or Java compatible application.

Using the scripting engine you can use Groovy as a scripting language inside your application, like Gradle does for their build scripts. To experiment with Groovy and test the code during this tutorial, you can also use the online Groovy Playground:.

Groovy Playground.

Tutorial pdf groovy

You can write Groovy code online and just execute it to quickly play around with some snippets. Please notice, that the playground currently has some limitations, like System. Groovy nearly is a superset of Java, which means most of the Java code is also valid Groovy code. It just adds a lot of syntactic sugar on top of Java. We will illustrate this with a short example.

Groovy Basics — SmartThings Classic Developer Documentation

This would be valid Java and valid Groovy. Except that you would need at least a class with a main method around it in Java to run.

In Groovy you can just place this inside a file, execute it via the console and it will work. But in Groovy we can shorten this line to:. In contrast to Java, you can use dynamic typing in Groovy.

To define a variable use the keyword def. That way Groovy will determine the type of the variable at runtime and the type might even change. For example the following snippet is valid Groovy code:.

As you can see the variable x changed its type during runtime. You are still free to define x with a type like int. In that case the second assignment would throw an error, because you explicitly told Groovy you want this variable to be of type int and it cannot implicitly cast a String to an Integer.

Groovy has a String implementation called GString which allow to add variables into the String so called String interpolation. This would produce the output Hello, World. The content of variable x is inserted into the string. If you want to use more complex statements in the string you need to add curly braces after the dollar sign, e.

This will produce the output Hello, D.