Basic overview of data types in solidity

Solidity is a high-level, object-oriented programming language that is used to write smart contracts and build D-apps. It also supports inheritance, libraries, and sophisticated user-defined types. Solidity is a statically typed language. That means, you will have to declare the type of variables in advance unlike javascript.

Any programming language has basic data types and structures which are used to write logical programs. Why should solidity be any different? Let’s take a look at data types which are available in solidity: 

Types of Data types

There are two types of data-types in solidity.

  • Value Tyeps
  • Reference Types

Value Types

A value type stores its data directly in the memory it owns. A value type will maintain an independent copy of any duplicated variables. Therefore, a change in the value of a duplicated variable will not affect the original variable.

E.g.

uint a = 2;
uint b = a;
a = 3;

In the above case, a = 3 and b = 2.

There are several value types in Solidity: 

  • Boolean: This data type accepts only two values True or False.
  • Integer: This data type is used to store integer values, int and uint are used to declare signed and unsigned integers respectively.
  • Fixed Point Numbers: These data types are not fully supported in solidity yet, as per the Solidity documentation. They can be declared as fixed and unfixed for signed and unsigned fixed-point numbers of varying sizes respectively.
  • Address: Address holds a 20-byte value which represents the size of an  Ethereum address. An address can be used to get balance or to transfer a balance by balance and transfer method respectively.
  • Bytes and Strings: Bytes are used to store a fixed-sized character set while the string is used to store the character set equal to or more than a byte. The length of bytes is from 1 to 32, while the string has a dynamic length. Byte has an advantage that it uses less gas, so better to use when we know the length of data.
  • Enums: It is used to create user-defined data types, used to assign a name to an integral constant which makes the contract more readable, maintainable, and less prone to errors. Options of enums can be represented by unsigned integer values starting from 0.

Here is the simple solidity contract which demonstrates each of the value types. You can directly copy paste the following code on Remix IDE.

// Solidity program to demonstrate
// value types
pragma solidity ^ 0.8.5;  
 
// Creating a contract
contract DemoValueTypes {  
   // Initializing Bool variable
   bool public boolean;
  
   // Initializing Integer variable
   int32 public intVar = -60313;
   //  Initializing String variable
   string public str = "This is the sample string";
   // Initializing Byte variable
   bytes1 public b = "a";
   
   // Defining an enumerator
   enum sampleEnum { first, second, third, defaultValue }
   // Defining a function to return values stored in an ENUM
 
   function accessEnum(uint8 index) public pure returns(
     sampleEnum) {
       if (index == 0) {
           return sampleEnum.first;
       } else if (index == 1) {
           return sampleEnum.second;
       } else if (index == 2) {
           return sampleEnum.third;
       } else {
           return sampleEnum.defaultValue;
       }         
   }
}  

Reference Types

Reference type variables store the location of the data. They don’t share the data directly. With the help of reference type, two different variables can refer to the same location where any change in one variable can affect the other one. Reference type in solidity are listed below: 

  • Arrays: Like any other language, an array is used to store variables of the same data type. By using the index location, the variable at the desired location can be accessed. The array size can be fixed or dynamic. It is possible to mark state variable arrays public and have Solidity create a getter. The numeric index becomes a required parameter for the getter. Accessing an array past its end causes a failing assertion. Methods .push() and .push(value) can be used to append a new element at the end of the array, where .push() appends a zero-initialized element and returns a reference to it.
  • Struct: Solidity allows users to create and define their own type in the form of structures. The structure is a group of different types even though it’s not possible to contain a member of its own type. The structure is a reference type variable which can contain both value type and reference type
  • Mapping: Mapping is a most used reference type that stores the data in a key-value pair where a key can be any value type. It is like a hashtable or dictionary as in any other programming language, where data can be retrieved by key.

Here is the simple solidity contract which demonstrates each of the reference types. You can directly copy paste the following code on Remix IDE.

// Solidity program to demonstrate
// reference types
pragma solidity ^ 0.8.5;  
 
// Creating a contract
contract DemoReferenceTypes {
   // Defining an array of fixed size  
   uint[5] public fixedSizeArray
     = [uint(1), 2, 3, 4, 5] ;
   
   // Defining a Structure
   struct DemoStruct {
       string firstVar;
       address secondVar;
       uint8 thirdVar;
   }

   // Creating a structure object
   DemoStruct public struct1;
 
   // Creating a mapping
   mapping  (address => DemoStruct) public sampleMapping;
  
   address[] student_result;

   constructor() {
     struct1.firstVar = "abcd";
     struct1.secondVar = address(0x0);
     struct1.thirdVar = 1;
     sampleMapping[address(0x10)] = struct1;
  }
}

Let’s dive deep into each of the data types in the following posts. Follow on twitter for more such posts and updates.


Discover more from BitsByBlocks

Subscribe to get the latest posts to your email.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Discover more from BitsByBlocks

Subscribe now to keep reading and get access to the full archive.

Continue reading