groovy入门(一)Environment and syntax

groovy入门(一)Environment and syntax

首页
http://groovy.codehaus.org/
别人的BLOG推荐的电子书
http://javacoder-qq-com.iteye.com/blog/264209
官方入门文档
http://groovy.codehaus.org/Tutorial+1+-+Getting+started
照着官方入门文档学习一下吧,找了找,中文资料也不多,而且貌似也不是很火。
1.setting up your java environment
I have java environment already,my version of java is 1.6.0_11

2.setting up your Groovy environment
Unzip the groovy archive to some logical place on your hard drive,I have mine in C:\groovy-1.7.0
set GROOVY_HOME environment variables on WINDOWS, the value set to C:\groovy-1.7.0
add C:\groovy-1.7.0\bin to my system path
open C:\groovy-1.7.0\bin\groovyConsole.bat by double clicking.I see the console.
you can type command below in the command window to make sure you have the right version:

C:\Documents and Settings\sillycat>groovy -version
Groovy Version: 1.7.0 JVM: 1.6.0_11

3.setting up optional jar files
you have a file C:\groovy-1.7.0\conf\groovy-starter.conf,it show how the groovy loading jars,it seemd like this:
load !{groovy.home}/lib/*.jar
load !{user.home}/.groovy/lib/*.jar
load ${tools.jar}
use cmd on windows and use this command to create the filder:
cd C:\Documents and Settings\sillycat
mkdir .groovy
mkdir .groovy/lib
now simply place your jar files into the .groovy/lib directory.

4.first programe hello world

open the groovy console use command
>groovyconsole

type the code below in the groovy console:
println "hello,fuck"

and the type <CTRL + R> to run the programe.

next beginning to learn the sytax of groovy 1.7

Variables
you can assign values to varibles for later use. Try the following:
x = 1
println x
x = new java.util.Date()
println x
x = -3.1499392
println x
x = false
println x
x = 'Hi'
println x

Lists and Maps
The Groovy language has built-in support for two important data types, lists and maps(Lists can be operated as arrays in Java language). Lists are used to store ordered collections of data.
myList = [1776,-1,33,99,0]
You can access a given item in the list with square bracket notation (indexes start at 0):
println myList[0]
println myList.size()

Map is unordered collections.
scores = ['carl':168,'kiko':199]
println scores['carl']
println scores.kiko

scores["Peter"] = 3
println scores.Peter

we can create an empty map or an empty list with the following:
emptyMap = [:]
emptyList = []

Conditional Execution
execute different code under different conditions. use the 'if' construct. For examples:
temp = Calendar.getInstance().get(Calendar.AM_PM)
if(Calendar.AM == temp){
println('good morning')
} else {
println('good evening')
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值