Lesson 2 of 4 Solidity Language Fundamentals

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.

TypeDescriptionDefault value
booltrue or falsefalse
uint8 ... uint256Unsigned integers in 8-bit steps; uint is uint2560
int8 ... int256Signed integers; int is int2560
addressA 20-byte account address; address payable can receive ether with transfer and send0x000...0
bytes1 ... bytes32Fixed-size byte arrays, such as hashesAll zero bytes
enumA user-defined set of named constants, stored as uint8The 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).

Fill in the number

Integers instead of decimals

Exam skills 2.1.2

Convert amounts into the integers that Solidity stores, since it has no floating-point numbers.

Log in to try this activity Interactive activities are free with an account.

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 with push when in storage.
bytes and string
Dynamic byte arrays; string holds 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.

True or false

How mappings behave

Exam skills 2.1.2

Check how Solidity mappings differ from hash tables in other languages.

Log in to try this activity Interactive activities are free with an account.

Data locations

Every reference-type variable in a function has a data location. The following comparison shows the three main locations.

storage
LifetimePermanent, part of contract state
MutableYes
CostHighest
Typical useState variables and references to them
memory
LifetimeUntil the function call ends
MutableYes
CostLow
Typical useTemporary values and return data
calldata
LifetimeUntil the external call ends
MutableNo (read-only)
CostLowest
Typical useExternal function parameters

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
}
Scenario questions

Choosing a data location

Exam skills 2.1.2

Choose the data location that gives each function correct and efficient behaviour.

Log in to try this activity Interactive activities are free with an account.

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.

How state variables are laid out in storage. Slots are 32 bytes, numbered from 0 in declaration order. Consecutive variables smaller than 32 bytes share a slot when they fit, as an address of 20 bytes and a uint64 of 8 bytes do here. A uint256 needs a whole slot. Mapping values and dynamic array elements are stored at slots computed by hashing, so they never collide with other variables.
Small consecutive variables share a slot.
Fill in the number

Counting storage slots

Exam skills 2.1.2

Work out how many storage slots state variables use, given the packing rules.

Log in to try this activity Interactive activities are free with an account.
Worked example

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.

Worked example

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.

Check yourself

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.