Groovy as a language offers support for built-in primitive and defined primitive types. Unlike Java, Groovy is an optionally typed language, accomplished by adding a variant data type. Depending on the type of data stored, Groovy (like Java) will store either the actual value or a reference to the object.
A primitive is stored in memory as the actual value instead of a reference to an object. However, as with Java, Groovy offers access to object-based representations of these objects.
In addition to the above primitive Groovy adds a keyword of def. This provides an untyped variable that supports runtime evaluation of type. See the following examples from the Groovy language documentation below:
One special kind of object is a map. Known as a dictionary or associative array in other languages, a map in Groovy allows for storing key-value pairs in a variable. The key is typically a string and the value can be any object.
Built-in Primitive Types
The Groovy language inherits all the Java primitive types:Type | Description |
boolean | A data type representing 21 values (programmatically defined as true and false) |
byte | An 8-bit number from -128 to 127 inclusive |
char | A single 16-bit Unicode character with values from \u0000 to \ufff inclusive |
short | A 16-bit signed integer with values from -32,768 to 32,767 inclusive |
int | A 32-bit signed integer with values from -231 to 231-1 inclusive |
long | A 32-bit signed integer with values from -263 to 263-1 inclusive |
float | A single-precision 32-bit IEEE 754 floating point number |
double | A double-precision 64-bit IEEE 754 floating point number |
In addition to the above primitive Groovy adds a keyword of def. This provides an untyped variable that supports runtime evaluation of type. See the following examples from the Groovy language documentation below:
def a = 1
assert a instanceof Integer
// Integer.MAX_VALUE
def b = 2147483647
assert b instanceof Integer
// Integer.MAX_VALUE + 1
def c = 2147483648
assert c instanceof LongIt should be noted that a variable defined using the def keyword is stored not as a primitive, but as the corresponding Java object.
Defined Primitive Types
Groovy supports creation of enumerations as a type of defined primitive type. Enumerations are a collection of named values.enum DefinedPrimitive { VALUE1, VALUE2, VALUE3 }
Composite Types
In addition to the defined primitive types, Groovy supports composite types including maps and arrays.Arrays
In addition to single values, Groovy allows for arrays of multiple individual primitive or complex values. By default, Groovy will, unlike in Java, automatically create a Java collection, java.util.ArrayList by default, meaning the size of the array is dynamic. It is possible to create an array as a true, fixed size array of primitives as well.Maps and Objects
As Groovy is an object-oriented language, variables can also store references to objects. These objects contain more complex data structures and a collection of methods that can act upon the data stored in the object. Class definitions and operations are described in a later section.One special kind of object is a map. Known as a dictionary or associative array in other languages, a map in Groovy allows for storing key-value pairs in a variable. The key is typically a string and the value can be any object.
No comments:
Post a Comment