BudiBadu Logo
Samplebadu

Solidity by Example: Data Types

0.8.x

Comprehensive guide to Solidity data types with this code example covering booleans, unsigned and signed integers, the special address type for Ethereum accounts, fixed-size bytes arrays, and enums for modeling finite state machines.

Code

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract DataTypes {
    // Boolean
    bool public isReady = true;

    // Unsigned Integers (uint8 to uint256)
    uint8 public u8 = 1;
    uint256 public u256 = 456;

    // Signed Integers
    int256 public i256 = -123;

    // Address (holds a 20-byte Ethereum address)
    address public owner = 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4;
    
    // Bytes (fixed size)
    bytes32 public hash = 0xabc123...; // (truncated for brevity)

    // Enum
    enum Status { Pending, Shipped, Delivered }
    Status public currentStatus = Status.Pending;

    function setStatus(Status _status) public {
        currentStatus = _status;
    }
}

Explanation

Solidity is a statically typed language where every variable must have its type explicitly declared. The Ethereum Virtual Machine operates on 256-bit words, making uint256 and bytes32 the most gas-efficient types. Common value types include bool for true/false values, int and uint for signed and unsigned integers of various sizes (8 to 256 bits), and the blockchain-specific address type.

The address type is unique to Solidity and holds a 20-byte Ethereum address, representing either an externally owned account or a smart contract. Key features of the address type include:

  • .balance property to check Ether holdings of any address
  • .transfer() and .send() methods for sending Ether
  • address payable variant that explicitly allows receiving Ether
  • Conversion using payable(address) to enable Ether transfers

To send Ether to an address, it must be of type address payable, and the receiving contract must implement a receive() or payable fallback() function.

Fixed-size byte arrays like bytes32 are highly efficient for storing raw data, cryptographic hashes, or identifiers. Since the EVM processes data in 32-byte chunks, bytes32 is optimized for storage and computation. For data exceeding 32 bytes or with variable length, use dynamic bytes or string types, though these incur higher gas costs due to length management overhead.

Enums create user-defined types with a finite set of constant values, internally represented as integers starting from 0. They're ideal for modeling states like order status, voting options, or contract phases. Using enums instead of raw integers makes code more readable and self-documenting while preventing invalid state assignments.

Code Breakdown

6
bool stores true or false values, consuming a full 32-byte storage slot.
16
address is a 20-byte type specific to EVM for handling Ethereum accounts.
19
bytes32 efficiently stores fixed 32-byte data like hashes or identifiers.
22
enum Status defines a custom type where Pending=0, Shipped=1, Delivered=2.