Explaining Solidity Value Types

Like every other programming language, Solidity has different data types. These data types are grouped into two categories which are the reference types and value types.
In this article, our focus will be on value types.
A value type is a data type that holds data directly in its own memory. When a variable of this type is being assigned to another variable or passed in a function, the value is copied. Value types in solidity are known to not take more than 32 bytes of storage.
Solidity provides us with the following value types:
- Booleans
- Integers
- Unsigned integers (uint)
- Address
- Byte (This is a representation of a fixed-sized array ranging from byte arrays)
- Enums
- Fixed point numbers
- Booleans: This is a solidity value type that holds two values- true or false. It is declared using the
boolkeyword. Booleans only take up 1 byte of storage and work with boolean operators such as
! = logical negation,
&& - logical and,
|| - logical or,
== - equality,
!= - inequality
Bool isAwake = false;
- Integers: Also known as signed integers. Integers can hold both negative and positive values and are declared using the
intkeyword. By default, an integer takes up 32 bytes, however, if you feel that your variable will not take up that much storage, you can explicitly declare the number of bits you want it to use. This can be done by declaring it using keywords such asint8,int24, up toint256.
Int public num = 230;
- Unsigned integers: Like integers, unsigned integers take up 32 bytes by default, but can be made to use lesser memory by explicitly declaring. Unsigned integers are declared using the
uintkeyword. Whenuintalone is used, it meansuint256. To use lesser memory, It can be declared like thisuint8. Unsigned integers can only hold 0 and positive values. An advantage of this is that it can hold twice the size of positive values compared to its signed counterpart.
uint count = 50;
- Address: This is a data type unique to Solidity. It holds a 20-byte value which is the size of an Ethereum address. The address type has two variations:
addressandaddress payable. They are alike in many ways apart from being able to send and receive money. The plainaddresscannot send money while theaddress payablecan send and receive. Address payable also comes with additional members such astransferandsend.
address person = msg.sender
- Fixed-size byte arrays: This contains a sequence of bytes with their length specified when they are being defined. They are used to store information in binary formats and can be declared in the following ways:
bytes1,bytes2and so on. The prepended numbers can range from 1 to 32.byte, when used without a number prepended, is an alias forbytes1which stores a single byte.
bytes public number = “0x3333”;
Enums: In solidity,
enumstands for enumerable. A first look at it will have you comparing it to JavaScript objects. However, they are not the same. It is a way of creating a predefined set of values. Enums are defined like this:Enum menu { rice, beans, yam, noodles }.Enums are explicitly convertible to integer types but not implicitly. Enum values are numbered in the way you define them. Under the hood, Solidity converts the names to integers. Meaning that in our example above, after conversion will look like this:0=rice, 1 = beans, 2 =yam, and 3 = noodles. The first value in the enum list will be 0, the second will be 1 and so it goes. It’s also been mentioned that with enums, we can reduce the number of bugs in our code. I’m looking forward to that. 😄Fixed-point numbers: This is a way to represent fractional numbers. Just like integers, we have
signedandunsignedfixed-point numbers, and they are defined by using thefixedandufixedkeywords.
They can also be defined by using theufixedMxNandfixedMxNkeywords. Here,Mrepresents the number of bits the type takes andNrepresents the number of decimal points.Mhas to be a number between 8 and 256 that is also divisible by 8, andNhas to be a number between 0 and 80. While fixed-point numbers can be declared in solidity, they do not have full support yet. They can also not be assigned to or from.
Conclusion
Data types in solidity are a little bit different from that of other languages. Coming from a language like JavaScript, some of these types can be confusing at first, but within a short period of time, you’ll grasp them and get moving.



