Groovy – Data Types

  • Post author:
  • Post category:Groovy
  • Post comments:1 Comment
Types

In any programming language, you need to use various variables to store various types of information. Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory to store the value associated with the variable.

You may like to store information of various data types like string, character, wide character, integer, floating point, Boolean, etc. Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory.

Built-in Data Types

Groovy offers a wide variety of built-in data types. Following is a list of data types which are defined in Groovy βˆ’

  • byte βˆ’ This is used to represent a byte value. An example is 2.
  • short βˆ’ This is used to represent a short number. An example is 10.
  • int βˆ’ This is used to represent whole numbers. An example is 1234.
  • long βˆ’ This is used to represent a long number. An example is 10000090.
  • float βˆ’ This is used to represent 32-bit floating point numbers. An example is 12.34.
  • double βˆ’ This is used to represent 64-bit floating point numbers which are longer decimal number representations which may be required at times. An example is 12.3456565.
  • char βˆ’ This defines a single character literal. An example is β€˜a’.
  • Boolean βˆ’ This represents a Boolean value which can either be true or false.
  • String βˆ’ These are text literals which are represented in the form of chain of characters. For example β€œHello World”.

Bound values

The following table shows the maximum allowed values for the numerical and decimal literals.

byte-128 to 127
short-32,768 to 32,767
int-2,147,483,648 to 2,147,483,647
long-9,223,372,036,854,775,808 to +9,223,372,036,854,775,807
float1.40129846432481707e-45 to 3.40282346638528860e+38
double4.94065645841246544e-324d to 1.79769313486231570e+308d

Class Numeric

Types In addition to the primitive types, the following object types (sometimes referred to as wrapper types) are allowed βˆ’

  • java.lang.Byte
  • java.lang.Short
  • java.lang.Integer
  • java.lang.Long
  • java.lang.Float
  • java.lang.Double

In addition, the following classes can be used for supporting arbitrary precision arithmetic βˆ’

NameDescriptionExample
java.math.BigIntegerImmutable arbitrary-precision signed integral numbers30g
java.math.BigDecimalImmutable arbitrary-precision signed decimal numbers3.5g

The following code example showcases how the different built-in data types can be used βˆ’

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 above program, we will get the following result βˆ’

5 
100 
10.56 
1.05E41 
30 
3.5

Next Topic:-Click Here

This Post Has One Comment

Leave a Reply