1 Introduction
This article is the fourth article of Groovy learning, learning the Number value type in Groovy grammar, and the knowledge of Boolean Boolean type.
Learn about the creation of numeric types in Groovy, and the kinds of numeric types that are allowed.
2. Numbers numeric type
Groovy supports different types of integers and decimal numbers, which are inherited from Java. It can be said that the numerical types supported by java are also supported by Groovy.
2.1 Integer types
The integer types in Groovy are: byte,char,short,int,long,BigInteger
Integers of these types can be created using the following declarations, examples follow:
copy// basic data type byte b = 1 char c = 2 short s = 3 int i = 4 long l = 5 //Large precision data type BigInteger bi = 6
If an optional type is used by using the def keyword, the type of the integer value will change: it will adapt to the capacity of the type that holds the value.
(ps: The char type cannot be created by the def type)
copy// basic data type def a = 1 assert a instanceof Integer //Assert that this value is of type Integer // Integer.MAX_VALUE, int maximum value def b = 2147483647 assert b instanceof Integer //Assert that this value is of type Integer // Integer.MAX_VALUE + 1 def c = 2147483648 //Because the length of this value exceeds the range of int, it will automatically become long type. assert c instanceof Long //Assert that this value is of type long // Long.MAX_VALUE def d = 9223372036854775807 assert d instanceof Long //Assert that this value is of type long // Long.MAX_VALUE + 1 def e = 9223372036854775808 // If the length of the value exceeds the range of long, it will become a BigInteger length type. assert e instanceof BigInteger //Assert that this value is of type BigInteger
In the above example, it is for positive integers. The same is true for negative integers. Examples are as follows:
copydef na = -1 assert na instanceof Integer // Integer.MIN_VALUE def nb = -2147483648 assert nb instanceof Integer // Integer.MIN_VALUE - 1 def nc = -2147483649 assert nc instanceof Long // Long.MIN_VALUE def nd = -9223372036854775808 assert nd instanceof Long // Long.MIN_VALUE - 1 def ne = -9223372036854775809 assert ne instanceof BigInteger
assert This keyword is an assertion. If the result of the assertion is not satisfied, the program will end.
2.1.1 Non-decimal representation
Numbers can also be represented in binary, octal, hexadecimal, and decimal.
The binary value indicates the effect:
copyint xInt = 0b10101111 assert xInt == 175 short xShort = 0b11001001 assert xShort == 201 as short byte xByte = 0b11 assert xByte == 3 as byte long xLong = 0b101101101101 assert xLong == 2925l BigInteger xBigInteger = 0b111100100001 assert xBigInteger == 3873g int xNegativeInt = -0b10101111 assert xNegativeInt == -175
If you want to use binary representation, you must use 0b as the beginning
Octal value representation effect: The typical format for an octal number is 0 followed by the octal number. Examples are as follows:
copyint xInt = 077 assert xInt == 63 short xShort = 011 assert xShort == 9 as short byte xByte = 032 assert xByte == 26 as byte long xLong = 0246 assert xLong == 166l BigInteger xBigInteger = 01111 assert xBigInteger == 585g int xNegativeInt = -077 assert xNegativeInt == -63
Hexadecimal number: The typical format represented is 0x followed by a hexadecimal value. Examples are as follows:
copyint 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
2.2 Floating point type
Like the representation of floating-point numbers in java, the floating-point numbers in Groovy are: float,double,BigDecimal.
Floating point numbers are created as follows:
copyfloat f = 1.234 double d = 2.345 BigDecimal bd = 3.456
Decimals can use an exponent, the exponent letter e or E, followed by an optional sign, the example effect is as follows:
copyassert 1e3 == 1_000.0 assert 2E4 == 20_000.0 assert 3e+1 == 30.0 assert 4E-2 == 0.04 assert 5e-1 == 0.5
If we want to create a float or double, we must create it through the above creation method. Instead of using def for dynamic creation.
In groovy, floating-point numbers only support the dynamic creation of BigDecimal. That is to say, we create def temp=12.3, the default type is BigDecimal, not float type.
But we can tell the def type to be float or double by adding f or d to the parameter value, the example is as follows:
copydef s= 12.3 //Created is a BigDecimal object def f = 12.2f// Created is a Float object def d =12.3d //Created is a Double object def g= 12.3g //Created is a BigDecimal object
2.3 Underlined values
This feature is the same as the value in Kotlin, we can add an underscore to the value. (PS: There are underlined numbers in the above example.)
When writing long numbers, the eye can have a hard time figuring out how some numbers fit together, for example values over tens of millions. Even larger values, all numbers mixed together. It is easy to fail to understand.
(PS: It is the same as adding a comma to distinguish when the relatively large amount exceeds 1000, we can distinguish it by underscore).
copylong 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
The above example does not affect the calculation and display of this value. It will not change from a value to a string because of adding an underscore.
2.4 Numeric type suffixes
When creating floating point numbers, I use f, g, d and other suffixes to add after the value. This is to tell the system what data type the parameter is. Except for these three. There are also the following suffixes:
type of data | suffix value |
---|---|
BigInteger | G or g |
Long | L or l |
Integer | I or i |
BigDecimal | G or g |
Double | D or d |
Float | F or f |
2.5 Mathematical operations
The following briefly shows some operators of mathematical calculations and their result types. (mainly addition and subtraction operations)
- The calculation result of byte,char,short and int is int type.
- The calculation result of long, byte, char, short and int will be long type.
- The result of calculations involving BitInteger and other integer parameters will be of type BitInteger.
- The mixed calculation result of byte, char, short, int, BigInteger will be BigDecimal type.
- Mixed calculation between float, double and BigDecimal, the result will be double.
- The result of an operation between two BigDecimals will be a BigDecimal.
These conversions are ordinary addition and subtraction operations. The corresponding table is as follows:
byte | char | short | int | long | BigInteger | float | double | BigDecimal | |
---|---|---|---|---|---|---|---|---|---|
byte | int | int | int | int | long | BigInteger | double | double | BigDecimal |
char | int | int | int | long | BigInteger | double | double | BigDecimal | |
short | int | int | long | BigInteger | double | double | BigDecimal | ||
int | int | long | BigInteger | double | double | BigDecimal | |||
long | long | BigInteger | double | double | BigDecimal | ||||
BigInteger | BigInteger | double | double | BigDecimal | |||||
float | double | double | double | ||||||
double | double | double | |||||||
BigDecimal | BigDecimal |
Thanks to Groovy's operator overloading, the usual arithmetic operators work with BigInteger and BigDecimal, unlike in Java where you have to use explicit methods to operate on these numbers.
2.5.1 Division operation
The division operator / (and /= for division and assignment) produces a double result if one of the operands is float or double, otherwise produces a BigDecimal result (when both operands are short, char, byte, int, long, Any combination of BigInteger or BigDecimal).
If the division is exact (i.e. produces a result that can be represented in the same precision and scale range), then use the divide() method to perform BigDecimal division, or use a MathContext with a precision of the maximum value of the two operands plus an additional A precision of 10, and a maximum value of 10 and a maximum value of the operand scale.
Example:
copydef static main(def args) { def s = 12.3 //Created is a BigDecimal object def f = 12.2f// Created is a Float object def d = 12.3d //Created is a Double object def aa = s / f / d def temp1= s.divide(f) //Will return BigDecimal type }
2.5.2 Exponentiation operation
Exponentiation is represented by the ** operator, which takes two arguments: base and exponent. The result of an exponentiation operation depends on its operands and the result of the operation (especially if the result can be represented as an integer value).
Summarized as follows:
- If exponent is of decimal number type (can be integer, can be decimal).
- If the result can be represented by an Integer, return an Integer.
- Returns long if the result is expressed in long.
- Otherwise a Double is returned.
- If the index is of type Integeral.
- If the radix is BigDecimal, return the BigDecimal result value.
- If the base is BigInteger, return the BigInteger result.
- The base is Intgent, if the result matches, return Intgent, otherwise return BigInteger.
- The base is Long, if the result matches, it will return Long, otherwise it will return BigInteger.
- If the exponent is negative, the type returned can be Integer, long or Double depending on the result value.
- If the exponent is positive or 0.
This is just to illustrate some changes that may occur in the data type after the operation is performed.
The sample code is as follows:
copy//The base and exponent are integers, and the result can be represented by Integer assert 2 ** 3 instanceof Integer // 8 assert 10 ** 9 instanceof Integer // 1_000_000_000 // The base is a long, so put the result in a long // (although it can fit integers) assert 5L ** 2 instanceof Long // 25 // The result cannot be represented as an Integer or Long, so a BigInteger is returned assert 100 ** 10 instanceof BigInteger // 10e20 assert 1234 ** 123 instanceof BigInteger // 170515806212727042875... // The base is a BigDecimal and the exponent is a negative integer // But the result can be expressed as an integer assert 0.5 ** -2 instanceof Integer // 4 // The base is an integer and the exponent is a negative floating point number // But again, the result can be expressed as an integer assert 1 ** -0.3f instanceof Integer // 1 //The base is an integer and the exponent is a negative integer //but the result will be evaluated as Double //(both base and exponent are actually converted to double values) assert 10 ** -1 instanceof Double // 0.1 //The base is a BigDecimal and the exponent is an int, so returns a BigDecimal assert 1.2 ** 10 instanceof BigDecimal // 6.1917364224 //The base is a float or double, and the exponent is an integer //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 //Exponent as a decimal number (can be integer and decimal) //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
3. Boolean Boolean type
Boolean values are a special data type used to represent true and false.
Boolean values can be stored in variables and assigned to fields, just like any other data type:
copydef myBooleanVariable = true boolean untypedBooleanVar = false booleanField = true
true and false are the only two primitive boolean values. But more complex Boolean expressions can be expressed with logical operators.
Additionally, Groovy has special rules (often called Groovy Truth) for coercing non-boolean objects to boolean values.
Boolean variables are relatively simple in themselves.
4. Summary
The above relevant information is in the Grooovy official website link address: http://docs.groovy-lang.org/docs/groovy-4.0.6/html/documentation/#_numbers
This article introduces several situations of basic numerical types, as well as some format conversions during operation.
It is enough to understand the general knowledge. Comprehend the relevant specific meanings in the subsequent use process.