Reference data types

Reference data types don’t hold value directly; instead, they store the location of the data. With the help of reference types, two different variables can refer to the same location, and any modification made in one variable can affect the other one. We’ll demonstrate this soon. There are a number of reference types; let’s take a look at them.

Strings and bytes

Strings and bytes are special types of arrays in Solidity. The bytes type is used to store a fixed-sized character set, while the string data type is used to store a character set equal to or more than a byte. The string type is equal to bytes but doesn’t allow us to check its length or index access. This means that unlike in other languages, in Solidity we cannot use string.length to check for the length of a string or string[3] to get the character at index 3. However, this is possible with bytes. The bytes type also has the advantage of using less gas, so it’s better to use it when we know the data length. Increments go from bytes1 to bytes32. Here’s how we can initialize strings and bytes in Solidity using string literals:

Get hands-on with 1400+ tech skills courses.