Value Types, Reference Types and Data Locations
Choosing the right type and data location is both a correctness and a cost decision. This lesson covers Solidity's value types, arrays, strings, structs, mappings and enums, and how storage, memory and calldata behave when data is assigned or passed.
Learning outcomes
After completing this lesson, you will be able to:
- Identify Solidity's value types and their default values.
- Describe arrays, strings, structs and mappings.
- Distinguish storage, memory and calldata.
- Predict whether an assignment copies data or creates a reference.
- Explain how state variables are laid out in storage slots.
Value types
Value types are always copied when assigned or passed. The following table lists them.
| Type | Description | Default value |
|---|---|---|
| bool | true or false | false |
| uint8 ... uint256 | Unsigned integers in 8-bit steps; uint is uint256 | 0 |
| int8 ... int256 | Signed integers; int is int256 | 0 |
| address | A 20-byte account address; address payable can receive ether with transfer and send | 0x000...0 |
| bytes1 ... bytes32 | Fixed-size byte arrays, such as hashes | All zero bytes |
| enum | A user-defined set of named constants, stored as uint8 | The first member |
Solidity has no floating-point numbers. Amounts are integers in the smallest unit (wei for ether, or the token's smallest unit), and fractions are handled by scaling, for example by 1018. Literals can use underscores (1_000_000) and units (1 ether, 2 gwei, 1 days).
Integers instead of decimals
Reference types
Reference types can be large, so Solidity makes you say where they live. The following terms describe them.
- Fixed and dynamic arrays
uint256[3]has a fixed length;uint256[]can grow withpushwhen in storage.- bytes and string
- Dynamic byte arrays;
stringholds UTF-8 text and has no built-in length or indexing. - struct
- A named group of fields, such as
struct Voter { bool voted; uint8 choice; }. - mapping
mapping(address => uint256): a hash table that exists only in storage.
Mappings behave differently from hash tables in other languages: every possible key already exists and returns the type's default value, they cannot be iterated, and they have no length. A contract that must list its keys, such as all token holders, maintains a separate array alongside the mapping.
How mappings behave
Data locations
Every reference-type variable in a function has a data location. The following comparison shows the three main locations.
Assignments between locations follow rules that cause many bugs. Assigning a storage variable to a memory variable copies it, so changes to the copy are lost. Assigning it to a local storage variable creates a reference, so changes are written to state. Since Solidity 0.8.28, value-type state variables can also be declared transient: they are cleared at the end of each transaction.
struct Order { uint256 amount; bool filled; }
mapping(uint256 => Order) public orders;
function fillWrong(uint256 id) external {
Order memory o = orders[id]; // copy
o.filled = true; // changes the copy only
}
function fillRight(uint256 id) external {
Order storage o = orders[id]; // reference
o.filled = true; // written to storage
}Choosing a data location
Storage layout
State variables are stored in 32-byte slots, numbered from 0 in declaration order. Consecutive variables smaller than 32 bytes are packed into one slot when they fit, which saves gas. Mapping values and dynamic array elements are stored at slots computed by hashing, so they never collide with other variables. Storage layout matters again for upgradeable proxies and gas optimisation, covered in later modules.
The following diagram lays out three state variables and a mapping.

Counting storage slots
A lost update
A voting contract has mapping(address => Voter) voters;. Its vote function contains Voter memory sender = voters[msg.sender]; require(!sender.voted); sender.voted = true;. Testers find that people can vote many times. Why?
sender is a memory copy, so setting voted changes only the copy, and storage still says the voter has not voted. Declaring it Voter storage sender = voters[msg.sender]; makes it a reference, so the flag is saved and the require blocks a second vote.
Calldata or memory?
An external function receives an array of 200 addresses and only reads it. Should the parameter be address[] memory or address[] calldata?
calldata. The array already exists in the transaction's input; declaring it as memory makes the EVM copy all 200 elements into memory first, costing extra gas. calldata reads them in place. Use memory only if the function must modify the array or is public and called internally with memory data.
What does a mapping return for a key that has never been set, and can you loop over its keys?
Show answer
It returns the default value of the value type, such as 0, false or the zero address. Mappings cannot be iterated and have no length; contracts keep a separate array of keys if they need to list them.
Exam Tips
Remember for the exam
- There are no floating-point types; use integers in the smallest unit.
- Mappings exist only in storage, cannot be iterated and return defaults for unset keys.
- storage-to-memory assignment copies; a storage local variable is a reference.
- Use calldata for read-only external array and string parameters.