Groovy中所有类型均为引用类型。即使是Java中的8种Primitive Type,groovy中背后使用的都是对应的包装类。没有boxing,unboxing操作.
一下为一些基本类型操作
//Integer def age = 10 println("age++ " + (age++)) // age++ 10, 先输出,再自增 assert age == 11 println("++age " + (++age)) // ++age 12,先自增,再输出 assert age == 12 assert 12.toString() == age.toString() // 可直接对常量使用方法 assert 12.8.toInteger() == 12 // 可直接对常量使用方法 //float def weight_f = 1.2f def weight_F = 1.2F assert weight_f.class.name == Float.name assert weight_F.class.name == Float.name //double def weight_d = 1.2d def weight_D = 1.2D assert weight_d.class.name == Double.name assert weight_D.class.name == Double.name //long def meter_l = 1000L assert meter_l.class.name == Long.name //bigInteger def universe_age_g = 1000G assert universe_age_g.class.name == BigInteger.name //bigDecimal def decimal_default = 1.2 ///注意默认为 bigDecimal类型,和Java不同 def decimal_b= 1.2g assert decimal_default.class.name == BigDecimal.name assert decimal_b.class.name == BigDecimal.name // operators def compare_a = 20 def compare_b = 10 assert compare_a > compare_b assert compare_b < compare_a assert (compare_a <=> compare_b) == 1 // <=> 相当于 compareTo函数 assert 5 / 2 == 2.5 //默认bigDecimal assert 3 % 2 == 1 //取余数 a.mod(b) assert 5.intdiv(2) == 2 //整除 assert compare_a >> 2 == 5 //右移两位,相当于除以4 assert compare_a << 2 == 80 //左移两位,相当于乘以4 //动态数据类型变化 --- Groovy中所有类型均为 引用类型。 即使是Java中的8种Primitive Type,groovy中背后使用的都是对应的包装类。没有boxing,unboxing操作 def type_variant = 100 assert type_variant.class.name == Integer.name type_variant = 100f assert type_variant.class.name == Float.name type_variant = 100d assert type_variant.class.name == Double.name type_variant = "STRING" assert type_variant.class.name == String.name