Solidity provides various data types, each with a specific purpose and behavior. When a variable is declared in Solidity but not explicitly initialized, it is assigned a default value depending on its type. Below is a comprehensive list of the different data types in Solidity along with their default values.
1. Boolean (bool
)
- Description: Represents
true
orfalse
. - Default Value:
false
bool flag; // default value is false
2. Integer Types
Solidity supports both signed (int
) and unsigned (uint
) integers of various sizes.
- Unsigned Integer (
uint
):- Represents non-negative integers.
- Sizes:
uint8
,uint16
, …,uint256
(in multiples of 8 bits). - Default Value:
0
uint number; // uint256 by default, value is 0 uint8 smallNumber; // value is 0
- Signed Integer (
int
):- Represents both positive and negative integers.
- Sizes:
int8
,int16
, …,int256
(in multiples of 8 bits). - Default Value:
0
int number; // int256 by default, value is 0 int8 smallNumber; // value is 0
3. Address (address
)
- Description: Holds a 20-byte Ethereum address.
- Default Value:
0x0000000000000000000000000000000000000000
address wallet; // default value is 0x0000000000000000000000000000000000000000
address payable
: A subtype ofaddress
that allows transferring Ether using.transfer()
or.send()
.
4. Fixed-Size Byte Arrays
- Description: Represents a fixed-size sequence of bytes.
- Sizes:
bytes1
,bytes2
, …,bytes32
. - Default Value: A byte array with all elements set to
0x00
.
bytes32 data; // default value is 0x0000000000000000000000000000000000000000000000000000000000000000
5. Dynamic-Size Byte Arrays (bytes
)
- Description: A dynamically-sized sequence of bytes.
- Default Value: An empty byte array
0x
.
bytes dynamicData; // default value is 0x (empty)
6. String (string
)
- Description: Represents a dynamically-sized UTF-8 encoded string.
- Default Value: An empty string
""
.
string name; // default value is "" (empty string)
7. Enumerations (enum
)
- Description: User-defined types that assign names to a set of values.
- Default Value: The first member of the enum.
enum Status { Inactive, Active, Blocked }
Status currentStatus; // default value is Status.Inactive (the first enum member)
8. Structs (struct
)
- Description: User-defined complex data types that group variables.
- Default Value: Each member of the struct is initialized to its default value.
struct Person {
string name;
uint age;
}
Person person; // default value is person.name = "", person.age = 0
9. Arrays
- Fixed-Size Arrays:
- Description: An array with a predefined length.
- Default Value: Each element is initialized to its default value.
uint[5] fixedArray; // default values: [0, 0, 0, 0, 0]
- Dynamic-Size Arrays:
- Description: An array without a predefined length.
- Default Value: An empty array.
uint[] dynamicArray; // default value: []
10. Mappings (mapping
)
- Description: Key-value pairs where all possible keys exist, and each key maps to a value type.
- Default Value: Each key maps to the default value of the value type.
mapping(address => uint) balances; // default value for any key is 0
Summary
Each data type in Solidity has a specific default value when not explicitly initialized:
bool
defaults tofalse
.uint
,int
, and their variants default to0
.address
defaults to0x0000000000000000000000000000000000000000
.bytes
andstring
default to an empty value.enum
defaults to the first element.struct
fields default to their respective types’ default values.arrays
have elements default to their types’ defaults.mappings
map to the default value of the value type.
Understanding these defaults is crucial for ensuring predictable behavior in your smart contracts. You can also read about how to initialize each of them, if you’re interested.