Groovy Tutorial for Java Developers – Part 1: The Basics
发布日期:2021-10-10 12:33:20 浏览次数:1 分类:技术文章

本文共 8518 字,大约阅读时间需要 28 分钟。

Groovy is an object-oriented programming language which could be described as Java paired with a dynamic scripting language like JavaScript or Python. You can use Groovy in two different “modes”: You can compile Groovy to JVM Bytecode like Java. 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.g. the Groovy Gradle plugin) to use and build Groovy in your regular projects. 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):

$ groovy your-script.groovy

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.

Hello World

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.

System.out.println("Hello World");

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:

println "Hello World"

What has happened to the line?

  • println is a short form of System.out.prinln
  • You don’t need semicolons in Groovy to end your statements (though you are still free to place them)
  • You don’t need the parantheses for the method call. If Groovy can detect what of the following code is the argument to the method, you can omit them.

Variables

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:

def x = 42println x.getClass()x = "Hello World"println x.getClass()

This script produces the output:

class java.lang.Integerclass java.lang.String

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.

Strings

Groovy has a String implementation called GString which allow to add variables into the String (so called String interpolation).

def x = "World"println "Hello, $x"

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.g.:

def firstName = "Douglas"def name = "Adams"println "Hello, ${firstName[0]}. $name"

This will produce the output Hello, D. Adams. If you want to use the array access operator [x] you will need the curly braces. You might have noticed, that we can access a specific character in the string like we can access an array. Groovy offers that support, due to its operator overloading and meta classes (which we will see further down in this tutorial). Instead of using double quotes (") you can also use single quotes (') as string delimiters. The difference is, that single quotes will always create a plain Java string without any interpolation features whereas double quotes will create a GString if necessary so that interpolation works. Therefor you will need to escape the dollar sign in a double quotes string, if you want to print out the dollar sign itself. Groovy also has support for multiline strings. Just use three double or single quotes (with the same meaning explained above) to create a multiline string:

def s = """This isa multilinestring"""

Implicit Truthy

Like JavaScript Groovy evaluates every object to a boolean value if required (e.g. when using it inside an if or when negating the value).

if("foobar") ...if(42) ...if(someObject) ...

All these statements are valid ifs. Groovy uses the following rules to convert any type to boolean:

  • Strings: If empty false, otherwise true
  • Collections and Maps are true if they are not empty
  • All non-zero numbers are true
  • Matchers (from a regular expression check) are true if they found at least one match (see next chapter)
  • Iterators with further elements are true
  • Object references are true if they aren’t null (you can define a custom truthy logic for your classes by implementing the boolean asBoolean() method)
    For detailed examples on the truthy conversion you can check the official documentation.

Regular Expressions

Groovy has some syntactic sugar around regular expressions. You can define a regular Pattern class by placing the ~ in front of a string. Besides the already mentioned single quote and double quotes there is a third method to define a string, which is primarily meant for regular expressions. You can define a string inside two slashes /../. Strings defined that way can use interpolation and support multiline. The main difference (in contrast to """) is, that you don’t need to escape a backslash character, which you often need in regular expression patterns. Therefor you need to escape the slash character.

def pattern = ~/a slash must be escaped \/ but backslash, like in a digit match \d does not/println pattern.getClass()

This script will show you, that pattern is of type java.util.regex.Pattern.

If you want to compare a string against a regular expression (and get the Matcher to read out the groups) you can use the find operator =~.

It takes a string on the left side and a string containing a regular expression on the right side and returns a Java Matcher, which gives access to the result. Since a Matcher instance evaluates to true, when it found at least one result, you can easily use this in an if statement:

def matcher = "The Hitchhiker's Guide to the Galaxy" =~ /Galaxy/if (matcher) {  println "Found the word 'Galaxy'"}

Often you want to access the result of the matcher. You can use the array index for that:

def m = "Groovy is groovy" =~ /(G|g)roovy/println m[0][0] // The first whole match (i.e. the first word Groovy)println m[0][1] // The first group in the first match (i.e. G)println m[1][0] // The second whole match (i.e. the word groovy)println m[1][1] // The first group in the second match (i.e. g)

New and noteworthy Operators

Groovy has some noteworthy new operators, that doesn’t really belong in any other of the chapters in this tutorial, so they should be introduced at this point.

Safe Navigation Operator

If you want to access a property of an object “nested inside” you have to check all the parent objects for null or your code will throw a NullPointerException. Let’s look at the following code snippet:

if(company.getContact() != null    && company.getContact().getAddress() != null    && company.getContact.getAddress().getCountry() == Country.NEW_ZEALAND) { ... }

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. This is code noise, that Groovy prevents with the safe navigation operator ?. Using this operator instead of the dot no NullPointerException will be thrown if any part is null. Instead the whole result of the navigation will be null:

if(company.getContact()?.getAddress()?.getCountry() == Country.NEW_ZEALAND) { ... }

If the contact or the address are null, the result of the left side will just be null, but no exception will be thrown.

Elvis Operator

The ternary operator in Java is often use to assign default values if an actual value is not present, like in the following example:

def name = client.getName() != null ? client.getName() : ""

If you want to assign a value or if its not “present” (i.e. if it evaluates to false) a default value, you can use the elvis operator ?:

def name = client.getName() ?: ""

That way Groovy will assign client.getName() if it isn’t false (in case of a string that means not null and not empty) or the empty string otherwise.

Why is this called the Elvis Operator? Turn your head around and look at the smileys hair.

What’s next?

This tutorial has described the basics in Groovy. The next part goes into detail on the usage of closures in Groovy.

转载地址:https://blog.csdn.net/qq_26847293/article/details/51321302 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Apache ActiveMQ教程(一)
下一篇:Extjs4 grid的排序

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2024年03月22日 13时04分21秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

java构造函数有什么用_java构造函数有什么用,怎么用 2019-04-21
mysql 匹配 隔开的_按空格分隔关键字并搜索MySQL数据库 2019-04-21
java factory用法_怎样使用Java实现Factory设计模式 2019-04-21
java窗口内容如何复制_求助Java窗口菜单如何实现复制粘贴剪切等功能(内附源代码)... 2019-04-21
盾神与砝码称重java_[蓝桥杯][算法提高VIP]盾神与砝码称重 2019-04-21
java输出狗的各类信息_第九章Java输入输出操作 2019-04-21
java notify怎么用_java 如何使用notify() 2019-04-21
java加载指定文件为当前文本,java:如何使用bufferedreader读取特定的行 2019-04-21
java metrics 怎么样,Java metrics 2019-04-21
在vscode中php语言配置,Visual Studio Code C / C++ 语言环境配置 2019-04-21
php怎么翻译数据库中的中文,javascript – 如何将翻译后的文本插入数据库php 2019-04-21
普朗克公式matlab,用MATLAB实现普朗克函数积分的快捷计算.pdf 2019-04-21
swoolec+%3c?php,PHP+Swoole并发编程的魅力 2019-04-21
php 404配置,phpcms如何配置404 2019-04-21
matlab wash矩阵产生,洗衣机净衣效能与衣损程度的关系分析 2019-04-21
php中如何调用sql server,php调用SQL SERVER 2008及以上版本的方法 2019-04-21
python多线程实现kmeans,3种方式实现python多线程并发处理 2019-04-21
matlab 变量不存在,matlab程序运行时提示变量未定义 2019-04-21
php编码函数 base58,1. Base58可逆加密 2019-04-21
oracle 在需要下列之一,Oracle存储过程中PLS-00103:出现符号“/”在需要下列之一时:(... 2019-04-21