GroovyCookBook: Numbers

GroovyCookBook: Numbers,第1张

GroovyCookBook: Numbers

NumbersIntroduction

Groovy makes heavy use of Java's numeric capabilities. This means that Groovy supports both the primitive and object number types available in Java but with a few enhancements thrown in.

The main highlights are that Groovy provides a more consistent view of numeric types to the developer. This is achieved through two main mechanisms:

  • Dealing with primitives is made to look like dealing with objects. E.g. you can go "42.toString()". In fact, primitives are autoboxed to their wrapper equivalents most of the time. Where needed for Java integration purposes, primitive types are retained.
  • Dealing with BigIntegers and BigDecimals (as well as your own types and 3rd part library types like Matrix or Complex number) is made to be the same as dealing with the simpler types, e.g. if I have three BigInteger or three Matrix instances called a, b and c, then I can write an expression like def d = (a + b) * c. This is achieved through operator overloading.

These features align with some of the Groovy language's guiding philosophies, namely "everything is an object" and "improved consistency where possible".

Primitive numeric types

The following primitive types are supported:

Name Description Allowed values Example byte8-bit signed two's complement-128 to 12742short16-bit signed two's complement-32,768 to 32,76720000int32-bit signed two's complement-2,147,483,648 to 2,147,483,6471000000000long64-bit signed two's complement-9,223,372,036,854,775,808 to +9,223,372,036,854,775,8071000000000000000000float32-bit IEEE 754 floating-point value1.40129846432481707e-45 to 3.40282346638528860e+383.5fdouble64-bit IEEE 754 floating-point value4.94065645841246544e-324d to 1.79769313486231570e+308d3.5d

Primitives are nearly always converted ("boxed") to their wrapper types. Autoboxing and unboxing happens automatically behind the scenes if integrating with Java APIs.

Class numeric types

In addition to the primitive types, the following object types (sometimes referred to as wrapper types) are allowed:

Name java.lang.Bytejava.lang.Shortjava.lang.Integerjava.lang.Longjava.lang.Floatjava.lang.Double

As well as the following classes for supporting arbitrary precision arithmetic:

Name Description Example java.math.BigIntegerImmutable arbitrary-precision signed integral numbers30gjava.math.BigDecimalImmutable arbitrary-precision signed decimal numbers3.5g

Java automatically imports all classes from the java.lang package. Groovy also does this for the BigInteger and BigDecimal classes, so you don't need to import any of these classes.

Declaring numeric variables

You can declare variables using either the primitive or wrapper variants (and optionally provide an initializing literal):

byte b1 = 42
Byte b2 = 42
short s1 = 20000
Short s2 = 20000
int i1 = 1000000000
Integer i2 = 1000000000
long l1 = 1000000000000000000
Long l2 = 1000000000000000000
Long l3 = 42L
float f1 = 3.5f
Float f2 = 3.5f
double d1 = 3.5e40
Double d2 = 3.5e40
Double d3 = 3.5d
BigInteger bi = 30g
BigDecimal bd = 3.5g
[b1, b2, s1, s2, i1, i2, l1, l2, l3, f1, f2, d1, d2, d3, bi, bd].each{ println it.dump() }

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/zaji/2085090.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-07-22
下一篇2022-07-22

发表评论

登录后才能评论

评论列表(0条)

    保存