Groovy支持不同类型的积分文字和十进制文字,由通常类型的Java支持
1.整形数字
整型文字类型与 Java 中的相同:
bytecharshortintlongjava.math.BigInteger
您可以使用以下声明创建这些类型的整数:
// primitive types
byte b = 1
char c = 2
short s = 3
int i = 4
long l = 5
// infinite precision
BigInteger bi = 6
如果使用def关键字使用可选键入,则整数的类型将有所不同:它将适应可以容纳该数字的类型的容量。
对于正数:
def a = 1
assert a instanceof Integer //true
// Integer.MAX_VALUE
def b = 2147483647
assert b instanceof Integer //true
// Integer.MAX_VALUE + 1
def c = 2147483648
assert c instanceof Long //true
// Long.MAX_VALUE
def d = 9223372036854775807
assert d instanceof Long //true
// Long.MAX_VALUE + 1
def e = 9223372036854775808
assert e instanceof BigInteger //true
以及负数:
def na = -1
assert na instanceof Integer //true
// Integer.MIN_VALUE
def nb = -2147483648
assert nb instanceof Integer //true
// Integer.MIN_VALUE - 1
def nc = -2147483649
assert nc instanceof Long //true
// Long.MIN_VALUE
def nd = -9223372036854775808
assert nd instanceof Long //true
// Long.MIN_VALUE - 1
def ne = -9223372036854775809
assert ne instanceof BigInteger //true
数字也可以用二进制、八进制、十六进制和十进制基表示。
1.2二进制数字
二进制数字以0b前缀开头:
int xInt = 0b10101111
assert xInt == 175 //true
short xShort = 0b11001001
assert xShort == 201 as short //true
byte xByte = 0b11
assert xByte == 3 as byte //true
long xLong = 0b101101101101
assert xLong == 2925l //true
BigInteger xBigInteger = 0b111100100001
assert xBigInteger == 3873g //true
int xNegativeInt = -0b10101111
assert xNegativeInt == -175 //true
1.3八进制数字
二进制数字以0前缀开头:
int xInt = 077
assert xInt == 63 //true
short xShort = 011
assert xShort == 9 as short //true
byte xByte = 032
assert xByte == 26 as byte //true
long xLong = 0246
assert xLong == 166l //true
BigInteger xBigInteger = 01111
assert xBigInteger == 585g //true
int xNegativeInt = -077
assert xNegativeInt == -63 //true
1.4十六进制数字
二进制数字以0x前缀开头:
int xInt = 0x77
assert xInt == 119
short xShort = 0xaa
assert xShort == 170 as short
byte xByte = 0x3a
assert xByte == 58 as byte
long xLong = 0xffff
assert xLong == 65535l
BigInteger xBigInteger = 0xaaaa
assert xBigInteger == 43690g
Double xDouble = new Double('0x1.0p0')
assert xDouble == 1.0d
int xNegativeInt = -0x77
assert xNegativeInt == -119
1.5十进制数字
十进制文字类型与 Java 中的相同:
floatdouble-
java.math.BigDecimal
您可以使用以下声明创建这些类型的十进制数:
// primitive types
float f = 1.234
double d = 2.345
// infinite precision
BigDecimal bd = 3.456
小数可以使用指数,带有e或E指数字母,后跟可选符号和表示指数的整数:
assert 1e3 == 1000.0 //true
assert 2E4 == 20000.0 //true
assert 3e+1 == 30.0 //true
assert 4E-2 == 0.04 //true
assert 5e-1 == 0.5 //true
为了方便精确地计算十进制数,Groovy 选择java.math.BigDecimal作为其十进制数类型。此外,float、double两者都受支持,但需要显式类型声明、类型强制或后缀。即使 java.math.BigDecimal是十进制数的默认值,此类文本在采用float或double作为参数类型的方法或闭包中也被接受。
2.数字中的下划线
在书写长字面数字时,很难弄清楚一些数字是如何组合在一起的,例如数千个单词的组等。通过允许您在数字文本中放置下划线,可以更轻松地发现这些组:
long creditCardNumber = 1234_5678_9012_3456L
long socialSecurityNumbers = 999_99_9999L
double monetaryAmount = 12_345_132.12
long hexBytes = 0xFF_EC_DE_5E
long hexWords = 0xFFEC_DE5E
long maxLong = 0x7fff_ffff_ffff_ffffL
long alsoMaxLong = 9_223_372_036_854_775_807L
long bytes = 0b11010010_01101001_10010100_10010010
3.数字类型后缀
我们可以通过给出一个后缀(见下表)来强制一个数字(包括二进制,八进制和十六进制)具有特定的类型,无论是大写还是小写。
| 类型 | 后缀 |
|---|---|
| BigInteger |
G or g
|
| Long |
L or l
|
| Integer |
I or i
|
| BigDecimal |
G or g
|
| Double |
D or d
|
| Float |
F or f
|
例子:
assert 42I == Integer.valueOf('42') //true
assert 42i == Integer.valueOf('42') // lowercase i more readable //true
assert 123L == Long.valueOf("123") // uppercase L more readable //true
assert 2147483648 == Long.valueOf('2147483648') // Long type used, value too large for an Integer //true
assert 456G == new BigInteger('456') //true
assert 456g == new BigInteger('456') //true
assert 123.45 == new BigDecimal('123.45') // default BigDecimal type used //true
assert .321 == new BigDecimal('.321') //true
assert 1.200065D == Double.valueOf('1.200065') //true
assert 1.234F == Float.valueOf('1.234') //true
assert 1.23E23D == Double.valueOf('1.23E23') //true
assert 0b1111L.class == Long // binary //true
assert 0xFFi.class == Integer // hexadecimal //true
assert 034G.class == BigInteger // octal //true
4.数学运算
-
byte、char、short、int这四种类型和int类型之间的二进制运算的数据类型是int -
byte、char、short、int、long这五种类型和long类型之间的二进制运算的数据类型是long -
BigInteger类型和其他整数类型运算的数据类型是BigInteger -
byte、char、short、int、long、BigInteger、BigDecimal这七种类型和BigDecimal类型之间的二进制运算的数据类型是BigDecimal -
float、double、BigDecimal这三种类型之间的二进制运算的数据类型是double
运算类型.png
由于 Groovy 的运算符重载,通常的算术运算符也可以与
BigInteger和BigDecimal一起使用,这与 Java 中不同,在 Java 中,您必须使用显式方法来操作这些数字
4.1除法运算符的情况
除法运算符/(以及/=除法和赋值)如果任一操作数是 floator double或 BigDecimal,则产生double结果,否则产生BigDecimal结果(当两个操作数都是整数类型byte 、char 、short、int、long、BigInteger 或BigDecimal 的任意组合)。
4.2指数运算符的情况
指数操作由运算符
**表示,具有两个参数:基数和指数。指数运算的结果取决于其操作数和运算结果(特别是如果结果可以表示为整数值)。
Groovy 的指数操作使用以下规则来确定结果类型:
- 如果指数是十进制值
- 如果结果可以表示为
Integer,则返回Integer - 否则,如果结果可以表示为
Long,则返回Long - 否则返回
Double
- 如果结果可以表示为
- 如果指数是整数值
- 如果指数严格为负数,则返回 一个
Integer、Long,或者Double,如果结果值适合该类型 - 如果指数为正数或零
- 如果基数为
BigDecimal,则返回结果值BigDecimal - 如果基数为
BigDecimal,则返回结果值BigInteger - 如果基数是
Integer,则返回Integer,如果结果值适合它,否则BigInteger - 如果基数是
Long,则返回Long,如果结果值适合它,否则BigInteger
我们可以用几个例子来说明这些规则:
- 如果基数为
- 如果指数严格为负数,则返回 一个
// base and exponent are ints and the result can be represented by an Integer
assert 2 ** 3 instanceof Integer // 8
assert 10 ** 9 instanceof Integer // 1_000_000_000
// the base is a long, so fit the result in a Long
// (although it could have fit in an Integer)
assert 5L ** 2 instanceof Long // 25
// the result can't be represented as an Integer or Long, so return a BigInteger
assert 100 ** 10 instanceof BigInteger // 10e20
assert 1234 ** 123 instanceof BigInteger // 170515806212727042875...
// the base is a BigDecimal and the exponent a negative int
// but the result can be represented as an Integer
assert 0.5 ** -2 instanceof Integer // 4
// the base is an int, and the exponent a negative float
// but again, the result can be represented as an Integer
assert 1 ** -0.3f instanceof Integer // 1
// the base is an int, and the exponent a negative int
// but the result will be calculated as a Double
// (both base and exponent are actually converted to doubles)
assert 10 ** -1 instanceof Double // 0.1
// the base is a BigDecimal, and the exponent is an int, so return a BigDecimal
assert 1.2 ** 10 instanceof BigDecimal // 6.1917364224
// the base is a float or double, and the exponent is an int
// but the result can only be represented as a Double value
assert 3.4f ** 5 instanceof Double // 454.35430372146965
assert 5.6d ** 2 instanceof Double // 31.359999999999996
// the exponent is a decimal value
// and the result can only be represented as a Double value
assert 7.8 ** 1.9 instanceof Double // 49.542708423868476
assert 2 ** 0.1f instanceof Double // 1.0717734636432956
