Introduction to value types
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:
- unsigned integers
- fixed-size byte arrays (
- fixed point numbers (they are not fully supported yet)
Booleans, of course, can hold two values
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
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
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…