Groovy In Action 学习笔记(一) Groovy初体验

Groovy 环境的安装

1.         下载 Groovy-binary-1.6.0.zip

http://groovy.codehaus.org/

2.         配置

a)      配置环境变量 GROOVY_HOME

b)      配置 path 变量 %GROOVY_HOME%\bin;

3.         试运行

C:\Documents and Settings\Administrator>groovy -version

Groovy Version: 1.6.0 JVM: 1.5.0_11

4.         注意事项

a)         请确认配置好了 JAVA_HOME Java 可以正常运行

 

Groovy 简介

1.         基于 jvm

2.         脚本语言

3.         使用 java 编写

4.         可编译成 .class 文件

 

书写 Groovy 的HelloWorld

使用 groovyConsole 书写 HelloWorld (groovyConsole如何使用,下面一个章节会有介绍)

 

1.    使用groovyConsole书写HelloWorld
// HelloWorld.java
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}

// HelloWorld.groovy
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}

代码完全一样。
相当于将HelloWorld.java直接改名成HelloWorld.groovy。

2.    继续简化 (省略public)
class HelloWorld {
    static void main(String[] args) {
        System.out.println("Hello World");
    }
}


3.    继续简化(省略HelloWorld类声明)
System.out.println("Hello World");

4.    继续简化(使用println)
println("Hello World");

5.    继续简化 (去掉括号、分号)
println "Hello World"

6.    修改(将双引号变成单引号)
println ’Hello World’

 

一段看似很简洁的java代码, 在Groovy中不断地进行简化, 易读易写, 这就是Groovy的魅力.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Thinking back to January 2007 when the first edition of this book hit the shelves, feels like time travel to the Middle Ages. The idea of using a programming language other than Java on the Java platform was widely considered frivolous. Today, a new language seems to pop up every other week, and we even go as far as designing languages for specific domains (DSLs) on a per-project basis. This evolution of languages reflects a change in concerns. If performance were still our utmost concern, we would all be coding in a low-level language. But if performance is considered “good enough” for our purposes, we now turn our focus on human approachability. Groovy has been a trendsetter for this development. Many Groovy features that ease the burden of developers are now commonplace in novel languages and may even find their way into newer versions of Java: literal declarations for common datatypes, simplified property access, null-safe dereferencing, closures, and more. Surprisingly many languages have adopted Groovy’s optional typing strategy—few languages can claim to have static and dynamic behavior at the same time, though, the way Groovy has since version 2. Just like Groovy, the first edition of this book set some trends as well. The idea of having every single listing as a self-testing piece of code resonated in the market and may be one reason why the book is among Manning’s top-ten bestsellers of the decade. Licensed to Mark Watson <nordickan@gmail.com> The feedback for the first edition was overwhelming. We never expected to have so many great developers speaking so nicely about our work. We have no words to express this feeling of being proud and humbled at the same time. Most touching, though, was the stranger who once gave Dierk a pat on the back and mumbled, “Thank you for the book!” and then disappeared into the crowd. This book is for him. We are fully aware that the first edition would have never been so successful if Groovy itself had been less appealing. The reason for Groovy’s success is easy to see: it delivers its power in the most Java-friendly manner. It is Java’s dynamic friend. The development of Groovy, from version 1.0 covered in the first edition of this book until the current version 2.4, has closed what used to be a syntax gap by providing enums, annotations, generics, the classic for loop, nested classes, varargs, static imports, and the ability to use Groovy closures where Java 8 expects lambda expressions. The Groovy project has progressed at a very high speed, not only in its core but also at its periphery. We see, for example, new usages of compile-time metaprogramming. This core feature gets instantly applied in the Spock testing framework, which in turn contributes back its “power assert” feature to the core. The community is buzzing and it has become a challenge to keep up to date with all the developments and activities. It’s only natural that many readers of the first edition of Groovy in Action (or “Gina” as we say for short) demanded an update that we are now happy to deliver as the second edition (codename “ReGina”). Our goal in this book is not only to rework the code examples, update the API description, and explain new features, but also to reflect the marketplace and the growth of the ecosystem. Groovy has evolved from a niche language to the default choice for dynamic programming on the Java platform for millions of developers. Major financial organizations use Groovy to transfer billions of dollars every day, space agencies watch the stars with the help of Groovy, and satellite live-data streams are handled by Groovy code. Groovy is traveling the oceans, shipping containers around the globe, helping software developers automate recurring tasks, and running Mom’s website. We felt an obligation to provide an up-to-date, solid, and comprehensive book to all these users. Not only did Groovy and its environment change, we authors changed as well. We enjoyed the luxury of working on Groovy projects, introducing new team members to the language, running workshops and tutorials, recognizing struggles (and occasionally struggling ourselves), finding lots of unanticipated use cases while consulting, exploring new practices, using the toolset in anger, and generally facing the Groovy development reality. The book reflects these experiences. In this second edition, we put more emphasis on the optional typing system, explain both dynamic and static metaprogramming in full depth, dive into type checking and static compilation, cover domain-specific languages, and introduce new modules that have evolved for user interfaces, testing, XML, JSON, database programing, Licensed to Mark Watson <nordickan@gmail.com> Web Services, dependency management, build automation, and concurrent programming as well as give you an updated overview of the Groovy ecosystem. We hope you will find this updated book an enjoyable and rewarding read.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值