Do you want to know how to create cryptocurrency tokens but you don’t know where to start in this information overload? It may seem hard and maybe even intimidating, but it doesn’t have to be! In this video, we’ll show you the different data types that are key to understanding the process of cryptocurrency creation. We’ll get to know the integers, booleans, fixed-point numbers, bytes, and strings. We’ll even introduce you to special data types like addresses. You’ll also know more in-depth about the reference types. To find out more watch our explanatory video and read the article below:
Welcome to lesson 4. In this lesson, I will discuss all the available data types in solidity language. First I will look at the value types and, they are called value types because the variables of these types are always passed by value, meaning that they are always copied when they are used as function arguments or in assignments. The first most basic value type is Booleans. The possible values for a Boolean are true and false like in any programming language and to simply declare it by typing bool, then the variable name, and then equals either true or false.
Another really common data type is integers. Which are declared by using keywords “int” and “uint”. Int stands for signed integer, which where, 255 bits are used to represent the value and one bit is used to denote a sign. “Uint” stands for unsigned integer, where all 256 bits are used to represent the value. Keywords “uint” and “int” go up in increments of 8. From the lowest value of 8 to the maximum value of 256, for example, int8, int16, uint16, etc. “Uint” and int by default are aliases for uint256 and int256, respectively.
Now, let’s have a look at a short example of how integers behave. Let’s copy this code and go to: remix.ethereum.org. Paste the code, you can see I have already done that before, and let’s explore what it does. As you can see there are three functions: “getFoobar, getMaxUint and getMaxInt”.
“GetFoobar” simply adds up two integer numbers, 42 and 50 and returns them. Let’s compile the contract again by clicking on create, and click here: “getFoobar” and execute the function, and we get the answer 92. These two functions “getMaxUint” and “getMaxint” are nothing special. They just declare the highest possible numbers for an unsigned integer here and a signed integer here. We don’t really have to know the highest values, it just for fun. You can see that the highest value for an unsigned integer is a lot larger that than for a signed integer. Also I am showing you this just to show that it’s possible to declare integer values by using hexadecimal notation rather than just typing, let’s say 255. Sometimes it’s more convenient but you don’t have to use it.
Now, let’s have a look at another data type: fixed point numbers. Fixed point numbers are denoted by keywords “fixed” and “ufix”-ed. Fixed stands for signed “fixed” point number and “ufix” stands for unsigned fixed point number. However, keep in mind that fixed point numbers are not fully supported yet by solidity. They can be declared but cannot be assigned to or from. I am only showing you this so that you are aware that fixed point numbers are coming to solidity and that you can declare them but you cannot use them yet. So if you want to use float type division in solidity, different size units have to be used.
Bytes is another familiar data type, which can be used to store raw byte data. Bytes is essentially a dinamic array of bytes, where byte is an alias for bytes 1 which is an array of one byte. The maximum bytes array is bytes 32 size, and you can see the example of it here, where I am declaring the maximum available bytes array and instead as we saw in the example for integers we got when we declared it like this, we got a huge number, in this case if we return this variable we will just get FFFFF 64 times.
Strings is another very common data type. String literals are written divider double or single quotes. As the integer literals the type can vary but they are implicitly convertible to bytes type. Under the hood, strings are basically an arbitrary length byte array.
So let’s set the new string by typing a, b, c, don’t forget to use double quotes or single quotes around it, and as we can see if we retrieve a string we retrieve a, b, c.
Now, let’s just for fun, let’s have a look how much this transaction cost. It cost 42 826 gas. Now remember that a string is a bytes array under the hood, so if we set this string a lot larger the bytes array will automatically be a lot larger and remember that everything in the theorem virtual machine costs gas to execute and memory takes gas as well. So the larger the memory, the larger the bytes array the more gas it will take to execute. So, let’s set this string and let’s see how much this transaction cost, and we can see that this transaction costs 70 693 gas. So that’s a lot more than if we set a short string.
Now, let’s have a look at addresses. Address is a special data type that holds a 20 byte value which is the size of an Ethereum address. Address types also have members and serve as a base for all contracts. Address comes with type members which are balanced and transfer and the balance makes it possible to query the balance of an address, and the transfer member makes it possible to send Ether in units of way to another address.
Now, if we look at this function, get default choice, it returns the default choice which we set as go straight, and the answer which we get is two. So the default choice is integer two. And in this case the Enum since it’s indexed from 0 to 1 go straight indeed does refer to the index two.
Now we can set the choice by ourselves by using this function: for example set sit still and we set our choice to action choices.sit still. If we do so and then retrieve the choice which hasn’t been declared yet, and here we use this function, get choice, we get the uint8 of 3. So, sit still is at index three in action choices Enum, and we, and that, this is what it is. So that’s how Enums work. They’re really useful when to make code more readable. So we had a look at all the value types that exist in solidity.
Now, let’s have a look at the reference types. Reference types are more complex types than value types because they do not always fit into 256 bits, and thus have to be handled more carefully than value types. Since copying them can be quite expensive, we have to think about whether we want them to be stored in memory, which is not persisting, or storage where the state variables are held.
Before we look at the reference types in more detail we need to have a look at the data locations that exist in solidity and that’s memory and storage. Memory and storage are analogous to memory and hard drive storage in a computer. The contract can use any amount of memory as long as it pays for it during execution of it’s code, but when execution stops the entire content of the memory is wiped and the next execution will start fresh.
The storage on the other hand is persistent into the blockchain itself so the next time the contract executes some code it has excess to all the data it previously stored into it’s storage area, so be careful about which one you use. If you do not specify whether memory or storage should be used, solidity by default will use storage for structs and arrays even when they are declared as local function variables. This is because arrays and structs are complex data structures and could be of variable length. This is ideal for storage as it operates as a key value data structure which can always be expanded. On the other hand memory dedicates a fixed length space in memory, and thus it’s less expensive to use but it cannot be resized. This is ideal for simpler data types such as integers, booleans and so on. Remember as a rule of thumb state variables are always in storage, function arguments are always in memory and local variables are always in reference storage.
Let’s have a look at structs. Structs is simply a way to define new data types. Let’s have a look at this short example, where we are declaring a new struct book. What this will allow us to do is to define new variables of data type book or make an array of books where each instance in an array is a book and each one of these would contain a title, an author and cost. What this allows to do is to make the code more readable and maintainable and also a lot clearer in most cases.
So this is how mapping works, in short detail. I hope you enjoyed this lesson, if, of course, all the explanations are not in full, full detail, so if you want to read more about it, I would recommend going to official solidity documentation, the types section and have a read more about it here.
I’ll see you next!