The Nitty-gritty of Value Types in Solidity

Ifeoma Sandra
Coinmonks

--

If variables are containers, then datatypes are the type of containers. The type of a container tells what kind of stuff can be put in it. For example, you don’t want to store fresh flowers in an enclosed sack bag 🤷‍♀️. In the same way you don’t want to store an integer value in a variable of datatype string…

Solidity is a statically typed language which means that we have to explicitly define the types of each of the variables.

Like in other programming languages, datatypes in solidity can be grouped into two major types:

  • value types
  • reference types

The difference between the value types and the reference types in solidity basically lies in the way they are being assigned to a variable and stored in the Ethereum virtual machine (EVM).

For the scope of this article, we will be laying emphasis on value types in solidity.

What really are value types?

A value type is a datatype that stores data in its own memory. They are called value types because the variables of these types are always passed by value. Hmm🤔 passed by value… what does it mean? This simply means that they are always copied when assigned to another variable or passed into a function as arguments.

The list below are the value types in solidity:

  • booleans
  • integers
  • addresses
  • fixed-size byte arrays
  • enums
  • fixed point numbers(these types are not fully supported yet)

Let’s delve into each of them! 🚀

Booleans

Booleans can hold two possible values. These values are constants which can either be true or false. All boolean operators such as !, &&, ||, ==, != are fully supported in solidity.

bool public myFirstBool;

Integers

Integers in solidity is a datatype used to store integer values and they are of two types:

  • signed integer declared as int
int public myFirstInt;
  • unsigned integer declared as uint
uint public myFirstUint;

Signed and Unsigned integers are of various sizes ranging from int8 to int256 and uint8 to uint256 respectively in steps of 8 (signed or unsigned of 8 up to 256 bits).

Integers support the following operators:

  • comparison operators: <=, <, == ,!=, >=, >
  • bit operators: & , | , ^ , ~
  • shift operators: << , >>
  • arithmetic operators: + , - , * , / , ** , %

Learn more about how these operators work with integers here.

Addresses

Generally, a variable of the type address holds 20 bytes which is the size of an Ethereum address. Addresses are of two types:

  • address
  • address payable

The address and address payable are almost the same but are also different. Okay…so how are they different?

The difference between the address and the address payable is that address payable is an address you can send ether to, while address cannot be sent ether. Also, address payable has additional members: the transfer and send.

Addresses can be converted from one type to the other in two ways: implicit conversions and explicit conversions.

  • Implicit conversions: Implicit conversion from address payable to address is allowed, but conversion from address to address payable must be done explicitly through payable(address).
  • Explicit conversions: uint160, integer literals, bytes20, and contract types allow explicit conversion to and from address.

Fixed-size Byte Arrays

Bytes are used to store a fixed-sized character set while the string is used to store the character set equal to or more than a byte. The length of bytes is from 1 to 32 (byte1, byte2, byte3,…, byte32) while the string has a dynamic length. The advantage byte has over string is that it uses less gas, so better to use when we know the length of data.

Bytes supports boolean operators, comparison operators and bitwise operators.

Enums

Enums restrict a variable to have one of only a few predefined values. The values in this enumerated list are called enums which makes the contract more readable, maintainable, and less prone to errors. Options of enums can be represented by unsigned integer values starting from 0.

see example below

we can also set a default value here 👇

Fixed Point Numbers

These data types are not fully supported in solidity yet, following the Solidity documentation. They can be declared as fixed and unfixed for signed and unsigned fixed-point numbers of varying sizes respectively.

Conclusion:

In this article, we learned about solidity value types and how each of them works.

Thank you 🤗 for sticking to the end. Enjoyed reading this article? 😉 Kindly leave your feedback and follow for more amazing articles to come.

Join Coinmonks Telegram Channel and Youtube Channel learn about crypto trading and investing

Also, Read

--

--

Ifeoma Sandra
Coinmonks

Ifeoma is passionate about technology and its many applications with experience in backend web development (NodeJs/NestJs).