Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
* Groovy中对不同类型变量的定义和使用
* @author strongant
* Contact me at <a href=""></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
You can’t perform that action at this time.