Data Types
As the name suggests, data-type specifies the type of the data present in the variable. Variables must be declared with a data-type.
Groovy offers various built-in data types:
| Data type | Description | Example |
|---|---|---|
| int | used to represent whole numbers | int x = 99999 |
| short | used to represent whole numbers | short x = 999 |
| long | used to represent whole numbers | long x = 99999999999L |
| float | used to represent fractional numbers | float x = 22.79f |
| double | used to represent fractional numbers | double x = 99.99d |
| char | used to represent a single character | char grade = 'A' |
| boolean | used to represent Boolean data either true or false | boolean isAvailable = true |
| byte | Used to represent a byte value | byte x = 99 |
| string | used to represent a series of characters | string msg = "Happy learning!" |
Examples
int int_var = 99999 // integer variable
println int_var;
short short_var = 999 // short variable
println short_var;
long long_var = 99999999999L // long variable
println long_var;
float float_var = 22.79f // float variable
println float_var;
double double_var = 99.99d // double variable
println double_var;
char char_var = 'A' // char variable
println char_var;
boolean isAvailable = true // boolean variable
println isAvailable;
byte byte_var = 99 // byte variable
println byte_var;
String msg = "Happy learning!" // String variable
println msg;
Check result here
Note:
; is optional in groovy