Solidity is statically typed, so every variable needs a type known at compile time. The main solidity data types are booleans, integers (uint and int), address, fixed-size byte arrays (bytes1 to bytes32), string and dynamic bytes, plus enums, structs, arrays and mappings. Most of them are value types that are copied on assignment, while arrays, structs and mappings are reference types that need a data location.
This guide goes through each group with a runnable contract and explains default values and data locations.
Value types always hold their data directly and are copied when assigned or passed to a function.
| Type | Example | Notes |
|---|---|---|
bool | true, false | Default is false |
uint8 to uint256 | uint256 total = 10; | Unsigned, in steps of 8 bits. uint is an alias of uint256 |
int8 to int256 | int256 delta = -5; | Signed integers |
address | 0xAb58... | A 20-byte account address |
address payable | An address that can receive ETH with transfer or send | |
bytes1 to bytes32 | bytes4 selector; | Fixed-size byte arrays, cheap to store |
enum | enum Status { Open, Closed } | Named integer constants |
Since Solidity 0.8.0, arithmetic on integers is checked: an overflow or underflow reverts the transaction. If you want wrapping behaviour on purpose, you must write the operation inside an unchecked { ... } block, which saves a little gas but removes the safety net.
Fixed-point numbers (fixed and ufixed) exist as type names but are not fully supported, so represent decimals with integers instead. Token amounts, for example, are stored as integers with an implied number of decimals, such as 18 for ETH in wei.
An address holds 20 bytes. It has members such as balance and code. An address payable also allows sending ETH. In modern code the recommended way to send ETH is a low-level call and a check of the result, rather than transfer, because transfer forwards only 2300 gas:
Context values such as msg.sender (the caller) and msg.value (ETH sent) are of type address and uint256. Time values like block.timestamp are also uint256.
Arrays, structs, mappings and also string and bytes are reference types. When you use them as a variable inside a function you must state where the data lives:
storage: the permanent state of the contract, the most expensive to write.memory: temporary data that exists during a call and can be changed.calldata: read-only input data of an external function, the cheapest for arguments.Assigning between storage and memory copies the data, while two storage references point to the same slot. This is a common source of bugs, so declare the location every time.
string is a UTF-8 byte sequence with no length or index access. If you need to compare strings, hash them with keccak256, and for fixed short labels prefer bytes32, which is cheaper.
Arrays come in two forms:
uint256[3], with the length set at compile time.uint256[], which can grow in storage with push and shrink with pop.Reading past the end of an array reverts. Looping over a large dynamic array in a transaction can run out of gas, so avoid unbounded loops.
A struct groups related fields into a custom type. An enum limits a value to a short list of names. A mapping associates a key with a value, for example mapping(address => uint256), and has no length and no way to list its keys. Every key conceptually exists and returns the default value (zero) when it was never set.
This contract uses most of the types above:
addMember takes the string from calldata and writes a struct into the mapping. activate uses a storage pointer, so assigning m.status changes the stored value directly. summary copies the array into memory before looping, so the loop does not repeatedly read storage. demoOverflow returns 0, because the unchecked block lets 255 plus 1 wrap around; without the block the call would revert.
Uninitialised variables are never random. They take the type's zero value: false, 0, the zero address, an empty string or array, and the first enum member.
| Type | Default |
|---|---|
bool | false |
uint and int | 0 |
address | 0x0000000000000000000000000000000000000000 |
string and bytes | empty |
enum | first member |
Implicit conversion is allowed only when no information is lost, for example from uint8 to uint256. Narrowing, such as uint8(bigValue), must be explicit and silently truncates, so check the range first.
None. uint is an alias for uint256. Smaller types like uint8 are useful for packing several values into one storage slot, but the EVM works in 32-byte words, so they do not save gas for single variables.
Use bytes32 for short fixed identifiers such as symbols or hashes, since it is cheaper and comparable directly. Use string for text of unknown length.
No. A mapping does not store its keys. If you need to list entries, keep a separate array of keys or emit events and index them off chain.
No usable ones. Use integers with a fixed number of decimals, or a fixed-point library, for money and ratios.
Solidity offers value types (bool, uint, int, address, bytes1 to bytes32, enum) and reference types (arrays, struct, mapping, string, bytes) that need a data location of storage, memory or calldata. Since 0.8 integer arithmetic is checked, defaults are always the zero value, and decimals are handled with integers. Choosing the smallest suitable type and the right location keeps contracts safe and cheaper to run.