Lesson 2: Value Types in Solidity

Lajos Deme
Solidify
Published in
5 min readSep 18, 2021

Introduction to value types

Photo by Michael Dziedzic on Unsplash

In Solidity, like in many other languages, data types can be broadly classified into two groups: reference types and value types. In this article, we will examine the various value types that are present in the language.

Let’s begin by defining what a value type is. A value type is a type that holds the data directly in the memory owned by it. Variables of these types are always passed by value, meaning that they are always copied when assigned to another variable or passed into a function.

The following types are value types in Solidity:

  • booleans
  • integers
  • unsigned integers
  • addresses
  • fixed-size byte arrays (bytes1 to bytes32)
  • enums
  • fixed point numbers (they are not fully supported yet)

Booleans

Booleans, of course, can hold two values true or false. Solidity supports all your regular boolean operators, such as !, &&, == etc. They only take up 1 byte of storage.

bool public a_boolean;

Signed and unsigned integers

Signed integers are declared with the int keyword, unsigned integers with the uint and both take up 32 bytes by default. If you know your variable will never hold that many bytes you can always make it smaller by explicitly specifying the number of bits. For example int128/uint128. You can specify this in steps of 8 starting from 8 up till 256. You can use regular comparison, arithmetic, and bitwise operators on both int and uint.

When it comes to addresses, things start to get a bit more interesting. We will go into greater detail on addresses in another lesson, but for now, you should know that there are two types of addresses, Externally Owned Addresses (EOA) and contract addresses. An EOA is an address to an Ethereum account, just like an inbox has an email address. You can use this to send/receive funds. The contract address is the address associated with a smart contract, and it is used to send/receive money to and from that contract. In both cases, the address is in the format of a 42 character hexadecimal address. The…

Lajos Deme
Solidify

Founder of www.mercuryprotocol.io | We're building the world's data marketplace