New to blockchain software development? Read my beginners guide here

Solidity data types

Created on August 2022 β€’ Tags: solidityguides

A guide on the data types in Solidity


Table of Contents for Solidity data types


Value types

addresses

An address in Solidity is a 20 byte (160 bits) representation of an address. There are two types - address and address payable.

You can convert an address to address payable easily - payable(yourAddress).

Enums

You can use enums like this in solidity:

contract YourContract {
enum Size{SMALL, MEDIUM, LARGE}

Size tshirtSize;

function setSize() public {
tshirtSize = Size.MEDIUM;
}
}

Numbers

  • signed ints
    • int which is equal to int256
    • you can also use anything such as int8, int32 etc
  • unsigned ints
    • uint which is equal to uint256

booleans

  • bool which accepts true or false as values.
  • The default value for a bool is always false

bytes

A byte is a 8 bit signed int. You can use the data types bytes1 up to bytes32

Reference types

Arrays

You can create arrays of multiple elements (of the same type) easily. They can also hold arrays of arrays, arrays of mappings etc.

Fixed size arrays

If you know how many items are in the array, you can set the fixed size. Here is an example of 7 strings.

    string[7] snowWhitesDwarfs;
Dynamic sized arrays

You can also add arrays of a dynamic size

  string[] peopleInGameOfThrones

Strings

Strings are very similar to arrays.

string greeting = "Hello world";

Structs

A struct is your own defined custom data type.

struct Person {
uint age;
string name;
}

Mapping

You can use a mapping like this:

mapping (uint => address) balances;

This in typescript would be Record<number, string>.

Converting between types in Solidity

There are a few ways to convert between types in Solidity - read my guide here for more!.

Spotted a typo or have a suggestion to make this crypto dev article better? Please let me know!

See all posts (70+ more)

See all posts (70+ more)

Was this post helpful? πŸ“§

If you liked this content and want to receive emails about future posts like this, enter your email. I'll never spam you.

Or follow me on @CryptoGuide_Dev on twitter

By using this site, you agree that you have read and understand its Privacy Policy and Terms of Use.
Use any information on this site at your own risk, I take no responsibility for the accuracy of safety of the information on this site.