一、标准输出
class FirstTest{
static void main(String[] args) {
// 输出内容会换行
println "后面有换行"
// 输出内容不会换行
print "后面没有换行"
}
}
二、导入其他类
- 不需要导入的类
import java.lang.*
import java.util.*
import java.io.*
import java.net.*
improt groovy.lang.*
import groovy.util.*
import java.math.BigInteger
import java.math.BigDecimal
- 需要导入的类
import groovy.xml.MarkupBuilder
class FirstTest{
static void main(String[] args) {
def xml = new MarkupBuilder()
}
}
三、注释
class FirstTest{
static void main(String[] args) {
// 这是一个单行注释
/*
这是一个多行注释
多行注释的第二行
*/
}
}
四、标识符
- 标识符被用来定义变量、函数或其他用户定义的变量。
- 标识符以字母开头、美元或下划线。不能以数字开头
class FirstTest{
static void main(String[] args) {
def student
def $xiaoming
def _teacher
}
}
五、关键字
关键字作为名称建议是在Groovy编程语言中保留的特殊字。
as | assert | break | case | catch |
---|---|---|---|---|
class | const | continue | def | default |
do | else | enum | extends | false |
Finally | for | goto | if | implements |
import | in | instanceof | interface | new |
pull | package | return | super | switch |
this | throw | throws | trait | true |
try | while |