TExcelTAD
Home › Solidity › Value Types

Value Types

Solidity is statically typed: every variable has a type that is known at compile time. Types fall into two families:

  • Value types are copied whenever they are assigned or passed to a function: booleans, integers, addresses, fixed-size byte arrays, enums.
  • Reference types (arrays, strings, structs, mappings) can be large, so you must say where they live. They get their own lessons.

Overview

Type Values Default
bool true, false false
uint8 … uint256 (uint) unsigned integers, in steps of 8 bits 0
int8 … int256 (int) signed integers 0
address a 20-byte account address address(0)
address payable an address you can send ether to address(0)
bytes1 … bytes32 fixed-size byte sequences all zero bytes
enum one of a list of named values the first value

There is no null or undefined. Every variable starts at its type's zero value, so "not set yet" and "set to zero" look the same. Keep that in mind when designing contracts.

solidity · try it yourself
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import "hardhat/console.sol";

contract Main {
    bool public flag;
    uint256 public amount;
    int256 public delta;
    address public account;
    bytes32 public id;

    function main() external view {
        console.log("bool   :", flag);
        console.log("uint256:", amount);
        console.logInt(delta);
        console.log("address:", account);
        console.logBytes32(id);
    }
}
bool   : false
uint256: 0
0
address: 0x0000000000000000000000000000000000000000
0x0000000000000000000000000000000000000000000000000000000000000000

console.log accepts uint256, string, bool and address values. For other types use the dedicated helpers such as console.logInt and console.logBytes32.

Booleans

bool supports ! (not), && (and), || (or), == and !=. && and || short-circuit: in a && b, b is not evaluated if a is false, which also saves gas.

solidity · try it yourself
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import "hardhat/console.sol";

contract Main {
    function canVote(uint256 age, bool registered) internal pure returns (bool) {
        return age >= 18 && registered;
    }

    function main() external pure {
        console.log("17, registered  :", canVote(17, true));
        console.log("30, registered  :", canVote(30, true));
        console.log("30, unregistered:", canVote(30, false));
        console.log("not true        :", !true);
    }
}
17, registered  : false
30, registered  : true
30, unregistered: false
not true        : false

Unlike JavaScript, there is no "truthiness": if (1) or if (someAddress) do not compile. Conditions must be real bool expressions such as x != 0.

Integers at a glance

uint256 is the workhorse type of Solidity: balances, counters, timestamps and IDs are almost always uint256. The EVM works on 256-bit words, so smaller types are not faster on their own; they only save space when several are packed together in storage.

solidity · try it yourself
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import "hardhat/console.sol";

contract Main {
    function main() external pure {
        uint8 small = 200;
        uint256 big = 2 ** 200;
        int16 negative = -1234;

        console.log("uint8 max  :", type(uint8).max);
        console.log("small + 55 :", small + 55);
        console.log("2**200     :", big);
        console.logInt(negative);
        console.log("7 / 2      :", uint256(7) / 2);   // integer division
        console.log("7 % 2      :", uint256(7) % 2);
    }
}
uint8 max  : 255
small + 55 : 255
2**200     : 1606938044258990275541962092341162602522202993782792835301376
-1234
7 / 2      : 3
7 % 2      : 1

Division always rounds toward zero, because there are no fractions. The next lesson covers integers in depth, including what happens when a value doesn't fit.

Type conversions

Converting from a smaller to a larger integer type is implicit. Going the other way, or between signed and unsigned, needs an explicit conversion, which may cut off high bits:

solidity · try it yourself
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import "hardhat/console.sol";

contract Main {
    function main() external pure {
        uint8 a = 250;
        uint256 widened = a;            // implicit: always safe
        uint16 value = 300;
        uint8 truncated = uint8(value); // explicit: keeps the low 8 bits
        int8 minusOne = -1;
        uint8 reinterpreted = uint8(minusOne);

        console.log("widened      :", widened);
        console.log("uint8(300)   :", truncated);
        console.log("uint8(int8 -1):", reinterpreted);
    }
}
widened      : 250
uint8(300)   : 44
uint8(int8 -1): 255

Warning: Explicit conversions never revert. uint8(300) silently becomes 44 (300 − 256). Check the range yourself before narrowing a value.

Literals

  • Underscores make long numbers readable: 1_000_000.
  • Scientific notation works for integers: 2e18 is 2000000000000000000.
  • Hex literals start with 0x: 0xff is 255.
  • Number literals are computed with unlimited precision at compile time, so 2**255 * 2 / 4 is fine as long as the final value fits.

Test yourself

What is the initial value of an unassigned address state variable?

  1. null
  2. undefined
  3. address(0)
  4. The deployer's address

What does uint8(uint16(300)) evaluate to?

  1. It reverts
  2. 255
  3. 44
  4. 300

Which condition compiles in Solidity?

  1. if (balance)
  2. if (balance != 0)
  3. if (!balance)
  4. if (balance?)