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 typeDescriptionExample
intused to represent whole numbersint x = 99999
shortused to represent whole numbersshort x = 999
longused to represent whole numberslong x = 99999999999L
floatused to represent fractional numbersfloat x = 22.79f
doubleused to represent fractional numbersdouble x = 99.99d
charused to represent a single characterchar grade = 'A'
booleanused to represent Boolean data either true or falseboolean isAvailable = true
byteUsed to represent a byte valuebyte x = 99
stringused to represent a series of charactersstring 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