Zero-Initialized Value
Every variable in Solidity has a default value when declared but not explicitly initialized. This zero-initialized state ensures predictable behavior and prevents undefined values.
Primitive Types
Integer Types
uintvariants:0intvariants:0
contract ZeroInt {
uint256 public u; // 0
int128 public i; // 0
uint8 public small; // 0
}Boolean
bool:false
contract ZeroBool {
public flag; // false
}Address Types
address:0x0000000000000000000000000000000000000000address payable:0x0000000000000000000000000000000000000000
contract ZeroAddress {
public addr; // zero address
public payable addrP; // zero address
}Complex Types
Fixed-size Arrays
Each element is zero-initialized based on its type:
contract ZeroFixedArray {
uint[5] public arr; // [0, 0, 0, 0, 0]
bool[3] public flags; // [false, false, false]
}Dynamic Arrays
- Empty array with length
0
contract ZeroDynamicArray {
uint[] public arr; // empty array []
address[] public users; // empty array []
}Mappings
All possible keys return zero-initialized values:
contract ZeroMapping {
mapping(address => uint) public balances;
// balances[any_address] returns 0
}Structs
Each field is zero-initialized according to its type:
contract ZeroStruct {
struct User {
uint id;
address addr;
bool active;
}
public user; // {id: 0, addr: zero address, active: false}
}Enums
Default value is the first enum member (index 0):
contract ZeroEnum {
enum Status { Pending, Active, Inactive }
public status; // Status.Pending
}Memory vs Storage
Storage Variables
State variables are automatically zero-initialized when the contract is deployed:
contract StorageZero {
uint public counter; // 0 at deployment
bool public initialized; // false at deployment
}Memory Variables
Local variables in memory are also zero-initialized:
contract MemoryZero {
function example() public pure returns (uint, bool) {
uint x; // 0
bool flag; // false
return (x, flag);
}
}Important
Unlike some languages where uninitialized variables contain garbage values, Solidity guarantees zero-initialization for all types. This prevents unexpected behavior and makes contracts more predictable.
Warning
- Always consider that uninitialized variables contain zero values
- When checking for “empty” state, test against the appropriate zero value
- Be aware that
address(0)often represents an invalid or uninitialized address in many protocols