May 14, 2021 Groovy
In any programming language, you need to use a variety of variables to store various types of information. 20> Variables simply keep values where they are stored, which means that when you create a variable, you keep some space in memory to store the values associated with the variable.
You may like to store information of various data types, such as strings, characters, wide characters, integers, floats, Boolean values, and so on. B ased on the data type of the variable, the operating system allocates memory and decides what can be stored in reserved memory.
Groovy offers a variety of built-in data types.
The following is a list of the types of data defined in Groovy -
Byte - This is used to represent byte values. F or example, 2.
short - This is used to represent a short integer. F or example, 10.
int - This is used to represent integers. F or example, 1234.
long - This is used to indicate a long pattern. F or example, 10000090.
float - This is used to represent a 32-bit float. F or example, 12.34.
double - This is used to represent 64-bit floating-point numbers, which are sometimes represented by longer deeds that may be required. /b11>For example, 12.3456565.
char - This defines a single character text. F or example, "A".
Boolean - This represents a Boolean value that can be true or false.
String - These are texts that are represented as strings. F or example, Hello World.
The following table shows the maximum allowed values in the number and scale text.
byte | -128 to 127 |
short | -32,768 to 32,767 |
Int | 2,147,483,648 to 147,483,647 |
long | -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 |
float | 1.40129846432481707e-45 to 3.40282346638528860e and 38 |
double | 4.94065645841246544e-324d to 1.79769313486231570e and 308d |
Types allow the following object types (sometimes referred to as wrapper types) in addition to the base type -
In addition, the following classes can be used to support high-precision computing -
Name | Describe | For example |
---|---|---|
java.math.BigInteger | A signed integer number of any precision that cannot be changed | 30g |
java.math.BigDecimal | The number of signed hedes with any precision that cannot be changed | 3.5g |
The following code example shows how to use different built-in data types -
class Example { static void main(String[] args) { //Example of a int datatype int x = 5; //Example of a long datatype long y = 100L; //Example of a floating point datatype float a = 10.56f; //Example of a double datatype double b = 10.5e40; //Example of a BigInteger datatype BigInteger bi = 30g; //Example of a BigDecimal datatype BigDecimal bd = 3.5g; println(x); println(y); println(a); println(b); println(bi); println(bd); } }
When we run the program above, we get the following results -
5 100 10.56 1.05E41 30 3.5