Solidity Data Types: Complete Guide for Smart Contract Development
Solidity is a statically-typed programming language designed for writing smart contracts on the Ethereum blockchain. This means you must specify the data type of every variable when declaring it, which helps the compiler catch errors and ensures your code is both safe and efficient.
Understanding data types is fundamental to smart contract development. Let’s explore the most commonly used data types in Solidity:
π’ Integers
Integers are whole numbers that can be either signed (allowing positive and negative values) or unsigned (positive values only).
int b = -10; // Signed integer
uint a = 5; // Unsigned integer
You can specify the size of integers using keywords like int8, int16, int32, int64, uint8, uint16, uint32, and uint64. The default size for int and uint is 256 bits, which is the most gas-efficient size for most operations.
β Booleans
Booleans represent logical values and can only be true or false.
bool bt = true;
bool bf = false;
π Strings
Strings are sequences of characters enclosed in double quotes, used for storing text data.
string hw = "Hello World";
π€ Fixed-size Byte Arrays (bytesN)
Fixed-size byte arrays store a specific number of bytes. The size is determined by the number N in the type declaration.
bytes32 eth = "ETH";
In this example, eth is a byte array that can store exactly 32 bytes. This is commonly used for storing hashes and other fixed-size data.
π Address
The address type represents a 20-byte Ethereum address, used for identifying contracts or external accounts.
address adr = 0x06012c8cf97BEaD5deAe237070F9587f8E7A266d;
You can use the address type to store the addresses of contracts or external accounts, enabling interactions between different parts of your smart contract system.
π Additional Resources
For a complete list of data types and more detailed explanations, refer to the official Solidity documentation: