Solidity data types can broadly be classified following two types:
- Value types
- Reference types
These two types in Solidity differ the way assigned to a variable and stored in EVM. Assigning a variable variable done by creating copy by copying the reference. Value types maintain independent copies of variables and changing in one variable affect value in another variable. However, changing values in reference type variables ensures that anybody that variables gets updated value.
A type is as type if it holds (value) directly within the memory owned by it. These types have values stored with them, elsewhere. is illustrated following diagram. example, a variable type unsigned integer (uint) 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 13 stored.
Accessing this variable will provide us with 13 directly:
Value types are types that take 32 bytes of memory in size. Solidity provides value types:
- bool: The boolean value hold true or false as its value
- uint: These are unsigned integers hold 0 and positive values only
- int: These are signed integers 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 hold predefined constant values.
Passing by value
When type variable is assigned variable or when type variable as an argument to a function, EVM creates variable instance and copies of value type into the target variable. passing by value. Changing values in original or target variables affect in another variable. Both the variables will maintain their independent, isolated values, can change without knowing about it.
Reference types, unlike value types, store their values directly within the variables themselves. , they store the address of where is stored. The variable holds the pointer memory location that holds data. Reference types are types take 32 bytes of memory in size. Reference types are shown next, by means of an illustration.
In the following example, an array variable type uint with size 6. Arrays in Solidity are based at zero this array can hold seven elements. The variable a has memory space allocated by EVM which is referred to as 0x123 and this location pointer value 0x456 stored in it. This pointer refers to memory location where the array data is stored. When accessing the variable, EVM dereferences of the pointer and shows from the array index as shown following diagram:
Solidity provides reference types:
- Arrays: These are fixed as dynamic arrays.
- Structs: These are custom, user-defined structures.
- String: a sequence of characters. In Solidity, strings are eventually stored as
- bytes. Details are given later chapter.
- Mappings: a hash table or dictionary in other languages storing key-value pairs.
Passing by reference
When a reference type variable is assigned variable or when a reference type variable as an argument to a function, EVM creates variable instance and copies the pointer from variable into the target variable. passing by reference. Both the variables are pointing to address location. Changing values original or target variables will change in other variables also. Both the variables will share values committed by one is reflected other variable.