Skip to content

Instantly share code, notes, and snippets.

@strongant
Created August 13, 2017 06:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save strongant/22c489f5a51f416d1bf3e97d86f031dd to your computer and use it in GitHub Desktop.
Save strongant/22c489f5a51f416d1bf3e97d86f031dd to your computer and use it in GitHub Desktop.
Groovy中对变量的定义和使用
/**
* Groovy中对不同类型变量的定义和使用
* @author strongant
* Contact me at <a href="mailto:strongant1994@gmail.com">strongant1994@gmail.com</a>
* @see
* @since 2017/8/13
*/
class DefineUseVariable {
public static void main(String[] args) {
// 声明一个值为admin,类型为String的变量
def name = 'admin'
// 声明一个值为10的数字,类型为Integer
def age = 10
// 声明一个值为20.0的数字,类型为BigDecimal
def money = 20.0
// 声明一个值为true的布尔变量,类型为Boolean
def isMale = true
// 再次声明一个值为admin,类型为String的变量,下面代码将运行错误。
//def name = "Bob"
// 输出变量的值和类型。
//注意,在输出的时候,如果要使用'$'符号进行输出变量,则字符串必须使用双引号的三双引号的情况下,才可以正常输出变量值。
// 如果是单引号,则不会输出变量值,原样输出
println "name is ${name}. I'm ${age} years old. I hava ${money} dollars."
println "name type: ${name.class}"
println "age type: ${age.class}"
println "money type: ${money.class}"
println "isMale type: ${isMale.class}"
// 声明一个值为10.0的浮点型的变量,类型为Float
def floatTypeVariable = 10.0F
println "floatTypeVariable type: ${floatTypeVariable.class}"
// 声明一个值为10.0的浮点型的变量,类型为Double
def doubleTypeVariable = 10.0D
println "doubleTypeVariable type: ${doubleTypeVariable.class}"
// 声明一个值为10000的长整型的变量,类型为Long
def longTypeVariable = 10000L
println "longTypeVariable type: ${longTypeVariable.class}"
// 声明一个值为10000的长整型的变量,类型为大长整型BigInteger
def bigIntegerTypeVariable = 10000G
println "bigIntegerTypeVariable type: ${bigIntegerTypeVariable.class}"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment