...

Code Block |
---|

```
//base-10 integers, positive or negative...
[ 2, -17, +987 ].each{ assert it }
//hex using leading 0x (lowercase or uppercase for a,b,c,d,e,f,x)...
[ 0xACe, 0X01ff ].each{ assert it }
//octal using leading 0...
[ 077, 01 ].each{ assert it }
``` |

...

Code Block |
---|

assert 42i.class == Integer //lowercase i more readable assert 123L.class == Long //uppercase L more readable assert 456g.class == BigInteger assert 0xFFi.class == Integer |

# Fixed-Size Integers

The fixed-size integers, Integer and Long, each have size limits but are more efficient in calculations.

...

Code Block |
---|

```
assert 45L as Integer == 45i
assert 45L as int == 45i //example of using 'int' for Integer
assert 45L.toInteger() == 45i //alternative syntax
assert 23L.intValue() == 23i //another alternative syntax
assert 45i as Long == 45L
assert 45i as long == 45L
assert 23i.toLong() == 23L
assert 45i.longValue() == 45L
//if converted number too large for target, only lowest order bits returned...
assert 256i as Byte == 0
assert 200i as byte == -56 //...and this may result in a negative number
``` |

...

Code Block |
---|

assert 3*(4+5) != 3*4+5 //parenthesized expressions always have highest precedence assert -3**2 == -(3**2) //power has next highest precedence assert ( 2*3**2 == 2*(3**2) ) && ( 2*3**2 != (2*3)**2 ) assert -3+2 != -(3+2) //unary operators have next highest precedence assert -~234 == -(~234) //unary operators group right-to-left //multiplication and modulo have next highest precedence assert 3*4%5 == (3*4)%5 //multiplication and modulo have equal precedence assert 3%4*5 == (3%4)*5 assert 4+5-6 == 3 //addition and subtraction have equal precedence, lower than mult/etc assert 4+5-6 == 3 assert 5+3*4 == 5+(3*4) |

Integers often convert their types during math operations. For + - *, a Long with an Integer converts the Integer to a Long:

...

Code Block |
---|

assert Integer.toString( 123456, 2 ) == '11110001001000000' assert Integer.toString( Integer.reverse( 123456 ), 2 ) == '10010001111000000000000000' //reverse bits assert Integer.reverseBytes( 0x157ACE42 ) == 0x42CE7A15 //also works for bytes |

### Boolean, Conditional, and Assignment Operators with Fixed-Sized Integers

The boolean, conditional, and assignment operators are of even lower precedence than the bitwise operators.

...

Code Block |
---|

def a = 7 a += 2 //short for a = a + 2 assert a == 9 a += (a = 3) //expands to a = a + (a = 3) before any part is evaluated assert a == 12 |

# BigIntegers

The BigInteger has arbitrary precision, growing as large as necessary to accommodate the results of an operation.

...