Featured Video Play Icon

Data Types In Solidity


Solidity data types can broadly be classified within the following two types:

  • Value types
  • Reference types

These two types in Solidity differ supported by the way they’re assigned to a variable and stored in EVM. Assigning a variable to a different variable is often done by creating a replacement copy or simply by copying the reference. Value types maintain independent copies of variables and changing the worth in one variable doesn’t affect value in another variable. However, changing values in reference type variables ensures that anybody pertaining to that variables gets updated value.

Value types

A type is mentioned as a worth type if it holds the info (value) directly within the memory owned by it. These types have values stored with them, rather than elsewhere. an equivalent is illustrated within the following diagram. during this example, a variable of knowledge type unsigned integer (uint) is said with 13 as its data(value). The variable a has memory space allocated by EVM which is referred to as 0x123 and this location has the worth 13 stored.
Accessing this variable will provide us with the worth 13 directly:

Data Types in Solidity

Value types are types that don’t take quite 32 bytes of memory in size. Solidity provides the subsequent value types:

  • bool: The boolean value which will hold true or false as its value
  • uint: These are unsigned integers that will hold 0 and positive values only
  • int: These are signed integers that will hold both negative and positive values
  • address: This represents an address of an account on the Ethereum environment
  • byte: This represents a fixed-sized byte array ( byte1 to bytes32 )
  • enum: Enumerations that will hold predefined constant values.

Passing by value

When a worth type variable is assigned to a different variable or when a worth type variable is shipped as an argument to a function, EVM creates a replacement variable instance and copies the worth of the first value type into the target variable. this is often referred to as passing by value. Changing values in original or target variables won’t affect the worth in another variable. Both the variables will maintain their independent, isolated values, and that they can change without the opposite knowing about it.

Reference types

Reference types, unlike value types, don’t store their values directly within the variables themselves. rather than the worth, they store the address of the situation where the worth is stored. The variable holds the pointer to a different memory location that holds the particular data. Reference types are types that will take quite 32 bytes of memory in size. Reference types are shown next, by means of an illustration.
In the following example, an array variable of knowledge type uint is said with size 6. Arrays in Solidity are based at zero then this array can hold seven elements. The variable a has memory space allocated by EVM which is referred to as 0x123 and this location features a pointer value 0x456 stored in it. This pointer refers to the particular memory location where the array data is stored. When accessing the variable, EVM dereferences the worth of the pointer and shows the worth from the array index as shown within the following diagram:

Solidity data types

Solidity provides the subsequent reference types:

  • Arrays: These are fixed also as dynamic arrays.
  • Structs: These are custom, user-defined structures.
  • String: this is often a sequence of characters. In Solidity, strings are eventually stored as
  • bytes. Details are given later in this chapter.
  • Mappings: this is often almost like a hash table or dictionary in other languages storing key-value pairs.

Passing by reference

When a reference type variable is assigned to a different variable or when a reference type variable is shipped as an argument to a function, EVM creates a replacement variable instance and copies the pointer from the first variable into the target variable. this is often referred to as passing by reference. Both the variables are pointing to an equivalent address location. Changing values within the original or target variables will change the worth in other variables also. Both the variables will share equivalent values and alter committed by one is reflected within the other variable.

Mansoor Ahmed is Chemical Engineer, web developer, a writer currently living in Pakistan. My interests range from technology to web development. I am also interested in programming, writing, and reading.
Posts created 422

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top