Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 47 ยท Language Basics

Value Types

Learn Solidity's value types: bool, uint and int sizes, address and address payable, fixed bytes, enums, literals and default values, with BigInt stand-ins.

Solidity is a statically typed language: every variable has a type that the compiler knows, and you must write it. That strictness is a feature on a blockchain, where a wrong assumption about a number can lose real money. The simplest types are called value types. A value type is stored directly, and when you assign it or pass it to a function, you get a copy. (Types that are copied only on request, such as arrays and structs, come next in Strings, Bytes, Arrays and Structs.)

This lesson covers booleans, integers, addresses, fixed-size byte arrays and enums, plus the literals and default values you will use constantly. Solidity code is shown read-only, and JavaScript BigInt stand-ins let you play with the same ranges and rules.

The value types

bool true or false.
uint / int Unsigned and signed whole numbers.
address A 20-byte account identifier.
bytes1 to bytes32 Fixed-size raw bytes.

Enums round out the list.

1 / 2

Booleans

bool holds true or false. It supports !, &&, ||, == and !=. The && and || operators short-circuit, which means the right side is not evaluated if the left side already decides the answer. That matters when the right side costs gas or can fail.

SOLIDITY
bool public isOpen = true;
bool public hasVoted;       // defaults to false

There is no "truthy" conversion. Writing if (1) or if (someNumber) is an error: a condition must be a real bool.

Integers: uint and int

Integers come in two families. uint is unsigned (zero and up) and int is signed (negative numbers too). Each has sizes from 8 to 256 bits in steps of 8: uint8, uint16, ..., uint256. The names uint and int alone mean uint256 and int256.

SOLIDITY
uint8 public small = 255;        // max for 8 bits
uint256 public big = 1_000_000;  // underscores are allowed
int256 public temperature = -40;

An n-bit uint holds 0 to 2^n - 1. An n-bit int holds -2^(n-1) to 2^(n-1) - 1. In code you can read these limits as type(uint8).max, type(int8).min and so on.

Beginners often wonder why they should not simply use uint8 everywhere to "save space". The EVM works in 256-bit words, so smaller types are not faster by themselves. They save space only when several small variables are packed into one storage slot (see Storage, Memory and Calldata), and conversions can even cost extra. The safe default is uint256, and smaller types are for packing or for matching an external format.

What happens on overflow, such as adding 1 to a uint8 holding 255, is the topic of Integers, Overflow and Checked Math. Integer division also truncates: 7 / 2 is 3. There are no decimal types for general use, so fractions are handled with scaling.

Addresses

An address holds a 20-byte value, the identifier of an account or contract. It has two forms:

  • address: a plain address. You can read its balance and test whether it has code, but you cannot send ETH to it directly.
  • address payable: the same, plus the ability to send ETH with .transfer or .send, and low-level .call.
SOLIDITY
address public owner = 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4;
address payable public treasury;

function pay() external {
    uint256 b = owner.balance;       // read a balance in wei
    treasury.transfer(0);            // only payable addresses
}

address zero = address(0);           // the zero address
address(this);                       // the address of this contract

You convert with payable(addr) when you intend to send ETH. The distinction is a compile-time safety net that forces you to be explicit. The zero address (address(0)) is the default and is used as "nobody" or "burn". Many bugs come from forgetting that an unset address is not an error, just zero, so contracts often check require(newOwner != address(0)). Sending ETH is covered in Sending and Receiving Ether.

Fixed-size bytes

bytes1 through bytes32 hold exactly that many raw bytes. They are cheap and good for hashes (bytes32 is the size of a Keccak-256 hash), identifiers and flags. You can index a single byte (b[0]), read .length, and compare, shift and mask them like numbers.

SOLIDITY
bytes4 public selector = 0xa9059cbb;   // 4 bytes
bytes32 public id = keccak256("hello");

Hex literals that fit a type exactly are accepted, and bytesN values are left-aligned: a short literal pads zeros on the right. This is the opposite of numbers, which pad on the left. Do not confuse bytes32 with the dynamic bytes and string types, which are reference types.

Enums

An enum is a named list of options, great for a state machine. Internally it is a small integer starting at 0.

SOLIDITY
enum Status { Pending, Active, Closed }

Status public status;               // defaults to Pending (the first member)

function open() external {
    status = Status.Active;
}

The first member is the default value. Converting an integer to an enum with a value beyond the last member reverts, so out-of-range states cannot sneak in. Enums make code readable (Status.Closed instead of 2), and they appear in nearly every project in the Projects chapter.

Literals

Solidity has handy literal forms that appear constantly:

Literal Meaning
1 wei, 1 gwei, 1 ether 1, 10^9 and 10^18 wei
1 seconds, 1 minutes, 1 hours, 1 days, 1 weeks Durations as seconds (60, 3600, 86400, ...)
1_000_000 Underscores for readability
2e3 Scientific form, equal to 2000
0xff Hexadecimal

Note that suffixes like ether and days are just multipliers, so 2 days is the integer 172800. There is no separate "time" type. The unit suffixes are covered in full in Operators, Ether Units and Time Units.

Two more literal forms create byte data. A hex literal such as hex"001122" is a sequence of bytes written in hexadecimal (an even number of digits), and it converts to bytes3, bytes or any large-enough bytesN. A string literal like "hello" converts to bytes5, bytes or string. Both can be assigned directly:

SOLIDITY
bytes public raw = hex"001122";        // 3 bytes
bytes5 public word = "hello";          // exactly 5 bytes, left-aligned
string public greeting = "hello";      // dynamic UTF-8 text

Reserved words such as enum, struct, mapping, contract, library and interface are keywords and cannot be used as variable names.

Default values

Every variable has a defined value from the moment it exists. Nothing is ever uninitialized:

Type Default
bool false
uint / int 0
address address(0)
bytesN all zero bytes
enum its first member

This is important for mappings, which return the default for any key that was never set.

Exploring ranges with BigInt

JavaScript's ordinary Number is a 64-bit float and cannot hold a uint256. BigInt can, so it is the natural stand-in. The code below builds the range table for each integer size and checks whether values fit. It does not enforce Solidity's rules, only shows them.

JS Integer ranges for every size

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Now model an address check, a default value and an enum with its bounds check. Addresses here are strings of 40 hex digits, which is what 20 bytes look like in text.

JS Addresses, defaults and enums

Press Run (or Ctrl + Enter) to see the console output here.

No output.

What is real: the type list, ranges, defaults, literal multipliers, and enum bounds follow Solidity. What is the stand-in: BigInt has no fixed width, so it will not wrap or revert on its own, and a JavaScript string is not a real 20-byte address with a checksum. How overflow behaves in real Solidity comes in the next lesson.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does a plain uint mean?

    uint is shorthand for uint256, and int for int256. 256 bits matches the EVM's word size.
  2. 2. What is the difference between address and address payable?

    Both are 20-byte values. The payable form allows sending ETH directly, and the type difference makes you opt in explicitly.
  3. 3. What is the default value of an enum variable?

    Enums are stored as integers starting at 0, and every variable starts at its zero value, so it begins as the first member.
  4. 4. What does 2 days evaluate to in Solidity?

    Time units are just multipliers that produce seconds: 2 days is 2 x 86400 = 172800.
  5. 5. What is 7 / 2 for unsigned integers in Solidity?

    There are no fractions in integer math; the remainder is dropped. Fixed-point scaling is used when you need fractions.

Summary

  • Value types (bool, integers, addresses, fixed bytes, enums) are copied on assignment.
  • uint and int mean 256 bits; smaller sizes help mainly when packing storage.
  • Integer division truncates, and there are no general decimal numbers.
  • address payable can receive ETH; address(0) is the default "nobody" value.
  • bytes1..bytes32 are fixed-size and left-aligned; enums are small integers with bounds checks.
  • Literals like 1 ether and 2 days are multipliers, and every type has a zero default value.

Next: Integers, Overflow and Checked Math.