Groovy基础语法 - 数字类型

Groovy支持不同类型的积分文字和十进制文字,由通常类型的Java支持

1.整形数字

整型文字类型与 Java 中的相同:

  • byte
  • char
  • short
  • int
  • long
  • java.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 中的相同:

  • float
  • double
  • java.math.BigDecimal
    您可以使用以下声明创建这些类型的十进制数:
// primitive types
float  f = 1.234
double d = 2.345

// infinite precision
BigDecimal bd =  3.456

小数可以使用指数,带有eE指数字母,后跟可选符号和表示指数的整数:

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作为其十进制数类型。此外,floatdouble两者都受支持,但需要显式类型声明、类型强制或后缀。即使 java.math.BigDecimal是十进制数的默认值,此类文本在采用floatdouble作为参数类型的方法或闭包中也被接受。

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.数学运算

  • bytecharshortint这四种类型和int类型之间的二进制运算的数据类型是int
  • bytecharshortintlong这五种类型和long类型之间的二进制运算的数据类型是long
  • BigInteger类型和其他整数类型运算的数据类型是BigInteger
  • bytecharshortintlongBigIntegerBigDecimal这七种类型和BigDecimal类型之间的二进制运算的数据类型是BigDecimal
  • floatdoubleBigDecimal这三种类型之间的二进制运算的数据类型是double
    运算类型.png

由于 Groovy 的运算符重载,通常的算术运算符也可以与BigIntegerBigDecimal一起使用,这与 Java 中不同,在 Java 中,您必须使用显式方法来操作这些数字

4.1除法运算符的情况

除法运算符/(以及/=除法和赋值)如果任一操作数是 floator doubleBigDecimal,则产生double结果,否则产生BigDecimal结果(当两个操作数都是整数类型bytecharshortintlongBigIntegerBigDecimal 的任意组合)。

4.2指数运算符的情况

指数操作由运算符**表示,具有两个参数:基数和指数。指数运算的结果取决于其操作数和运算结果(特别是如果结果可以表示为整数值)。
Groovy 的指数操作使用以下规则来确定结果类型:

  • 如果指数是十进制值
    • 如果结果可以表示为 Integer,则返回Integer
    • 否则,如果结果可以表示为 Long,则返回Long
    • 否则返回Double
  • 如果指数是整数值
    • 如果指数严格为负数,则返回 一个IntegerLong ,或者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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,835评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,598评论 1 295
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,569评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,159评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,533评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,710评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,923评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,674评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,421评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,622评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,115评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,428评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,114评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,097评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,875评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,753评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,649评论 2 271