Solidity Reference Type

Amarachi Ugwu
2 min readFeb 26, 2022

--

Splash Image

Unlike Value type variables which stores their own data, 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.

Solidity has two types, the Value types, and Reference types but I am focusing on just the reference types, check out value types here:

This reference type variables are passed by reference while the value type are parsed by value, check the difference below.

Reference type in solidity are listed below.

Arrays

An array is a group of variables of the same data type in which variable has a particular location known as an index. By using the index location, the desired variable can be accessed. The array size can be fixed or dynamic.

In solidity arrays only accept one data type unlike other programming languages like javascript which access different data types in an array.

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.

You can think of struct like a database table where you specify the column and then dynamically add rows of different data into the table.

Mapping

Mapping is a most used reference type, that stores the data in a key-value pair where a key can be any value types. It is like a hash table or dictionary as in any other programming language, where data can be retrieved by key.

Mapping is similar to an array but differs in the sense that the key of a mapping can be any value type while an array is indexed.

Example: In the below example, the contract Types initializes the values of various Reference Types.

Output

--

--