89DEVs

Data Types in Solidity

In the Solidity programming languages, data types for variables can be classified into two types: value types and reference types. The difference is that value types actually store a value, while reference types store just a reference to where the data is actually stored. It is therefore important to be more careful with reference data types. 

Value Data Types in Solidity

The code below shows some examples of data types used in a smart-contract written in solidity. // SPDX-License-Identifier: MIT pragma solidity ^0.8.13; contract ValueDataTypes { bool public boo1 = true; bool public boo2 = false; uint8 public u8 = 1; uint public u256 = 444; int8 public i8 = -1; int public i256 = 444; address public addr = 0x0000000000000000000000000000000000000000; }

Reference Data Types in Solidity

Reference data types, don't store the actual value instead they store just a reference to the actual data location. Reference data types in Solidity are array, struct and mapping. There are three options where the reference types can be stored: memory, storage, and calldata. With reference data types it is possible, that two different variables point to the same location and if one is changed the change affects the other variable as well. It is important to be more careful with these types, otherwise, unexpected behavior could occur.

Default Values in Solidity

If a variable is declared without assigning a value to it, the variable has a default value depending on its data type. The following table gives an overview over the default values for each data type.
Data Type Description Default Value
bool boolean false
uint unsigned integer 0
int integer 0
address ethereum address 0x0000000000000000000000000000000000000000
bytes32 fixed-size byte array 0x0000000000000000000000000000000000000000000000000000000000000000
string string (empty)

        

Summary

Click to jump to section